|
| 1 | +/** @type {import('next').NextConfig} */ |
| 2 | +import { dirname, relative } from 'path'; |
| 3 | +import hash from 'string-hash'; |
| 4 | +import { fileURLToPath } from 'url'; |
| 5 | + |
| 6 | +const __filename = fileURLToPath(import.meta.url); |
| 7 | +const __dirname = dirname(__filename); |
| 8 | +const context = __dirname; |
| 9 | + |
| 10 | +const nextConfig = { |
| 11 | + webpack(config) { |
| 12 | + // Grab the existing rule that handles SVG imports |
| 13 | + const fileLoaderRule = config.module.rules.find((rule) => |
| 14 | + rule.test?.test?.('.svg'), |
| 15 | + ); |
| 16 | + |
| 17 | + // SVG Rules. |
| 18 | + // |
| 19 | + config.module.rules.push( |
| 20 | + // Reapply the existing rule, but only for svg imports ending in ?url |
| 21 | + { |
| 22 | + ...fileLoaderRule, |
| 23 | + test: /\.svg$/i, |
| 24 | + resourceQuery: /url/, // *.svg?url |
| 25 | + }, |
| 26 | + // Apply svgr to svgs and keep their IDs from clashing |
| 27 | + { |
| 28 | + test: /\.svg$/i, |
| 29 | + issuer: { |
| 30 | + and: [/\.(ts|tsx|js|jsx|md|mdx)$/], |
| 31 | + }, |
| 32 | + // fileLoaderRule.issuer, |
| 33 | + resourceQuery: { not: [...fileLoaderRule.resourceQuery.not, /url/] }, |
| 34 | + use: ({ resource }) => ({ |
| 35 | + loader: '@svgr/webpack', |
| 36 | + options: { |
| 37 | + prettier: false, |
| 38 | + svgo: true, |
| 39 | + titleProp: true, |
| 40 | + svgoConfig: { |
| 41 | + plugins: [ |
| 42 | + { |
| 43 | + name: 'cleanupIds', |
| 44 | + params: { |
| 45 | + remove: true, |
| 46 | + minify: true, |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: 'prefixIds', |
| 51 | + params: { |
| 52 | + delim: '-', |
| 53 | + prefixClassNames: true, |
| 54 | + prefixIds: true, |
| 55 | + prefix: () => `svg${hash(relative(context, resource))}`, |
| 56 | + }, |
| 57 | + }, |
| 58 | + // { |
| 59 | + // name: 'preset-default', |
| 60 | + // params: { |
| 61 | + // overrides: { removeViewBox: false }, |
| 62 | + // }, |
| 63 | + // }, |
| 64 | + ], |
| 65 | + }, |
| 66 | + }, |
| 67 | + }), |
| 68 | + }, |
| 69 | + ); |
| 70 | + |
| 71 | + // Modify the file loader rule to ignore *.svg, since we have it handled now. |
| 72 | + fileLoaderRule.exclude = /\.svg$/i; |
| 73 | + |
| 74 | + return config; |
| 75 | + }, |
| 76 | +}; |
| 77 | + |
| 78 | +export default nextConfig; |
0 commit comments