|
1 | 1 | const path = require("path");
|
2 | 2 | const { deepClone, styleRuleByName } = require("./utils");
|
| 3 | +const { loaderByName, throwUnexpectedConfigError } = require("@craco/craco"); |
3 | 4 |
|
4 | 5 | const lessRegex = /\.less$/;
|
5 | 6 | const lessModuleRegex = /\.module\.less$/;
|
6 | 7 |
|
7 |
| -const overrideWebpackConfig = ({ context, webpackConfig, pluginOptions }) => { |
8 |
| - const { loaderByName, throwUnexpectedConfigError } = require("@craco/craco"); |
| 8 | +const throwError = (message, githubIssueQuery) => |
| 9 | + throwUnexpectedConfigError({ |
| 10 | + packageName: "craco-less", |
| 11 | + githubRepo: "DocSpring/craco-less", |
| 12 | + message, |
| 13 | + githubIssueQuery, |
| 14 | + }); |
9 | 15 |
|
| 16 | +const overrideWebpackConfig = ({ context, webpackConfig, pluginOptions }) => { |
10 | 17 | // This is mocked in Windows tests
|
11 | 18 | const pathSep = module.exports.pathSep;
|
12 |
| - |
13 |
| - const throwError = (message, githubIssueQuery) => |
14 |
| - throwUnexpectedConfigError({ |
15 |
| - packageName: "craco-less", |
16 |
| - githubRepo: "DocSpring/craco-less", |
17 |
| - message, |
18 |
| - githubIssueQuery, |
19 |
| - }); |
20 |
| - |
21 | 19 | pluginOptions = pluginOptions || {};
|
22 | 20 |
|
23 | 21 | const createLessRule = ({ baseRule, overrideRule }) => {
|
@@ -174,8 +172,44 @@ const overrideWebpackConfig = ({ context, webpackConfig, pluginOptions }) => {
|
174 | 172 | return webpackConfig;
|
175 | 173 | };
|
176 | 174 |
|
| 175 | +const overrideJestConfig = ({ context, jestConfig }) => { |
| 176 | + const moduleNameMapper = jestConfig.moduleNameMapper; |
| 177 | + const cssModulesPattern = Object.keys(moduleNameMapper).find((p) => |
| 178 | + p.match(/\\\.module\\\.\(.*?css.*?\)/) |
| 179 | + ); |
| 180 | + |
| 181 | + if (!cssModulesPattern) { |
| 182 | + throwError( |
| 183 | + `Can't find CSS Modules pattern under moduleNameMapper in the ${context.env} jest config!`, |
| 184 | + "jest+moduleNameMapper+css" |
| 185 | + ); |
| 186 | + } |
| 187 | + |
| 188 | + moduleNameMapper[cssModulesPattern.replace("css", "css|less")] = |
| 189 | + moduleNameMapper[cssModulesPattern]; |
| 190 | + delete moduleNameMapper[cssModulesPattern]; |
| 191 | + |
| 192 | + const transformIgnorePatterns = jestConfig.transformIgnorePatterns; |
| 193 | + const cssModulesPatternIndex = transformIgnorePatterns.findIndex((p) => |
| 194 | + p.match(/\\\.module\\\.\(.*?css.*?\)/) |
| 195 | + ); |
| 196 | + if (cssModulesPatternIndex === -1) { |
| 197 | + throwError( |
| 198 | + `Can't find CSS Modules pattern under transformIgnorePatterns in the ${context.env} jest config!`, |
| 199 | + "jest+transformIgnorePatterns+css" |
| 200 | + ); |
| 201 | + } |
| 202 | + |
| 203 | + transformIgnorePatterns[cssModulesPatternIndex] = transformIgnorePatterns[ |
| 204 | + cssModulesPatternIndex |
| 205 | + ].replace("css", "css|less"); |
| 206 | + |
| 207 | + return jestConfig; |
| 208 | +}; |
| 209 | + |
177 | 210 | // pathSep is mocked in Windows tests
|
178 | 211 | module.exports = {
|
179 | 212 | overrideWebpackConfig,
|
| 213 | + overrideJestConfig, |
180 | 214 | pathSep: path.sep,
|
181 | 215 | };
|
0 commit comments