1
- const path = require ( "path" ) ;
2
1
const { deepClone, styleRuleByName } = require ( "./utils" ) ;
3
2
const { throwUnexpectedConfigError } = require ( "@craco/craco" ) ;
4
3
5
4
const lessRegex = / \. l e s s $ / ;
6
5
const lessModuleRegex = / \. m o d u l e \. l e s s $ / ;
7
6
7
+ const loaderRegexMap = {
8
+ "style-loader" : / [ \\ / ] s t y l e - l o a d e r [ \\ / ] / ,
9
+ "css-loader" : / [ \\ / ] c s s - l o a d e r [ \\ / ] / ,
10
+ "postcss-loader" : / [ \\ / ] p o s t c s s - l o a d e r [ \\ / ] / ,
11
+ "resolve-url-loader" : / [ \\ / ] r e s o l v e - u r l - l o a d e r [ \\ / ] / ,
12
+ "mini-css-extract-plugin" : / [ \\ / ] m i n i - c s s - e x t r a c t - p l u g i n [ \\ / ] / ,
13
+ "sass-loader" : / [ \\ / ] s a s s - l o a d e r [ \\ / ] / ,
14
+ } ;
15
+
16
+ const hasLoader = ( loaderName , ruleLoader ) =>
17
+ loaderRegexMap [ loaderName ] . test ( ruleLoader ) ;
18
+
8
19
const throwError = ( message , githubIssueQuery ) =>
9
20
throwUnexpectedConfigError ( {
10
21
packageName : "craco-less" ,
@@ -14,8 +25,6 @@ const throwError = (message, githubIssueQuery) =>
14
25
} ) ;
15
26
16
27
const overrideWebpackConfig = ( { context, webpackConfig, pluginOptions } ) => {
17
- // This is mocked in Windows tests
18
- const pathSep = module . exports . pathSep ;
19
28
pluginOptions = pluginOptions || { } ;
20
29
21
30
const createLessRule = ( { baseRule, overrideRule } ) => {
@@ -40,7 +49,7 @@ const overrideWebpackConfig = ({ context, webpackConfig, pluginOptions }) => {
40
49
41
50
if (
42
51
( context . env === "development" || context . env === "test" ) &&
43
- rule . loader . includes ( ` ${ pathSep } style-loader${ pathSep } ` )
52
+ hasLoader ( " style-loader" , rule . loader )
44
53
) {
45
54
lessRule . use . push ( {
46
55
loader : rule . loader ,
@@ -49,15 +58,15 @@ const overrideWebpackConfig = ({ context, webpackConfig, pluginOptions }) => {
49
58
...( pluginOptions . styleLoaderOptions || { } ) ,
50
59
} ,
51
60
} ) ;
52
- } else if ( rule . loader . includes ( ` ${ pathSep } css-loader${ pathSep } ` ) ) {
61
+ } else if ( hasLoader ( " css-loader" , rule . loader ) ) {
53
62
lessRule . use . push ( {
54
63
loader : rule . loader ,
55
64
options : {
56
65
...rule . options ,
57
66
...( pluginOptions . cssLoaderOptions || { } ) ,
58
67
} ,
59
68
} ) ;
60
- } else if ( rule . loader . includes ( ` ${ pathSep } postcss-loader${ pathSep } ` ) ) {
69
+ } else if ( hasLoader ( " postcss-loader" , rule . loader ) ) {
61
70
lessRule . use . push ( {
62
71
loader : rule . loader ,
63
72
options : {
@@ -68,9 +77,7 @@ const overrideWebpackConfig = ({ context, webpackConfig, pluginOptions }) => {
68
77
} ,
69
78
} ,
70
79
} ) ;
71
- } else if (
72
- rule . loader . includes ( `${ pathSep } resolve-url-loader${ pathSep } ` )
73
- ) {
80
+ } else if ( hasLoader ( "resolve-url-loader" , rule . loader ) ) {
74
81
lessRule . use . push ( {
75
82
loader : rule . loader ,
76
83
options : {
@@ -80,7 +87,7 @@ const overrideWebpackConfig = ({ context, webpackConfig, pluginOptions }) => {
80
87
} ) ;
81
88
} else if (
82
89
context . env === "production" &&
83
- rule . loader . includes ( ` ${ pathSep } mini-css-extract-plugin${ pathSep } ` )
90
+ hasLoader ( " mini-css-extract-plugin" , rule . loader )
84
91
) {
85
92
lessRule . use . push ( {
86
93
loader : rule . loader ,
@@ -89,7 +96,7 @@ const overrideWebpackConfig = ({ context, webpackConfig, pluginOptions }) => {
89
96
...( pluginOptions . miniCssExtractPluginOptions || { } ) ,
90
97
} ,
91
98
} ) ;
92
- } else if ( rule . loader . includes ( ` ${ pathSep } sass-loader${ pathSep } ` ) ) {
99
+ } else if ( hasLoader ( " sass-loader" , rule . loader ) ) {
93
100
lessRule . use . push ( {
94
101
loader : require . resolve ( "less-loader" ) ,
95
102
options : {
@@ -132,7 +139,6 @@ const overrideWebpackConfig = ({ context, webpackConfig, pluginOptions }) => {
132
139
exclude : lessModuleRegex ,
133
140
} ,
134
141
pluginOptions,
135
- pathSep,
136
142
} ) ;
137
143
138
144
if ( pluginOptions . modifyLessRule ) {
@@ -208,9 +214,7 @@ const overrideJestConfig = ({ context, jestConfig }) => {
208
214
return jestConfig ;
209
215
} ;
210
216
211
- // pathSep is mocked in Windows tests
212
217
module . exports = {
213
218
overrideWebpackConfig,
214
219
overrideJestConfig,
215
- pathSep : path . sep ,
216
220
} ;
0 commit comments