Skip to content

Commit 671395d

Browse files
feat: support condition names from package.json (#369)
1 parent d3c5f58 commit 671395d

16 files changed

+1320
-1287
lines changed

package-lock.json

Lines changed: 1134 additions & 1262 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,24 @@
4747
"schema-utils": "^2.7.0"
4848
},
4949
"devDependencies": {
50-
"@babel/cli": "^7.10.3",
51-
"@babel/core": "^7.10.3",
52-
"@babel/preset-env": "^7.10.3",
50+
"@babel/cli": "^7.10.4",
51+
"@babel/core": "^7.10.4",
52+
"@babel/preset-env": "^7.10.4",
5353
"@commitlint/cli": "^9.0.1",
5454
"@commitlint/config-conventional": "^9.0.1",
5555
"@webpack-contrib/defaults": "^6.3.0",
5656
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
57-
"babel-jest": "^26.0.1",
57+
"babel-jest": "^26.1.0",
5858
"cross-env": "^7.0.2",
5959
"del": "^5.1.0",
6060
"del-cli": "^3.0.1",
61-
"eslint": "^7.3.0",
61+
"eslint": "^7.3.1",
6262
"eslint-config-prettier": "^6.11.0",
63-
"eslint-plugin-import": "^2.21.2",
63+
"eslint-plugin-import": "^2.22.0",
6464
"eslint-plugin-prettier": "^3.1.4",
6565
"husky": "^4.2.5",
6666
"inspect-loader": "^1.0.0",
67-
"jest": "^26.0.1",
67+
"jest": "^26.1.0",
6868
"lint-staged": "^10.2.11",
6969
"memfs": "^3.2.0",
7070
"npm-run-all": "^4.1.5",

src/utils.js

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const trailingSlash = /[/\\]$/;
1111
// This somewhat changed in Less 3.x. Now the file name comes without the
1212
// automatically added extension whereas the extension is passed in as `options.ext`.
1313
// So, if the file name matches this regexp, we simply ignore the proposed extension.
14-
const isModuleName = /^~([^/]+|[^/]+\/|@[^/]+[/][^/]+|@[^/]+\/?|@[^/]+[/][^/]+\/)$/;
14+
const isModuleImport = /^~([^/]+|[^/]+\/|@[^/]+[/][^/]+|@[^/]+\/?|@[^/]+[/][^/]+\/)$/;
1515

1616
// `[drive_letter]:\` + `\\[server]\[sharename]\`
1717
const isNativeWin32Path = /^[a-zA-Z]:[/\\]|^\\\\/i;
@@ -24,6 +24,7 @@ const isNativeWin32Path = /^[a-zA-Z]:[/\\]|^\\\\/i;
2424
*/
2525
function createWebpackLessPlugin(loaderContext) {
2626
const resolve = loaderContext.getResolve({
27+
conditionNames: ['less', 'style'],
2728
mainFields: ['less', 'style', 'main', '...'],
2829
mainFiles: ['index', '...'],
2930
extensions: ['.less', '.css'],
@@ -51,26 +52,17 @@ function createWebpackLessPlugin(loaderContext) {
5152
return false;
5253
}
5354

54-
getUrl(filename, options) {
55-
if (options.ext && !isModuleName.test(filename)) {
56-
return this.tryAppendExtension(filename, options.ext);
57-
}
58-
59-
return filename;
60-
}
61-
62-
async resolveFilename(filename, currentDirectory, options) {
63-
const url = this.getUrl(filename, options);
55+
async resolveFilename(filename, currentDirectory) {
56+
// Less is giving us trailing slashes, but the context should have no trailing slash
57+
const context = currentDirectory.replace(trailingSlash, '');
6458

6559
const request = urlToRequest(
66-
url,
67-
url.charAt(0) === '/' ? loaderContext.rootContext : null
60+
filename,
61+
// eslint-disable-next-line no-undefined
62+
filename.charAt(0) === '/' ? loaderContext.rootContext : undefined
6863
);
6964

70-
// Less is giving us trailing slashes, but the context should have no trailing slash
71-
const context = currentDirectory.replace(trailingSlash, '');
72-
73-
return this.resolveRequests(context, [request, url]);
65+
return this.resolveRequests(context, [...new Set([request, filename])]);
7466
}
7567

7668
resolveRequests(context, possibleRequests) {
@@ -97,7 +89,7 @@ function createWebpackLessPlugin(loaderContext) {
9789
let result;
9890

9991
try {
100-
if (isModuleName.test(filename)) {
92+
if (isModuleImport.test(filename)) {
10193
const error = new Error();
10294

10395
error.type = 'Next';

test/__snapshots__/loader.test.js.snap

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,60 @@ Array [
246246
247247
exports[`loader should provide a useful error message if there was a syntax error: warnings 1`] = `Array []`;
248248
249+
exports[`loader should resolve "@import" with "css" extension: css 1`] = `
250+
"@import \\"css.css\\";
251+
"
252+
`;
253+
254+
exports[`loader should resolve "@import" with "css" extension: errors 1`] = `Array []`;
255+
256+
exports[`loader should resolve "@import" with "css" extension: warnings 1`] = `Array []`;
257+
258+
exports[`loader should resolve "@import" with "less" extension: css 1`] = `
259+
".box {
260+
color: #fe33ac;
261+
border-color: #fdcdea;
262+
background: url(box.png);
263+
}
264+
.box div {
265+
-webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
266+
box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
267+
}
268+
"
269+
`;
270+
271+
exports[`loader should resolve "@import" with "less" extension: errors 1`] = `Array []`;
272+
273+
exports[`loader should resolve "@import" with "less" extension: warnings 1`] = `Array []`;
274+
275+
exports[`loader should resolve "@import" with "php" extension: css 1`] = `
276+
"a {
277+
color: red;
278+
}
279+
"
280+
`;
281+
282+
exports[`loader should resolve "@import" with "php" extension: errors 1`] = `Array []`;
283+
284+
exports[`loader should resolve "@import" with "php" extension: warnings 1`] = `Array []`;
285+
286+
exports[`loader should resolve "@import" without "less" extension: css 1`] = `
287+
".box {
288+
color: #fe33ac;
289+
border-color: #fdcdea;
290+
background: url(box.png);
291+
}
292+
.box div {
293+
-webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
294+
box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
295+
}
296+
"
297+
`;
298+
299+
exports[`loader should resolve "@import" without "less" extension: errors 1`] = `Array []`;
300+
301+
exports[`loader should resolve "@import" without "less" extension: warnings 1`] = `Array []`;
302+
249303
exports[`loader should resolve absolute path with alias: css 1`] = `
250304
".box {
251305
color: #fe33ac;
@@ -408,6 +462,17 @@ exports[`loader should resolve non-less import with alias: errors 1`] = `Array [
408462
409463
exports[`loader should resolve non-less import with alias: warnings 1`] = `Array []`;
410464
465+
exports[`loader should resolve the "less" field from the "exports" field from "package.json": css 1`] = `
466+
".load-me {
467+
color: red;
468+
}
469+
"
470+
`;
471+
472+
exports[`loader should resolve the "less" field from the "exports" field from "package.json": errors 1`] = `Array []`;
473+
474+
exports[`loader should resolve the "less" field from the "exports" field from "package.json": warnings 1`] = `Array []`;
475+
411476
exports[`loader should resolve unresolved url with alias: css 1`] = `
412477
".box {
413478
color: #fe33ac;

test/fixtures/foo.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
a {
2+
color: red;
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import 'package-with-exports';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "css.css";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "basic.less";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "foo.php";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "basic";

0 commit comments

Comments
 (0)