|
1 | 1 |
|
2 | | -const withTypedCssModules = (nextConfig = {}) => ({ |
3 | | - ...nextConfig, |
4 | | - webpack (config) { |
5 | | - const cssLoaderRegex = /(?<!post)css-loader/; |
6 | | - const typingsForCssModulesLoader = { |
7 | | - loader: "@teamsupercell/typings-for-css-modules-loader", |
8 | | - options: { |
9 | | - disableLocalsExport: true, |
10 | | - verifyOnly: process.env.NODE_ENV === "production", |
11 | | - banner: `// AUTOGENERATED FILE -- DO NOT EDIT DIRECTLY\n`, |
12 | | - }, |
13 | | - }; |
| 2 | +const withTypedCssModules = (webpackConfig) => { |
| 3 | + const cssLoaderRegex = /(?<!post)css-loader/; |
| 4 | + const typingsForCssModulesLoader = { |
| 5 | + loader: "@teamsupercell/typings-for-css-modules-loader", |
| 6 | + options: { |
| 7 | + disableLocalsExport: true, |
| 8 | + verifyOnly: process.env.NODE_ENV === "production", |
| 9 | + banner: `// AUTOGENERATED FILE -- DO NOT EDIT DIRECTLY\n`, |
| 10 | + }, |
| 11 | + }; |
14 | 12 |
|
15 | | - // looking for the webpack rules where `css-loader` is specified |
16 | | - const { oneOf } = config.module.rules.find((rule) => Array.isArray(rule.oneOf)); |
17 | | - const rules = oneOf.filter((rule) => Array.isArray(rule.use)); |
| 13 | + // looking for the webpack rules where `css-loader` is specified |
| 14 | + const { oneOf } = webpackConfig.module.rules.find((rule) => Array.isArray(rule.oneOf)); |
| 15 | + const rules = oneOf.filter((rule) => Array.isArray(rule.use)); |
18 | 16 |
|
19 | | - rules.forEach((rule) => { |
20 | | - const cssLoaderIndex = rule.use.findIndex((loader) => cssLoaderRegex.test(loader.loader)); |
21 | | - // no `css-loader` here -> skip |
22 | | - if (cssLoaderIndex === -1) { return; } |
23 | | - // webpack loaders are executed in reverse order |
24 | | - // we want to inject the typings loader after `css-loader`, |
25 | | - // but before other loaders `style-loader` or `mini-css-extract` |
26 | | - rule.use.splice(cssLoaderIndex, 0, { ...typingsForCssModulesLoader }); |
27 | | - }) |
| 17 | + rules.forEach((rule) => { |
| 18 | + const cssLoaderIndex = rule.use.findIndex((loader) => cssLoaderRegex.test(loader.loader)); |
| 19 | + // no `css-loader` here -> skip |
| 20 | + if (cssLoaderIndex === -1) { return; } |
| 21 | + // webpack loaders are executed in reverse order |
| 22 | + // we want to inject the typings loader after `css-loader`, |
| 23 | + // but before other loaders `style-loader` or `mini-css-extract` |
| 24 | + rule.use.splice(cssLoaderIndex, 0, { ...typingsForCssModulesLoader }); |
| 25 | + }) |
28 | 26 |
|
29 | | - return config; |
30 | | - } |
31 | | -}) |
| 27 | + return webpackConfig |
| 28 | +} |
| 29 | + |
32 | 30 |
|
33 | 31 | /** @type {import('next').NextConfig} */ |
34 | | -module.exports = withTypedCssModules({ |
| 32 | +module.exports = { |
35 | 33 | reactStrictMode: true, |
36 | | -}) |
| 34 | + webpack (config) { |
| 35 | + config = withTypedCssModules(config) |
| 36 | + return config |
| 37 | + } |
| 38 | +} |
0 commit comments