Skip to content

Commit 443bd5a

Browse files
refactor: first resolve filename in less, then in webpack (#340)
BREAKING CHANGE: some resolutions can be changed
1 parent 82cb6a7 commit 443bd5a

File tree

3 files changed

+35
-57
lines changed

3 files changed

+35
-57
lines changed

src/createWebpackLessPlugin.js

+32-45
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1-
import { promisify } from 'util';
2-
31
import less from 'less';
42

53
import { urlToRequest } from 'loader-utils';
64

75
/* eslint-disable class-methods-use-this */
8-
9-
const stringifyLoader = require.resolve('./stringifyLoader.js');
106
const trailingSlash = /[/\\]$/;
11-
const isLessCompatible = /\.(le|c)ss$/;
127

138
// This somewhat changed in Less 3.x. Now the file name comes without the
149
// automatically added extension whereas the extension is passed in as `options.ext`.
@@ -23,11 +18,6 @@ const isModuleName = /^~[^/\\]+$/;
2318
* @returns {LessPlugin}
2419
*/
2520
function createWebpackLessPlugin(loaderContext) {
26-
const { fs } = loaderContext;
27-
28-
const loadModule = promisify(loaderContext.loadModule.bind(loaderContext));
29-
const readFile = promisify(fs.readFile.bind(fs));
30-
3121
const resolve = loaderContext.getResolve({
3222
mainFields: ['less', 'style', 'main', '...'],
3323
mainFiles: ['_index', 'index', '...'],
@@ -40,7 +30,6 @@ function createWebpackLessPlugin(loaderContext) {
4030
return false;
4131
}
4232

43-
// Our WebpackFileManager handles all the files
4433
return true;
4534
}
4635

@@ -61,6 +50,20 @@ function createWebpackLessPlugin(loaderContext) {
6150
return filename;
6251
}
6352

53+
async resolveFilename(filename, currentDirectory, options) {
54+
const url = this.getUrl(filename, options);
55+
56+
const moduleRequest = urlToRequest(
57+
url,
58+
url.charAt(0) === '/' ? '' : null
59+
);
60+
61+
// Less is giving us trailing slashes, but the context should have no trailing slash
62+
const context = currentDirectory.replace(trailingSlash, '');
63+
64+
return this.resolveRequests(context, [moduleRequest, url]);
65+
}
66+
6467
resolveRequests(context, possibleRequests) {
6568
if (possibleRequests.length === 0) {
6669
return Promise.reject();
@@ -81,50 +84,34 @@ function createWebpackLessPlugin(loaderContext) {
8184
});
8285
}
8386

84-
async loadFile(filename, currentDirectory, options, ...args) {
85-
const url = this.getUrl(filename, options);
86-
87-
const moduleRequest = urlToRequest(
88-
url,
89-
url.charAt(0) === '/' ? '' : null
90-
);
91-
92-
// Less is giving us trailing slashes, but the context should have no trailing slash
93-
const context = currentDirectory.replace(trailingSlash, '');
94-
let resolvedFilename;
87+
async loadFile(filename, ...args) {
88+
let result;
9589

9690
try {
97-
resolvedFilename = await this.resolveRequests(context, [
98-
moduleRequest,
99-
url,
100-
]);
91+
result = await super.loadFile(filename, ...args);
10192
} catch (error) {
102-
loaderContext.emitError(error);
93+
if (error.type !== 'File') {
94+
loaderContext.emitError(error);
10395

104-
return super.loadFile(filename, currentDirectory, options, ...args);
105-
}
96+
return Promise.reject(error);
97+
}
98+
99+
try {
100+
result = await this.resolveFilename(filename, ...args);
101+
} catch (e) {
102+
loaderContext.emitError(e);
106103

107-
loaderContext.addDependency(resolvedFilename);
104+
return Promise.reject(error);
105+
}
108106

109-
if (isLessCompatible.test(resolvedFilename)) {
110-
const fileBuffer = await readFile(resolvedFilename);
111-
const contents = fileBuffer.toString('utf8');
107+
loaderContext.addDependency(result);
112108

113-
return {
114-
contents,
115-
filename: resolvedFilename,
116-
};
109+
return super.loadFile(result, ...args);
117110
}
118111

119-
const loadedModule = await loadModule(
120-
[stringifyLoader, resolvedFilename].join('!')
121-
);
122-
const contents = JSON.parse(loadedModule);
112+
loaderContext.addDependency(result.filename);
123113

124-
return {
125-
contents,
126-
filename: resolvedFilename,
127-
};
114+
return result;
128115
}
129116
}
130117

src/stringifyLoader.js

-12
This file was deleted.

test/fixtures/less/folder/some.file

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.some-file {
2+
background: hotpink;
3+
}

0 commit comments

Comments
 (0)