Skip to content

Commit 8e020e9

Browse files
fix: import files hosted remotely (#333)
1 parent cfeccd5 commit 8e020e9

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

src/createWebpackLessPlugin.js

+9-17
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@ const stringifyLoader = require.resolve('./stringifyLoader.js');
1010
const trailingSlash = /[/\\]$/;
1111
const isLessCompatible = /\.(le|c)ss$/;
1212

13-
// Less automatically adds a .less file extension if no extension was given.
14-
// This is problematic if there is a module request like @import "~some-module";
15-
// because in this case Less will call our file manager with `~some-module.less`.
16-
// Since dots in module names are highly discouraged, we can safely assume that
17-
// this is an error and we need to remove the .less extension again.
18-
// However, we must not match something like @import "~some-module/file.less";
19-
const matchMalformedModuleFilename = /(~[^/\\]+)\.less$/;
20-
2113
// This somewhat changed in Less 3.x. Now the file name comes without the
2214
// automatically added extension whereas the extension is passed in as `options.ext`.
2315
// So, if the file name matches this regexp, we simply ignore the proposed extension.
@@ -43,7 +35,11 @@ function createWebpackLessPlugin(loaderContext) {
4335
});
4436

4537
class WebpackFileManager extends less.FileManager {
46-
supports() {
38+
supports(filename) {
39+
if (this.isPathAbsolute(filename)) {
40+
return false;
41+
}
42+
4743
// Our WebpackFileManager handles all the files
4844
return true;
4945
}
@@ -58,15 +54,11 @@ function createWebpackLessPlugin(loaderContext) {
5854
}
5955

6056
getUrl(filename, options) {
61-
if (less.version[0] >= 3) {
62-
if (options.ext && !isModuleName.test(filename)) {
63-
return this.tryAppendExtension(filename, options.ext);
64-
}
65-
66-
return filename;
57+
if (options.ext && !isModuleName.test(filename)) {
58+
return this.tryAppendExtension(filename, options.ext);
6759
}
6860

69-
return filename.replace(matchMalformedModuleFilename, '$1');
61+
return filename;
7062
}
7163

7264
async loadFile(filename, currentDirectory, options) {
@@ -109,7 +101,7 @@ function createWebpackLessPlugin(loaderContext) {
109101
install(lessInstance, pluginManager) {
110102
pluginManager.addFileManager(new WebpackFileManager());
111103
},
112-
minVersion: [2, 1, 1],
104+
minVersion: [3, 0, 0],
113105
};
114106
}
115107

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@import (inline) url("https://fonts.googleapis.com/css?family=Roboto:300");
2+
@import (less) url("https://fonts.googleapis.com/css?family=Roboto:400");
3+
@import (once) url("https://fonts.googleapis.com/css?family=Roboto:500");
4+
@import (multiple) url("https://fonts.googleapis.com/css?family=Roboto:700");
5+
6+
// This url not allowed (should be ignored)
7+
@import (optional) url("https://fonts.googleapis.com/css?family=Roboto:600");

test/index.test.js

+4
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,10 @@ test('should not try to resolve CSS imports with URLs', async () => {
206206
await compileAndCompare('import-url');
207207
});
208208

209+
test('should delegate resolving (LESS) imports with URLs to "less" package', async () => {
210+
await compileAndCompare('import-keyword-url');
211+
});
212+
209213
test('should allow to import non-less files', async () => {
210214
let inspect;
211215
const rules = moduleRules.nonLessImport((i) => {

0 commit comments

Comments
 (0)