Skip to content

Commit 7c22523

Browse files
Fix/#98 find loaders path separator agnostically (#99)
Find Webpack CSS loaders using platform-independent path-separator logic. --------- Co-authored-by: Kamron Batman <3953314+kamronbatman@users.noreply.github.com>
1 parent 4b0343c commit 7c22523

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,4 @@ Before submitting a pull request, please check the following:
278278
- [fanck0605](https://github.com/fanck0605)
279279
- [xyy94813](https://github.com/xyy94813)
280280
- [kamronbatman](https://github.com/kamronbatman)
281+
- [fourpastmidnight](https://github.com/fourpastmidnight)

lib/craco-less.dev.test.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ beforeEach(() => {
2323
});
2424
process.env.NODE_ENV = "test";
2525
}
26-
CracoLessPlugin.pathSep = path.sep;
2726

2827
// loadWebpackDevConfig() caches the config internally, so we need to
2928
// deep clone the object before each test.
@@ -110,8 +109,6 @@ test("the webpack config is modified correctly without any options", () => {
110109
});
111110

112111
test("the webpack config is modified correctly without any options on Windows", () => {
113-
CracoLessPlugin.pathSep = "\\";
114-
115112
// Windows uses "\" path separators.
116113
// Note: This is a noop when running tests on Windows.
117114
const replaceSlashesInLoader = (rule) => {

lib/craco-less.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1-
const path = require("path");
21
const { deepClone, styleRuleByName } = require("./utils");
32
const { throwUnexpectedConfigError } = require("@craco/craco");
43

54
const lessRegex = /\.less$/;
65
const lessModuleRegex = /\.module\.less$/;
76

7+
const loaderRegexMap = {
8+
"style-loader": /[\\/]style-loader[\\/]/,
9+
"css-loader": /[\\/]css-loader[\\/]/,
10+
"postcss-loader": /[\\/]postcss-loader[\\/]/,
11+
"resolve-url-loader": /[\\/]resolve-url-loader[\\/]/,
12+
"mini-css-extract-plugin": /[\\/]mini-css-extract-plugin[\\/]/,
13+
"sass-loader": /[\\/]sass-loader[\\/]/,
14+
};
15+
16+
const hasLoader = (loaderName, ruleLoader) =>
17+
loaderRegexMap[loaderName].test(ruleLoader);
18+
819
const throwError = (message, githubIssueQuery) =>
920
throwUnexpectedConfigError({
1021
packageName: "craco-less",
@@ -14,8 +25,6 @@ const throwError = (message, githubIssueQuery) =>
1425
});
1526

1627
const overrideWebpackConfig = ({ context, webpackConfig, pluginOptions }) => {
17-
// This is mocked in Windows tests
18-
const pathSep = module.exports.pathSep;
1928
pluginOptions = pluginOptions || {};
2029

2130
const createLessRule = ({ baseRule, overrideRule }) => {
@@ -40,7 +49,7 @@ const overrideWebpackConfig = ({ context, webpackConfig, pluginOptions }) => {
4049

4150
if (
4251
(context.env === "development" || context.env === "test") &&
43-
rule.loader.includes(`${pathSep}style-loader${pathSep}`)
52+
hasLoader("style-loader", rule.loader)
4453
) {
4554
lessRule.use.push({
4655
loader: rule.loader,
@@ -49,15 +58,15 @@ const overrideWebpackConfig = ({ context, webpackConfig, pluginOptions }) => {
4958
...(pluginOptions.styleLoaderOptions || {}),
5059
},
5160
});
52-
} else if (rule.loader.includes(`${pathSep}css-loader${pathSep}`)) {
61+
} else if (hasLoader("css-loader", rule.loader)) {
5362
lessRule.use.push({
5463
loader: rule.loader,
5564
options: {
5665
...rule.options,
5766
...(pluginOptions.cssLoaderOptions || {}),
5867
},
5968
});
60-
} else if (rule.loader.includes(`${pathSep}postcss-loader${pathSep}`)) {
69+
} else if (hasLoader("postcss-loader", rule.loader)) {
6170
lessRule.use.push({
6271
loader: rule.loader,
6372
options: {
@@ -68,9 +77,7 @@ const overrideWebpackConfig = ({ context, webpackConfig, pluginOptions }) => {
6877
},
6978
},
7079
});
71-
} else if (
72-
rule.loader.includes(`${pathSep}resolve-url-loader${pathSep}`)
73-
) {
80+
} else if (hasLoader("resolve-url-loader", rule.loader)) {
7481
lessRule.use.push({
7582
loader: rule.loader,
7683
options: {
@@ -80,7 +87,7 @@ const overrideWebpackConfig = ({ context, webpackConfig, pluginOptions }) => {
8087
});
8188
} else if (
8289
context.env === "production" &&
83-
rule.loader.includes(`${pathSep}mini-css-extract-plugin${pathSep}`)
90+
hasLoader("mini-css-extract-plugin", rule.loader)
8491
) {
8592
lessRule.use.push({
8693
loader: rule.loader,
@@ -89,7 +96,7 @@ const overrideWebpackConfig = ({ context, webpackConfig, pluginOptions }) => {
8996
...(pluginOptions.miniCssExtractPluginOptions || {}),
9097
},
9198
});
92-
} else if (rule.loader.includes(`${pathSep}sass-loader${pathSep}`)) {
99+
} else if (hasLoader("sass-loader", rule.loader)) {
93100
lessRule.use.push({
94101
loader: require.resolve("less-loader"),
95102
options: {
@@ -132,7 +139,6 @@ const overrideWebpackConfig = ({ context, webpackConfig, pluginOptions }) => {
132139
exclude: lessModuleRegex,
133140
},
134141
pluginOptions,
135-
pathSep,
136142
});
137143

138144
if (pluginOptions.modifyLessRule) {
@@ -208,9 +214,7 @@ const overrideJestConfig = ({ context, jestConfig }) => {
208214
return jestConfig;
209215
};
210216

211-
// pathSep is mocked in Windows tests
212217
module.exports = {
213218
overrideWebpackConfig,
214219
overrideJestConfig,
215-
pathSep: path.sep,
216220
};

lib/craco-less.prod.test.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ beforeEach(() => {
2626
});
2727
process.env.NODE_ENV = "test";
2828
}
29-
CracoLessPlugin.pathSep = path.sep;
3029

3130
// loadWebpackProdConfig() caches the config internally, so we need to
3231
// deep clone the object before each test.
@@ -121,8 +120,6 @@ test("the webpack config is modified correctly without any options", () => {
121120
});
122121

123122
test("the webpack config is modified correctly without any options on Windows", () => {
124-
CracoLessPlugin.pathSep = "\\";
125-
126123
// Windows uses "\" path separators.
127124
// Note: This is a noop when running tests on Windows.
128125
const replaceSlashesInLoader = (rule) => {

0 commit comments

Comments
 (0)