Skip to content

Commit

Permalink
fix: avoid rebuilt unnecessary files on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Jun 11, 2020
1 parent cc23007 commit 6537a3d
Show file tree
Hide file tree
Showing 9 changed files with 4,013 additions and 856 deletions.
4,779 changes: 3,936 additions & 843 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,31 @@
},
"dependencies": {
"clone": "^2.1.2",
"less": "^3.11.1",
"less": "^3.11.3",
"loader-utils": "^2.0.0",
"schema-utils": "^2.6.6"
"schema-utils": "^2.7.0"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"@babel/cli": "^7.10.1",
"@babel/core": "^7.10.2",
"@babel/preset-env": "^7.10.2",
"@commitlint/cli": "^8.3.5",
"@commitlint/config-conventional": "^8.3.4",
"@webpack-contrib/defaults": "^6.3.0",
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
"babel-jest": "^26.0.1",
"cross-env": "^7.0.2",
"del": "^5.1.0",
"del-cli": "^3.0.0",
"eslint": "^6.8.0",
"del-cli": "^3.0.1",
"eslint": "^7.2.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-import": "^2.21.2",
"eslint-plugin-prettier": "^3.1.3",
"husky": "^4.2.5",
"inspect-loader": "^1.0.0",
"jest": "^26.0.1",
"lint-staged": "^10.2.2",
"memfs": "^3.1.2",
"lint-staged": "^10.2.9",
"memfs": "^3.2.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.5",
"standard-version": "^8.0.0",
Expand Down
6 changes: 5 additions & 1 deletion src/LessError.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from 'path';

class LessError extends Error {
constructor(error) {
super();
Expand All @@ -6,7 +8,9 @@ class LessError extends Error {
'\n',
...LessError.getFileExcerptIfPossible(error),
error.message.charAt(0).toUpperCase() + error.message.slice(1),
` Error in ${error.filename} (line ${error.line}, column ${error.column})`,
` Error in ${path.normalize(error.filename)} (line ${
error.line
}, column ${error.column})`,
].join('\n');

this.hideStack = true;
Expand Down
12 changes: 10 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from 'path';

import { getOptions } from 'loader-utils';
import validateOptions from 'schema-utils';

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

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

callback(new LessError(lessError));
Expand Down
11 changes: 11 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,17 @@ exports[`loader should transform urls: errors 1`] = `Array []`;
exports[`loader should transform urls: warnings 1`] = `Array []`;
exports[`loader should watch imports correctly: css 1`] = `
"a {
color: red;
}
"
`;
exports[`loader should watch imports correctly: errors 1`] = `Array []`;
exports[`loader should watch imports correctly: warnings 1`] = `Array []`;
exports[`loader should work third-party plugins as fileLoader: css 1`] = `
".file-loader {
background: coral;
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/node_modules/package/style.less

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/fixtures/watch.less
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import '~package/style.less';
8 changes: 8 additions & 0 deletions test/helpers/getCodeFromLess.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ const pathMap = {
'fixtures',
'import-absolute-target.less'
),
'~package/style.less': path.resolve(
__dirname,
'..',
'fixtures',
'node_modules',
'package',
'style.less'
),
};

class ResolvePlugin extends less.FileManager {
Expand Down
29 changes: 29 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,33 @@ describe('loader', () => {
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should watch imports correctly', async () => {
const testId = './watch.less';
const compiler = getCompiler(testId);
const stats = await compile(compiler);
const codeFromBundle = getCodeFromBundle(stats, compiler);
const codeFromLess = await getCodeFromLess(testId);
const { fileDependencies } = stats.compilation;

const fixtures = [
path.resolve(__dirname, 'fixtures', 'watch.less'),
path.resolve(
__dirname,
'fixtures',
'node_modules',
'package',
'style.less'
),
];

fixtures.forEach((fixture) => {
expect(fileDependencies.has(fixture)).toBe(true);
});

expect(codeFromBundle.css).toBe(codeFromLess.css);
expect(codeFromBundle.css).toMatchSnapshot('css');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
expect(getErrors(stats)).toMatchSnapshot('errors');
});
});

0 comments on commit 6537a3d

Please sign in to comment.