index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. module.exports = (nextConfig = {}) => {
  2. return Object.assign({}, nextConfig, {
  3. webpack(config, options) {
  4. const { isServer } = options;
  5. if (!options.defaultLoaders) {
  6. throw new Error(
  7. 'This plugin is not compatible with Next.js versions below 5.0.0 https://err.sh/next-plugins/upgrade'
  8. );
  9. }
  10. const assetPrefix = nextConfig.assetPrefix || '';
  11. const enableSvg = nextConfig.enableSvg || false;
  12. const limit = nextConfig.inlineFontLimit || 8192;
  13. // let testPattern = /\.(woff(2)?|eot|ttf|otf)(\?v=\d+\.\d+\.\d+)$/;
  14. let testPattern = /\.(woff(2)?|eot|ttf|otf)(\?v=\d+\.\d+\.\d+)?$/;
  15. if (enableSvg) testPattern = /\.(woff(2)?|eot|ttf|otf|svg)(\?v=\d+\.\d+\.\d+)?$/;
  16. config.module.rules.push({
  17. test: testPattern,
  18. // Next.js already handles url() in css/sass/scss files
  19. issuer: /\.\w+(?<!(s?c|sa)ss)$/i,
  20. use: [
  21. {
  22. loader: require.resolve('url-loader'),
  23. options: {
  24. limit,
  25. fallback: require.resolve('file-loader'),
  26. publicPath: `${assetPrefix}/_next/static/chunks/fonts/`,
  27. outputPath: `${isServer ? "../" : ""}static/chunks/fonts/`,
  28. name: '[name]-[hash].[ext]'
  29. }
  30. }
  31. ]
  32. });
  33. if (typeof nextConfig.webpack === 'function') {
  34. return nextConfig.webpack(config, options);
  35. }
  36. return config;
  37. }
  38. });
  39. };