Skip to content

Commit 2bdabf5

Browse files
authored
Merge pull request #169 from webpack-contrib/feat/new-resolver
Feat/new resolver
2 parents 0a3d0af + 45ff5ca commit 2bdabf5

17 files changed

+178
-82
lines changed

src/createWebpackLessPlugin.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ const less = require('less');
22
const loaderUtils = require('loader-utils');
33
const pify = require('pify');
44

5+
const stringifyLoader = require.resolve('./stringifyLoader.js');
56
const trailingSlash = /[\\/]$/;
7+
const isLessCompatible = /\.(le|c)ss$/;
68

79
/**
810
* Creates a Less plugin that uses webpack's resolving engine that is provided by the loaderContext.
@@ -11,9 +13,11 @@ const trailingSlash = /[\\/]$/;
1113
* @param {string=} root
1214
* @returns {LessPlugin}
1315
*/
14-
function createWebpackLessPlugin(loaderContext, root) {
16+
function createWebpackLessPlugin(loaderContext) {
17+
const { fs } = loaderContext;
1518
const resolve = pify(loaderContext.resolve.bind(loaderContext));
1619
const loadModule = pify(loaderContext.loadModule.bind(loaderContext));
20+
const readFile = pify(fs.readFile.bind(fs));
1721

1822
class WebpackFileManager extends less.FileManager {
1923
supports(/* filename, currentDirectory, options, environment */) { // eslint-disable-line class-methods-use-this
@@ -22,7 +26,7 @@ function createWebpackLessPlugin(loaderContext, root) {
2226
}
2327

2428
loadFile(filename, currentDirectory /* , options, environment */) { // eslint-disable-line class-methods-use-this
25-
const moduleRequest = loaderUtils.urlToRequest(filename, root);
29+
const moduleRequest = loaderUtils.urlToRequest(filename);
2630
// Less is giving us trailing slashes, but the context should have no trailing slash
2731
const context = currentDirectory.replace(trailingSlash, '');
2832
let resolvedFilename;
@@ -32,11 +36,17 @@ function createWebpackLessPlugin(loaderContext, root) {
3236
resolvedFilename = f;
3337
loaderContext.addDependency(resolvedFilename);
3438

35-
return loadModule(`-!${__dirname}/stringify.loader.js!${resolvedFilename}`);
39+
if (isLessCompatible.test(resolvedFilename)) {
40+
return readFile(resolvedFilename)
41+
.then(contents => contents.toString('utf8'));
42+
}
43+
44+
return loadModule([stringifyLoader, resolvedFilename].join('!'))
45+
.then(JSON.parse);
3646
})
3747
.then((contents) => {
3848
return {
39-
contents: JSON.parse(contents),
49+
contents,
4050
filename: resolvedFilename,
4151
};
4252
});

src/getOptions.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ function getOptions(loaderContext) {
1818
// We need to set the filename because otherwise our WebpackFileManager will receive an undefined path for the entry
1919
options.filename = loaderContext.resource;
2020

21-
// It's safe to mutate the array now because it has already been cloned
22-
options.plugins.push(createWebpackLessPlugin(loaderContext, options.root));
21+
// When no paths are given, we use the webpack resolver
22+
if ('paths' in options === false) {
23+
// It's safe to mutate the array now because it has already been cloned
24+
options.plugins.push(createWebpackLessPlugin(loaderContext));
25+
}
2326

2427
return options;
2528
}

src/stringify.loader.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/stringifyLoader.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* The stringifyLoader turns any content into a valid JavaScript string. This allows us to load any content
3+
* with loaderContext.loadModule() without webpack complaining about non-JS modules.
4+
*
5+
* @param {string} content
6+
* @return {string}
7+
*/
8+
function stringifyLoader(content) {
9+
return JSON.stringify(content);
10+
}
11+
12+
module.exports = stringifyLoader;
File renamed without changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// You can't use include paths and webpack's resolver simulatenously.
2+
@import "some/module.less";
3+
@import "~some/module.less";

test/fixtures/less/folder/some.file

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "folder/some.file";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// some/module.less is intended to be loaded from node_modules if the folder is configured as include path.
2+
// webpack would expect this import to be prepended with a ~ character.
3+
@import "some/module.less";
File renamed without changes.

0 commit comments

Comments
 (0)