Skip to content

Commit e913cb1

Browse files
fix: do not crash on 'false' aliases (#1292)
1 parent 2388439 commit e913cb1

19 files changed

+312
-1182
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
"stylus": "^0.54.8",
9090
"stylus-loader": "^4.3.0",
9191
"url-loader": "^4.1.1",
92-
"webpack": "^5.33.2"
92+
"webpack": "^5.34.0"
9393
},
9494
"keywords": [
9595
"webpack",

src/plugins/postcss-icss-parser.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ const plugin = (options = {}) => {
4040
...new Set([normalizedUrl, request]),
4141
]);
4242

43+
if (!resolvedUrl) {
44+
return;
45+
}
46+
47+
// eslint-disable-next-line consistent-return
4348
return { url: resolvedUrl, prefix, tokens };
4449
};
4550

@@ -49,8 +54,14 @@ const plugin = (options = {}) => {
4954
const results = await Promise.all(tasks);
5055

5156
for (let index = 0; index <= results.length - 1; index++) {
52-
const { url, prefix, tokens } = results[index];
53-
const newUrl = prefix ? `${prefix}!${url}` : url;
57+
const item = results[index];
58+
59+
if (!item) {
60+
// eslint-disable-next-line no-continue
61+
continue;
62+
}
63+
64+
const newUrl = item.prefix ? `${item.prefix}!${item.url}` : item.url;
5465
const importKey = newUrl;
5566
let importName = imports.get(importKey);
5667

@@ -68,9 +79,11 @@ const plugin = (options = {}) => {
6879
options.api.push({ importName, dedupe: true, index });
6980
}
7081

71-
for (const [replacementIndex, token] of Object.keys(tokens).entries()) {
82+
for (const [replacementIndex, token] of Object.keys(
83+
item.tokens
84+
).entries()) {
7285
const replacementName = `___CSS_LOADER_ICSS_IMPORT_${index}_REPLACEMENT_${replacementIndex}___`;
73-
const localName = tokens[token];
86+
const localName = item.tokens[token];
7487

7588
importReplacements[token] = replacementName;
7689

src/plugins/postcss-import-parser.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,10 @@ const plugin = (options = {}) => {
166166
const needKeep = await options.filter(url, media);
167167

168168
if (!needKeep) {
169-
return null;
169+
return;
170170
}
171171
}
172172

173-
atRule.remove();
174-
175173
if (isRequestable) {
176174
const request = requestify(url, options.rootContext);
177175

@@ -180,9 +178,19 @@ const plugin = (options = {}) => {
180178
...new Set([request, url]),
181179
]);
182180

181+
if (!resolvedUrl) {
182+
return;
183+
}
184+
185+
atRule.remove();
186+
187+
// eslint-disable-next-line consistent-return
183188
return { url: resolvedUrl, media, prefix, isRequestable };
184189
}
185190

191+
atRule.remove();
192+
193+
// eslint-disable-next-line consistent-return
186194
return { url, media, prefix, isRequestable };
187195
})
188196
);

src/plugins/postcss-url-parser.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ const plugin = (options = {}) => {
292292
const needKeep = await options.filter(url);
293293

294294
if (!needKeep) {
295-
return null;
295+
return;
296296
}
297297
}
298298

@@ -309,19 +309,26 @@ const plugin = (options = {}) => {
309309
...new Set([request, url]),
310310
]);
311311

312+
if (!resolvedUrl) {
313+
return;
314+
}
315+
316+
// eslint-disable-next-line consistent-return
312317
return { ...parsedDeclaration, url: resolvedUrl, hash };
313318
})
314319
);
315320

316-
const results = await Promise.all(resolvedDeclarations);
317-
318321
const urlToNameMap = new Map();
319322
const urlToReplacementMap = new Map();
320323

321324
let hasUrlImportHelper = false;
322325

323-
for (let index = 0; index <= results.length - 1; index++) {
324-
const item = results[index];
326+
for (
327+
let index = 0;
328+
index <= resolvedDeclarations.length - 1;
329+
index++
330+
) {
331+
const item = resolvedDeclarations[index];
325332

326333
if (!item) {
327334
// eslint-disable-next-line no-continue

test/__snapshots__/import-option.test.js.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,6 +1865,10 @@ Warning
18651865
]
18661866
`;
18671867

1868+
exports[`"import" option should work with 'false' aliases: errors 1`] = `Array []`;
1869+
1870+
exports[`"import" option should work with 'false' aliases: warnings 1`] = `Array []`;
1871+
18681872
exports[`"import" option should work with a value equal to "false": errors 1`] = `Array []`;
18691873

18701874
exports[`"import" option should work with a value equal to "false": module 1`] = `

test/__snapshots__/modules-option.test.js.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4239,6 +4239,10 @@ Object {
42394239

42404240
exports[`"modules" option should work with "exportOnlyLocals" and "namedExport" option: warnings 1`] = `Array []`;
42414241

4242+
exports[`"modules" option should work with "false" alises: errors 1`] = `Array []`;
4243+
4244+
exports[`"modules" option should work with "false" alises: warnings 1`] = `Array []`;
4245+
42424246
exports[`"modules" option should work with "url" and "namedExport": errors 1`] = `Array []`;
42434247

42444248
exports[`"modules" option should work with "url" and "namedExport": module 1`] = `

test/__snapshots__/url-option.test.js.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,10 @@ Warning
655655
]
656656
`;
657657

658+
exports[`"url" option should work with 'false' aliases: errors 1`] = `Array []`;
659+
660+
exports[`"url" option should work with 'false' aliases: warnings 1`] = `Array []`;
661+
658662
exports[`"url" option should work with a value equal to "Function": errors 1`] = `Array []`;
659663

660664
exports[`"url" option should work with a value equal to "Function": module 1`] = `

test/fixtures/import/false-alias.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import "/style.css";
2+
3+
.class {
4+
color: red;
5+
}

test/fixtures/import/false-alias.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import css from './false-alias.css';
2+
3+
__export__ = css;
4+
5+
export default css;

0 commit comments

Comments
 (0)