Skip to content

Commit 24021cd

Browse files
fix: import alias without tilde (#335)
1 parent 4604f20 commit 24021cd

File tree

9 files changed

+133
-15
lines changed

9 files changed

+133
-15
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"npm-run-all": "^4.1.5",
7676
"prettier": "^2.0.4",
7777
"standard-version": "^7.0.1",
78+
"strip-ansi": "^6.0.0",
7879
"webpack": "^4.42.1"
7980
},
8081
"keywords": [

src/createWebpackLessPlugin.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function createWebpackLessPlugin(loaderContext) {
3131
const resolve = loaderContext.getResolve({
3232
mainFields: ['less', 'style', 'main', '...'],
3333
mainFiles: ['_index', 'index', '...'],
34-
extensions: ['.less', '.css', '...'],
34+
extensions: ['.less', '.css'],
3535
});
3636

3737
class WebpackFileManager extends less.FileManager {
@@ -61,7 +61,27 @@ function createWebpackLessPlugin(loaderContext) {
6161
return filename;
6262
}
6363

64-
async loadFile(filename, currentDirectory, options) {
64+
resolveRequests(context, possibleRequests) {
65+
if (possibleRequests.length === 0) {
66+
return Promise.reject();
67+
}
68+
69+
return resolve(context, possibleRequests[0])
70+
.then((result) => {
71+
return result;
72+
})
73+
.catch((error) => {
74+
const [, ...tailPossibleRequests] = possibleRequests;
75+
76+
if (tailPossibleRequests.length === 0) {
77+
throw error;
78+
}
79+
80+
return this.resolveRequests(context, tailPossibleRequests);
81+
});
82+
}
83+
84+
async loadFile(filename, currentDirectory, options, ...args) {
6585
const url = this.getUrl(filename, options);
6686

6787
const moduleRequest = urlToRequest(
@@ -71,7 +91,18 @@ function createWebpackLessPlugin(loaderContext) {
7191

7292
// Less is giving us trailing slashes, but the context should have no trailing slash
7393
const context = currentDirectory.replace(trailingSlash, '');
74-
const resolvedFilename = await resolve(context, moduleRequest);
94+
let resolvedFilename;
95+
96+
try {
97+
resolvedFilename = await this.resolveRequests(context, [
98+
moduleRequest,
99+
url,
100+
]);
101+
} catch (error) {
102+
loaderContext.emitError(error);
103+
104+
return super.loadFile(filename, currentDirectory, options, ...args);
105+
}
75106

76107
loaderContext.addDependency(resolvedFilename);
77108

test/__snapshots__/index.test.js.snap

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ exports[`should fail if a file is tried to be loaded from include paths and with
1111
in /test/fixtures/less/error-mixed-resolvers.less (line 3, column 0)"
1212
`;
1313

14-
exports[`should provide a useful error message if the import could not be found 1`] = `
15-
"Module build failed (from ./src/cjs.js):
14+
exports[`should provide a useful error message if the import could not be found: errors 1`] = `
15+
Array [
16+
"ModuleBuildError: Module build failed (from \`replaced original path\`):
1617
1718
1819
@import \\"not-existing\\";
1920
^
20-
Can't resolve './not-existing.less' in '/test/fixtures/less'
21-
in /test/fixtures/less/error-import-not-existing.less (line 1, column 0)"
21+
'not-existing' wasn't found. Tried - /test/fixtures/less/not-existing.less,npm://not-existing,npm://not-existing.less,not-existing.less
22+
in /test/fixtures/less/error-import-not-existing.less (line 1, column 0)",
23+
"ModuleError: Module Error (from \`replaced original path\`):
24+
Can't resolve 'not-existing.less' in '/test/fixtures/less'",
25+
]
2226
`;
2327

2428
exports[`should provide a useful error message if there was a syntax error 1`] = `
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@import "~fileAlias";
2+
@import "~assets/folder/url-path.less";
3+
@import "assets/folder/url-path.less";
4+
5+
body {
6+
background: url(assets/resources/circle.svg);
7+
}
8+
9+
.abs {
10+
background: url(~assets/resources/circle.svg);
11+
}

test/helpers/createSpec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const ignore = [
2121
];
2222
const lessReplacements = [
2323
[/~some\//g, '../node_modules/some/'],
24+
[/(~)?assets\//g, '../less/'],
25+
[/~fileAlias/g, '../less/img.less'],
2426
[/~(aliased-)?some"/g, '../node_modules/some/module.less"'],
2527
];
2628
const cssReplacements = [[/\.\.\/node_modules\/some\//g, '~some/']];

test/helpers/getErrors.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import normalizeErrors from './normalizeErrors';
2+
3+
export default (stats) => {
4+
return normalizeErrors(stats.compilation.errors).sort();
5+
};

test/helpers/normalizeErrors.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// eslint-disable-next-line import/no-extraneous-dependencies
2+
import stripAnsi from 'strip-ansi';
3+
4+
function removeCWD(str) {
5+
const isWin = process.platform === 'win32';
6+
let cwd = process.cwd();
7+
8+
if (isWin) {
9+
// eslint-disable-next-line no-param-reassign
10+
str = str.replace(/\\/g, '/');
11+
// eslint-disable-next-line no-param-reassign
12+
cwd = cwd.replace(/\\/g, '/');
13+
}
14+
15+
return stripAnsi(str)
16+
.replace(/\(from .*?\)/, '(from `replaced original path`)')
17+
.replace(new RegExp(cwd, 'g'), '');
18+
}
19+
20+
export default (errors) => {
21+
return errors.map((error) =>
22+
removeCWD(error.toString().split('\n').slice(0, 12).join('\n'))
23+
);
24+
};

test/index.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import compile from './helpers/compile';
44
import moduleRules from './helpers/moduleRules';
55
import { readCssFixture, readSourceMap } from './helpers/readFixture';
66
import compareErrorMessage from './helpers/compareErrorMessage';
7+
import getErrors from './helpers/getErrors';
78

89
const nodeModulesPath = path.resolve(__dirname, 'fixtures', 'node_modules');
910

@@ -88,10 +89,12 @@ test('should add all resolved imports as dependencies, including node_modules',
8889
);
8990
});
9091

91-
test('should resolve aliases as configured', async () => {
92+
test('should resolve aliases in diffrent variants', async () => {
9293
await compileAndCompare('import-webpack-alias', {
9394
resolveAlias: {
9495
'aliased-some': 'some',
96+
fileAlias: path.resolve(__dirname, 'fixtures', 'less', 'img.less'),
97+
assets: path.resolve(__dirname, 'fixtures', 'less'),
9598
},
9699
});
97100
});
@@ -300,9 +303,7 @@ test('should provide a useful error message if the import could not be found', a
300303
moduleRules.basic()
301304
).catch((e) => e);
302305

303-
expect(err).toBeInstanceOf(Error);
304-
305-
compareErrorMessage(err.message);
306+
expect(getErrors(err.stats)).toMatchSnapshot('errors');
306307
});
307308

308309
test('should provide a useful error message if there was a syntax error', async () => {

0 commit comments

Comments
 (0)