/
craco-less.dev.test.js
366 lines (323 loc) · 12.5 KB
/
craco-less.dev.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
const path = require("path");
const CracoLessPlugin = require("./craco-less");
const {
applyCracoConfigPlugins,
applyWebpackConfigPlugins
} = require("@craco/craco/lib/features/plugins");
const clone = require("clone");
const { craPaths, loadWebpackDevConfig } = require("@craco/craco/lib/cra");
const context = { env: "development", paths: craPaths };
let webpackConfig;
let originalWebpackConfig;
beforeEach(() => {
if (!originalWebpackConfig) {
process.env.NODE_ENV = "development";
originalWebpackConfig = loadWebpackDevConfig({
reactScriptsVersion: "react-scripts"
});
process.env.NODE_ENV = "test";
}
CracoLessPlugin.pathSep = path.sep;
// loadWebpackDevConfig() caches the config internally, so we need to
// deep clone the object before each test.
webpackConfig = clone(originalWebpackConfig);
});
const applyCracoConfigAndOverrideWebpack = cracoConfig => {
cracoConfig = applyCracoConfigPlugins(cracoConfig, context);
webpackConfig = applyWebpackConfigPlugins(
cracoConfig,
webpackConfig,
context
);
};
test("the webpack config is modified correctly without any options", () => {
applyCracoConfigAndOverrideWebpack({
plugins: [{ plugin: CracoLessPlugin }]
});
const oneOfRule = webpackConfig.module.rules.find(r => r.oneOf);
expect(oneOfRule).not.toBeUndefined();
const lessRule = oneOfRule.oneOf.find(
r => r.test && r.test.toString() === "/\\.less$/"
);
expect(lessRule).not.toBeUndefined();
expect(lessRule.use[0].loader).toContain(`${path.sep}style-loader`);
expect(lessRule.use[0].options).toEqual({});
expect(lessRule.use[1].loader).toContain(`${path.sep}css-loader`);
expect(lessRule.use[1].options).toEqual({
importLoaders: 3,
sourceMap: false
});
expect(lessRule.use[2].loader).toContain(`${path.sep}postcss-loader`);
expect(lessRule.use[2].options.ident).toEqual("postcss");
expect(lessRule.use[2].options.plugins).not.toBeUndefined();
expect(lessRule.use[3].loader).toContain(`${path.sep}resolve-url-loader`);
expect(lessRule.use[3].options).toEqual({ sourceMap: false });
expect(lessRule.use[4].loader).toContain(`${path.sep}less-loader`);
expect(lessRule.use[4].options).toEqual({});
});
test("the webpack config is modified correctly without any options on Windows", () => {
CracoLessPlugin.pathSep = "\\";
// Windows uses "\" path separators.
// Note: This is a noop when running tests on Windows.
const replaceSlashesInLoader = rule => {
if (typeof rule === "string") {
return rule.replace(/\//g, "\\");
} else if (rule.loader) {
if (typeof rule.loader === "string") {
// Ignore file-loader, because we use loaderByName from craco.
if (rule.loader.includes(`${path.sep}file-loader${path.sep}`))
return rule;
rule.loader = rule.loader.replace(/\//g, "\\");
} else {
rule.loader = rule.loader.map(replaceSlashesInLoader);
}
} else if (rule.use) {
rule.use = rule.use.map(replaceSlashesInLoader);
}
return rule;
};
webpackConfig.module.rules[2].oneOf = webpackConfig.module.rules[2].oneOf.map(
replaceSlashesInLoader
);
applyCracoConfigAndOverrideWebpack({
plugins: [{ plugin: CracoLessPlugin }]
});
const oneOfRule = webpackConfig.module.rules.find(r => r.oneOf);
expect(oneOfRule).not.toBeUndefined();
const lessRule = oneOfRule.oneOf.find(
r => r.test && r.test.toString() === "/\\.less$/"
);
expect(lessRule).not.toBeUndefined();
expect(lessRule.use[0].loader).toContain("\\style-loader");
expect(lessRule.use[0].options).toEqual({});
expect(lessRule.use[1].loader).toContain("\\css-loader");
expect(lessRule.use[1].options).toEqual({
importLoaders: 3,
sourceMap: false
});
expect(lessRule.use[2].loader).toContain("\\postcss-loader");
expect(lessRule.use[2].options.ident).toEqual("postcss");
expect(lessRule.use[2].options.plugins).not.toBeUndefined();
expect(lessRule.use[3].loader).toContain(`\\resolve-url-loader`);
expect(lessRule.use[3].options).toEqual({ sourceMap: false });
// We use `require.resolve("less-loader")`, so it's the OS separator here
expect(lessRule.use[4].loader).toContain(`${path.sep}less-loader`);
expect(lessRule.use[4].options).toEqual({});
});
test("the webpack config is modified correctly with less-loader options", () => {
applyCracoConfigAndOverrideWebpack({
plugins: [
{
plugin: CracoLessPlugin,
options: {
lessLoaderOptions: {
modifyVars: {
"@less-variable": "#fff"
},
javascriptEnabled: true
}
}
}
]
});
const oneOfRule = webpackConfig.module.rules.find(r => r.oneOf);
expect(oneOfRule).not.toBeUndefined();
const lessRule = oneOfRule.oneOf.find(
r => r.test && r.test.toString() === "/\\.less$/"
);
expect(lessRule).not.toBeUndefined();
expect(lessRule.use[2].loader).toContain(`${path.sep}postcss-loader`);
expect(lessRule.use[2].options.ident).toEqual("postcss");
expect(lessRule.use[2].options.plugins).not.toBeUndefined();
expect(lessRule.use[3].loader).toContain(`${path.sep}resolve-url-loader`);
expect(lessRule.use[3].options).toEqual({ sourceMap: false });
expect(lessRule.use[4].loader).toContain(`${path.sep}less-loader`);
expect(lessRule.use[4].options).toEqual({
javascriptEnabled: true,
modifyVars: {
"@less-variable": "#fff"
}
});
});
test("the webpack config is modified correctly with all loader options", () => {
applyCracoConfigAndOverrideWebpack({
plugins: [
{
plugin: CracoLessPlugin,
options: {
lessLoaderOptions: {
modifyVars: {
"@less-variable": "#fff"
},
javascriptEnabled: true
},
cssLoaderOptions: {
modules: true,
localIdentName: "[local]_[hash:base64:5]"
},
postcssLoaderOptions: {
ident: "test-ident"
},
styleLoaderOptions: {
sourceMaps: true
},
miniCssExtractPluginOptions: {
testOption: "test-value"
}
}
}
]
});
const oneOfRule = webpackConfig.module.rules.find(r => r.oneOf);
expect(oneOfRule).not.toBeUndefined();
const lessRule = oneOfRule.oneOf.find(
r => r.test && r.test.toString() === "/\\.less$/"
);
expect(lessRule).not.toBeUndefined();
expect(lessRule.use[0].loader).toContain(`${path.sep}style-loader`);
expect(lessRule.use[0].options).toEqual({
sourceMaps: true
});
expect(lessRule.use[1].loader).toContain(`${path.sep}css-loader`);
expect(lessRule.use[1].options).toEqual({
modules: true,
importLoaders: 3,
localIdentName: "[local]_[hash:base64:5]",
sourceMap: false
});
expect(lessRule.use[2].loader).toContain(`${path.sep}postcss-loader`);
expect(lessRule.use[2].options.ident).toEqual("test-ident");
expect(lessRule.use[2].options.plugins).not.toBeUndefined();
expect(lessRule.use[3].loader).toContain(`${path.sep}resolve-url-loader`);
expect(lessRule.use[3].options).toEqual({ sourceMap: false });
expect(lessRule.use[4].loader).toContain(`${path.sep}less-loader`);
expect(lessRule.use[4].options).toEqual({
javascriptEnabled: true,
modifyVars: {
"@less-variable": "#fff"
}
});
});
test("the webpack config is modified correctly with the modifyLessRule option", () => {
applyCracoConfigAndOverrideWebpack({
plugins: [
{
plugin: CracoLessPlugin,
options: {
modifyLessRule: (rule, context) => {
if (context.env === "production") {
rule.use[0].options.testOption = "test-value-production";
} else {
rule.use[0].options.testOption = "test-value-development";
}
return rule;
}
}
}
]
});
const oneOfRule = webpackConfig.module.rules.find(r => r.oneOf);
expect(oneOfRule).not.toBeUndefined();
const lessRule = oneOfRule.oneOf.find(
r => r.test && r.test.toString() === "/\\.less$/"
);
expect(lessRule).not.toBeUndefined();
expect(lessRule.use[0].loader).toContain(`${path.sep}style-loader`);
expect(lessRule.use[0].options.testOption).toEqual("test-value-development");
expect(lessRule.use[2].loader).toContain(`${path.sep}postcss-loader`);
expect(lessRule.use[2].options.ident).toEqual("postcss");
expect(lessRule.use[2].options.plugins).not.toBeUndefined();
expect(lessRule.use[3].loader).toContain(`${path.sep}resolve-url-loader`);
expect(lessRule.use[3].options).toEqual({ sourceMap: false });
expect(lessRule.use[4].loader).toContain(`${path.sep}less-loader`);
expect(lessRule.use[4].options).toEqual({});
});
test("throws an error when we can't find file-loader in the webpack config", () => {
let oneOfRule = webpackConfig.module.rules.find(r => r.oneOf);
oneOfRule.oneOf = oneOfRule.oneOf.filter(
r => !(r.loader && r.loader.includes("file-loader"))
);
const runTest = () => {
applyCracoConfigAndOverrideWebpack({
plugins: [{ plugin: CracoLessPlugin }]
});
};
expect(runTest).toThrowError(
"Can't find file-loader in the development webpack config!\n\n" +
"This error probably occurred because you updated react-scripts or craco. " +
"Please try updating craco-less to the latest version:\n\n" +
" $ yarn upgrade craco-less\n\n" +
"Or:\n\n" +
" $ npm update craco-less\n\n" +
"If that doesn't work, craco-less needs to be fixed to support the latest version.\n" +
"Please check to see if there's already an issue in the FormAPI/craco-less repo:\n\n" +
" * https://github.com/FormAPI/craco-less/issues?q=is%3Aissue+webpack+file-loader\n\n" +
"If not, please open an issue and we'll take a look. (Or you can send a PR!)\n\n" +
"You might also want to look for related issues in the " +
"craco and create-react-app repos:\n\n" +
" * https://github.com/sharegate/craco/issues?q=is%3Aissue+webpack+file-loader\n" +
" * https://github.com/facebook/create-react-app/issues?q=is%3Aissue+webpack+file-loader\n"
);
});
test("throws an error when we can't find the oneOf rules in the webpack config", () => {
let oneOfRule = webpackConfig.module.rules.find(r => r.oneOf);
oneOfRule.oneOf = null;
const runTest = () => {
applyCracoConfigAndOverrideWebpack({
plugins: [{ plugin: CracoLessPlugin }]
});
};
expect(runTest).toThrowError(
"Can't find a 'oneOf' rule under module.rules in the development webpack config!\n\n" +
"This error probably occurred because you updated react-scripts or craco. " +
"Please try updating craco-less to the latest version:\n\n" +
" $ yarn upgrade craco-less\n\n" +
"Or:\n\n" +
" $ npm update craco-less\n\n" +
"If that doesn't work, craco-less needs to be fixed to support the latest version.\n" +
"Please check to see if there's already an issue in the FormAPI/craco-less repo:\n\n" +
" * https://github.com/FormAPI/craco-less/issues?q=is%3Aissue+webpack+rules+oneOf\n\n" +
"If not, please open an issue and we'll take a look. (Or you can send a PR!)\n\n" +
"You might also want to look for related issues in the " +
"craco and create-react-app repos:\n\n" +
" * https://github.com/sharegate/craco/issues?q=is%3Aissue+webpack+rules+oneOf\n" +
" * https://github.com/facebook/create-react-app/issues?q=is%3Aissue+webpack+rules+oneOf\n"
);
});
test("throws an error when react-scripts adds an unknown webpack rule", () => {
let oneOfRule = webpackConfig.module.rules.find(r => r.oneOf);
const sassRule = oneOfRule.oneOf.find(rule =>
rule.test.toString().includes("scss|sass")
);
sassRule.use.push({
loader: "/path/to/unknown-loader/index.js"
});
const runTest = () => {
applyCracoConfigAndOverrideWebpack({
plugins: [{ plugin: CracoLessPlugin }]
});
};
expect(runTest).toThrowError(
new RegExp(
"Found an unhandled loader in the development webpack config: " +
"/path/to/unknown-loader/index.js"
)
);
});
test("throws an error when the sass rule is missing", () => {
let oneOfRule = webpackConfig.module.rules.find(r => r.oneOf);
oneOfRule.oneOf = oneOfRule.oneOf.filter(
rule => !(rule.test && rule.test.toString().includes("scss|sass"))
);
const runTest = () => {
applyCracoConfigAndOverrideWebpack({
plugins: [{ plugin: CracoLessPlugin }]
});
};
expect(runTest).toThrowError(
new RegExp(
"Can't find the webpack rule to match scss/sass files in the " +
"development webpack config!"
)
);
});