Skip to content

Commit

Permalink
Output sourceMap by default on less-loader
Browse files Browse the repository at this point in the history
We have checked if sourceMaps are needed on resolve-url-loader, and according to the source code of react-scripts, all preProcessor's options must be the same({sourceMap:true}), so directly expand sass-loader's options maybe a better choice.

facebook/create-react-app@914c95e

https://github.com/facebook/create-react-app/blob/v4.0.3/packages/react-scripts/config/webpack.config.js#L163

Signed-off-by: Chuck Fan <[email protected]>
  • Loading branch information
fanck0605 committed Aug 27, 2021
1 parent a73322c commit 9a5a7c0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
16 changes: 10 additions & 6 deletions lib/craco-less.dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ test("the webpack config is modified correctly without any options", () => {
});

expect(lessRule.use[4].loader).toContain(`${path.sep}less-loader`);
expect(lessRule.use[4].options).toEqual({});
expect(lessRule.use[4].options).toEqual({ sourceMap: true });

const lessModuleRule = oneOfRule.oneOf.find(
(r) => r.test && r.test.toString() === "/\\.module\\.less$/"
Expand Down Expand Up @@ -99,7 +99,7 @@ test("the webpack config is modified correctly without any options", () => {
});

expect(lessModuleRule.use[4].loader).toContain(`${path.sep}less-loader`);
expect(lessModuleRule.use[4].options).toEqual({});
expect(lessModuleRule.use[4].options).toEqual({ sourceMap: true });
});

test("the webpack config is modified correctly without any options on Windows", () => {
Expand Down Expand Up @@ -158,7 +158,7 @@ test("the webpack config is modified correctly without any options on Windows",

// 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({});
expect(lessRule.use[4].options).toEqual({ sourceMap: true });

const lessModuleRule = oneOfRule.oneOf.find(
(r) => r.test && r.test.toString() === "/\\.module\\.less$/"
Expand Down Expand Up @@ -188,7 +188,7 @@ test("the webpack config is modified correctly without any options on Windows",

// We use `require.resolve("less-loader")`, so it's the OS separator here
expect(lessModuleRule.use[4].loader).toContain(`${path.sep}less-loader`);
expect(lessModuleRule.use[4].options).toEqual({});
expect(lessModuleRule.use[4].options).toEqual({ sourceMap: true });
});

test("the webpack config is modified correctly with less-loader options", () => {
Expand Down Expand Up @@ -227,6 +227,7 @@ test("the webpack config is modified correctly with less-loader options", () =>

expect(lessRule.use[4].loader).toContain(`${path.sep}less-loader`);
expect(lessRule.use[4].options).toEqual({
sourceMap: true,
javascriptEnabled: true,
modifyVars: {
"@less-variable": "#fff",
Expand All @@ -252,6 +253,7 @@ test("the webpack config is modified correctly with less-loader options", () =>

expect(lessModuleRule.use[4].loader).toContain(`${path.sep}less-loader`);
expect(lessModuleRule.use[4].options).toEqual({
sourceMap: true,
javascriptEnabled: true,
modifyVars: {
"@less-variable": "#fff",
Expand Down Expand Up @@ -320,6 +322,7 @@ test("the webpack config is modified correctly with all loader options", () => {

expect(lessRule.use[4].loader).toContain(`${path.sep}less-loader`);
expect(lessRule.use[4].options).toEqual({
sourceMap: true,
javascriptEnabled: true,
modifyVars: {
"@less-variable": "#fff",
Expand Down Expand Up @@ -357,6 +360,7 @@ test("the webpack config is modified correctly with all loader options", () => {

expect(lessModuleRule.use[4].loader).toContain(`${path.sep}less-loader`);
expect(lessModuleRule.use[4].options).toEqual({
sourceMap: true,
javascriptEnabled: true,
modifyVars: {
"@less-variable": "#fff",
Expand Down Expand Up @@ -404,7 +408,7 @@ test("the webpack config is modified correctly with the modifyLessRule option",
});

expect(lessRule.use[4].loader).toContain(`${path.sep}less-loader`);
expect(lessRule.use[4].options).toEqual({});
expect(lessRule.use[4].options).toEqual({ sourceMap: true });
});

test("the webpack config is modified correctly with the modifyLessModuleRule option", () => {
Expand Down Expand Up @@ -459,7 +463,7 @@ test("the webpack config is modified correctly with the modifyLessModuleRule opt
});

expect(lessModuleRule.use[4].loader).toContain(`${path.sep}less-loader`);
expect(lessModuleRule.use[4].options).toEqual({});
expect(lessModuleRule.use[4].options).toEqual({ sourceMap: true });

const sassModuleRule = oneOfRule.oneOf.find((rule) => {
if (!rule.test) return false;
Expand Down
4 changes: 1 addition & 3 deletions lib/craco-less.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,10 @@ const overrideWebpackConfig = ({ context, webpackConfig, pluginOptions }) => {
},
});
} else if (rule.loader.includes(`${pathSep}sass-loader${pathSep}`)) {
const defaultLessLoaderOptions =
context.env === "production" ? { sourceMap: true } : {};
lessRule.use.push({
loader: require.resolve("less-loader"),
options: {
...defaultLessLoaderOptions,
...rule.options,
...pluginOptions.lessLoaderOptions,
},
});
Expand Down

0 comments on commit 9a5a7c0

Please sign in to comment.