Skip to content

Commit 6537a3d

Browse files
fix: avoid rebuilt unnecessary files on windows
1 parent cc23007 commit 6537a3d

File tree

9 files changed

+4013
-856
lines changed

9 files changed

+4013
-856
lines changed

package-lock.json

Lines changed: 3936 additions & 843 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,31 @@
4242
},
4343
"dependencies": {
4444
"clone": "^2.1.2",
45-
"less": "^3.11.1",
45+
"less": "^3.11.3",
4646
"loader-utils": "^2.0.0",
47-
"schema-utils": "^2.6.6"
47+
"schema-utils": "^2.7.0"
4848
},
4949
"devDependencies": {
50-
"@babel/cli": "^7.8.4",
51-
"@babel/core": "^7.9.6",
52-
"@babel/preset-env": "^7.9.6",
50+
"@babel/cli": "^7.10.1",
51+
"@babel/core": "^7.10.2",
52+
"@babel/preset-env": "^7.10.2",
5353
"@commitlint/cli": "^8.3.5",
5454
"@commitlint/config-conventional": "^8.3.4",
5555
"@webpack-contrib/defaults": "^6.3.0",
5656
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
5757
"babel-jest": "^26.0.1",
5858
"cross-env": "^7.0.2",
5959
"del": "^5.1.0",
60-
"del-cli": "^3.0.0",
61-
"eslint": "^6.8.0",
60+
"del-cli": "^3.0.1",
61+
"eslint": "^7.2.0",
6262
"eslint-config-prettier": "^6.11.0",
63-
"eslint-plugin-import": "^2.20.2",
63+
"eslint-plugin-import": "^2.21.2",
6464
"eslint-plugin-prettier": "^3.1.3",
6565
"husky": "^4.2.5",
6666
"inspect-loader": "^1.0.0",
6767
"jest": "^26.0.1",
68-
"lint-staged": "^10.2.2",
69-
"memfs": "^3.1.2",
68+
"lint-staged": "^10.2.9",
69+
"memfs": "^3.2.0",
7070
"npm-run-all": "^4.1.5",
7171
"prettier": "^2.0.5",
7272
"standard-version": "^8.0.0",

src/LessError.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import path from 'path';
2+
13
class LessError extends Error {
24
constructor(error) {
35
super();
@@ -6,7 +8,9 @@ class LessError extends Error {
68
'\n',
79
...LessError.getFileExcerptIfPossible(error),
810
error.message.charAt(0).toUpperCase() + error.message.slice(1),
9-
` Error in ${error.filename} (line ${error.line}, column ${error.column})`,
11+
` Error in ${path.normalize(error.filename)} (line ${
12+
error.line
13+
}, column ${error.column})`,
1014
].join('\n');
1115

1216
this.hideStack = true;

src/index.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import path from 'path';
2+
13
import { getOptions } from 'loader-utils';
24
import validateOptions from 'schema-utils';
35

@@ -35,13 +37,19 @@ function lessLoader(source) {
3537
getLessImplementation(options.implementation)
3638
.render(data, lessOptions)
3739
.then(({ css, map, imports }) => {
38-
imports.forEach(this.addDependency, this);
40+
imports.forEach((item) => {
41+
// `less` return forward slashes on windows when `webpack` resolver return an absolute windows path in `WebpackFileManager`
42+
// Ref: https://github.com/webpack-contrib/less-loader/issues/357
43+
this.addDependency(path.normalize(item));
44+
});
3945

4046
callback(null, css, typeof map === 'string' ? JSON.parse(map) : map);
4147
})
4248
.catch((lessError) => {
4349
if (lessError.filename) {
44-
this.addDependency(lessError.filename);
50+
// `less` return forward slashes on windows when `webpack` resolver return an absolute windows path in `WebpackFileManager`
51+
// Ref: https://github.com/webpack-contrib/less-loader/issues/357
52+
this.addDependency(path.normalize(lessError.filename));
4553
}
4654

4755
callback(new LessError(lessError));

test/__snapshots__/loader.test.js.snap

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,17 @@ exports[`loader should transform urls: errors 1`] = `Array []`;
323323
324324
exports[`loader should transform urls: warnings 1`] = `Array []`;
325325
326+
exports[`loader should watch imports correctly: css 1`] = `
327+
"a {
328+
color: red;
329+
}
330+
"
331+
`;
332+
333+
exports[`loader should watch imports correctly: errors 1`] = `Array []`;
334+
335+
exports[`loader should watch imports correctly: warnings 1`] = `Array []`;
336+
326337
exports[`loader should work third-party plugins as fileLoader: css 1`] = `
327338
".file-loader {
328339
background: coral;

test/fixtures/node_modules/package/style.less

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/watch.less

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import '~package/style.less';

test/helpers/getCodeFromLess.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ const pathMap = {
6262
'fixtures',
6363
'import-absolute-target.less'
6464
),
65+
'~package/style.less': path.resolve(
66+
__dirname,
67+
'..',
68+
'fixtures',
69+
'node_modules',
70+
'package',
71+
'style.less'
72+
),
6573
};
6674

6775
class ResolvePlugin extends less.FileManager {

test/loader.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,4 +461,33 @@ describe('loader', () => {
461461
expect(getWarnings(stats)).toMatchSnapshot('warnings');
462462
expect(getErrors(stats)).toMatchSnapshot('errors');
463463
});
464+
465+
it('should watch imports correctly', async () => {
466+
const testId = './watch.less';
467+
const compiler = getCompiler(testId);
468+
const stats = await compile(compiler);
469+
const codeFromBundle = getCodeFromBundle(stats, compiler);
470+
const codeFromLess = await getCodeFromLess(testId);
471+
const { fileDependencies } = stats.compilation;
472+
473+
const fixtures = [
474+
path.resolve(__dirname, 'fixtures', 'watch.less'),
475+
path.resolve(
476+
__dirname,
477+
'fixtures',
478+
'node_modules',
479+
'package',
480+
'style.less'
481+
),
482+
];
483+
484+
fixtures.forEach((fixture) => {
485+
expect(fileDependencies.has(fixture)).toBe(true);
486+
});
487+
488+
expect(codeFromBundle.css).toBe(codeFromLess.css);
489+
expect(codeFromBundle.css).toMatchSnapshot('css');
490+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
491+
expect(getErrors(stats)).toMatchSnapshot('errors');
492+
});
464493
});

0 commit comments

Comments
 (0)