Skip to content

Commit a3f9601

Browse files
n1ru4ljoshwiens
authored andcommitted
fix: resolve @import with absolute paths (#201)
* - Fixes #93 * - Adds test for absolute import
1 parent 9ed895f commit a3f9601

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

src/createWebpackLessPlugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function createWebpackLessPlugin(loaderContext) {
3434

3535
loadFile(filename, currentDirectory /* , options, environment */) { // eslint-disable-line class-methods-use-this
3636
const url = filename.replace(matchMalformedModuleFilename, '$1');
37-
const moduleRequest = loaderUtils.urlToRequest(url);
37+
const moduleRequest = loaderUtils.urlToRequest(url, url.charAt(0) === '/' ? '' : null);
3838
// Less is giving us trailing slashes, but the context should have no trailing slash
3939
const context = currentDirectory.replace(trailingSlash, '');
4040
let resolvedFilename;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.it-works {
2+
color: yellow;
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "@{absolutePath}";

test/helpers/createSpec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const ignore = [
1414
'error-mixed-resolvers',
1515
'error-syntax',
1616
'error-import-file-with-error',
17+
'import-absolute',
18+
'import-absolute-target',
1719
];
1820
const lessReplacements = [
1921
[/~some\//g, '../node_modules/some/'],

test/index.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,20 @@ test('should add a file with an error as dependency so that the watcher is trigg
268268
lessFixturePath('error-syntax.less'),
269269
].sort());
270270
});
271+
272+
test('should be able to import a file with an absolute path', async () => {
273+
const importedFilePath = path.resolve(__dirname, 'fixtures', 'less', 'import-absolute-target.less');
274+
const loaderOptions = {
275+
globalVars: {
276+
absolutePath: `'${importedFilePath}'`,
277+
},
278+
};
279+
let inspect;
280+
const rules = moduleRules.basic(loaderOptions, {}, (i) => {
281+
inspect = i;
282+
});
283+
await compile('import-absolute', rules)
284+
.catch(e => e);
285+
const [css] = inspect.arguments;
286+
expect(css).toEqual('.it-works {\n color: yellow;\n}\n');
287+
});

0 commit comments

Comments
 (0)