|
| 1 | +const merge = require('deepmerge'); |
| 2 | + |
1 | 3 | /**
|
2 | 4 | * Factory to make a typings for css module rewire tool
|
3 | 5 | *
|
4 |
| - * @param {object} options This option is the string append after loader eg: '{namedExport: true, camelCase: true } |
| 6 | + * @param {object} options Options to deep merge with the existing options |
| 7 | + * @see https://webpack.js.org/loaders/css-loader/#options |
5 | 8 | */
|
6 | 9 | function createRewireTypingsForCssModule(options) {
|
7 |
| - return function(config, env) { |
8 |
| - // style files regexes |
9 |
| - // This is copy from ejected webpack.config.js |
10 |
| - const cssModuleRegex = /\.module\.css$/; |
11 |
| - const sassModuleRegex = /\.module\.(scss|sass)$/; |
| 10 | + return function (config, env) { |
| 11 | + // Disabled in production: |
| 12 | + if (env === 'production') return config; |
12 | 13 |
|
13 |
| - const devMode = env !== "production"; |
14 |
| - // Only work on dev mode |
15 |
| - if (devMode) { |
16 |
| - const oneOfRule = config.module.rules.find( |
17 |
| - rule => rule.oneOf !== undefined |
18 |
| - ); |
| 14 | + // style files regexes copied from ejected webpack.config.js |
| 15 | + const regexes = [ |
| 16 | + /\.module\.css$/.toString(), |
| 17 | + /\.module\.(scss|sass)$/.toString() |
| 18 | + ]; |
19 | 19 |
|
20 |
| - if (oneOfRule) { |
21 |
| - oneOfRule.oneOf |
22 |
| - .filter(oneOf => { |
23 |
| - return ( |
24 |
| - oneOf.test && |
25 |
| - (oneOf.test.toString() === cssModuleRegex.toString() || |
26 |
| - oneOf.test.toString() === sassModuleRegex.toString()) |
27 |
| - ); |
28 |
| - }) |
29 |
| - .forEach(oneOf => { |
30 |
| - // To insert css-modules-typescript-loader before css-loader |
31 |
| - let cssLoaderIndex = -1; |
32 |
| - oneOf.use.forEach((entry, index) => { |
33 |
| - if (typeof entry == "object") { |
34 |
| - if ( |
35 |
| - entry.loader && |
36 |
| - entry.loader.includes("css-loader") && |
37 |
| - !entry.loader.includes("postcss-loader") |
38 |
| - ) { |
39 |
| - cssLoaderIndex = index; |
40 |
| - } |
41 |
| - } |
42 |
| - return entry; |
43 |
| - }); |
44 |
| - // Add the loader |
45 |
| - if (cssLoaderIndex !== -1) { |
46 |
| - // Append options |
47 |
| - if (oneOf.use[cssLoaderIndex].options) { |
48 |
| - oneOf.use[cssLoaderIndex].options = { |
49 |
| - ...oneOf.use[cssLoaderIndex].options, |
50 |
| - ...options |
51 |
| - }; |
52 |
| - } |
| 20 | + const oneOfs = config.module.rules.find((rule) => !!rule.oneOf).oneOf; |
| 21 | + for (const oneOf of oneOfs) { |
| 22 | + if (!oneOf.test || !regexes.includes(oneOf.test.toString())) continue; |
| 23 | + const cssLoader = oneOf.use.find( |
| 24 | + (entry) => |
| 25 | + entry.loader && |
| 26 | + entry.loader.includes('css-loader') && |
| 27 | + !entry.loader.includes('postcss-loader') |
| 28 | + ); |
| 29 | + cssLoader.options = merge(cssLoader.options, options); |
53 | 30 |
|
54 |
| - oneOf.use.splice( |
55 |
| - cssLoaderIndex, |
56 |
| - 0, |
57 |
| - "css-modules-typescript-loader" |
58 |
| - ); |
59 |
| - console.log(JSON.stringify(oneOf, 2)); |
60 |
| - } |
61 |
| - }); |
| 31 | + // Insert css-modules-typescript-loader before css-loader |
| 32 | + if (!oneOf.use.includes('css-modules-typescript-loader')) { |
| 33 | + const index = oneOf.use.indexOf(cssLoader); |
| 34 | + oneOf.use.splice(index, 0, 'css-modules-typescript-loader'); |
62 | 35 | }
|
63 | 36 | }
|
64 |
| - |
65 | 37 | return config;
|
66 | 38 | };
|
67 | 39 | }
|
68 | 40 |
|
69 | 41 | const rewireTypingsForCssModule = createRewireTypingsForCssModule({
|
70 |
| - localsConvention: "camelCase" |
| 42 | + modules: { |
| 43 | + exportLocalsConvention: 'camelCase' |
| 44 | + } |
71 | 45 | });
|
72 | 46 |
|
73 | 47 | rewireTypingsForCssModule.factory = createRewireTypingsForCssModule;
|
|
0 commit comments