Skip to content

Commit

Permalink
fix: resolve @import with absolute paths (#201)
Browse files Browse the repository at this point in the history
* - Fixes #93

* - Adds test for absolute import
  • Loading branch information
n1ru4l authored and joshwiens committed May 30, 2017
1 parent 9ed895f commit a3f9601
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/createWebpackLessPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function createWebpackLessPlugin(loaderContext) {

loadFile(filename, currentDirectory /* , options, environment */) { // eslint-disable-line class-methods-use-this
const url = filename.replace(matchMalformedModuleFilename, '$1');
const moduleRequest = loaderUtils.urlToRequest(url);
const moduleRequest = loaderUtils.urlToRequest(url, url.charAt(0) === '/' ? '' : null);
// Less is giving us trailing slashes, but the context should have no trailing slash
const context = currentDirectory.replace(trailingSlash, '');
let resolvedFilename;
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/less/import-absolute-target.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.it-works {
color: yellow;
}
1 change: 1 addition & 0 deletions test/fixtures/less/import-absolute.less
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "@{absolutePath}";
2 changes: 2 additions & 0 deletions test/helpers/createSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const ignore = [
'error-mixed-resolvers',
'error-syntax',
'error-import-file-with-error',
'import-absolute',
'import-absolute-target',
];
const lessReplacements = [
[/~some\//g, '../node_modules/some/'],
Expand Down
17 changes: 17 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,20 @@ test('should add a file with an error as dependency so that the watcher is trigg
lessFixturePath('error-syntax.less'),
].sort());
});

test('should be able to import a file with an absolute path', async () => {
const importedFilePath = path.resolve(__dirname, 'fixtures', 'less', 'import-absolute-target.less');
const loaderOptions = {
globalVars: {
absolutePath: `'${importedFilePath}'`,
},
};
let inspect;
const rules = moduleRules.basic(loaderOptions, {}, (i) => {
inspect = i;
});
await compile('import-absolute', rules)
.catch(e => e);
const [css] = inspect.arguments;
expect(css).toEqual('.it-works {\n color: yellow;\n}\n');
});

0 comments on commit a3f9601

Please sign in to comment.