Skip to content

Commit f23b110

Browse files
fix: move await out of loop
1 parent b22e689 commit f23b110

File tree

2 files changed

+63
-48
lines changed

2 files changed

+63
-48
lines changed

src/plugins/postcss-import-parser.js

+30-21
Original file line numberDiff line numberDiff line change
@@ -157,41 +157,50 @@ const plugin = (options = {}) => {
157157
media = valueParser.stringify(mediaNodes).trim().toLowerCase();
158158
}
159159

160-
if (options.filter) {
161-
const processURL = await options.filter(normalizedUrl, media);
162-
if (!processURL) {
163-
// eslint-disable-next-line no-continue
164-
continue;
165-
}
166-
}
167-
168-
node.remove();
169-
170-
if (isRequestable) {
171-
const request = requestify(normalizedUrl, options.rootContext);
160+
tasks.push(
161+
(async () => {
162+
if (options.filter) {
163+
const processURL = await options.filter(normalizedUrl, media);
164+
if (!processURL) {
165+
return null;
166+
}
167+
}
168+
169+
node.remove();
170+
171+
if (isRequestable) {
172+
const request = requestify(
173+
normalizedUrl,
174+
options.rootContext
175+
);
172176

173-
tasks.push(
174-
(async () => {
175177
const { resolver, context } = options;
176178
const resolvedUrl = await resolveRequests(resolver, context, [
177179
...new Set([request, normalizedUrl]),
178180
]);
179181

180182
return { url: resolvedUrl, media, prefix, isRequestable };
181-
})()
182-
);
183-
} else {
184-
tasks.push({ url, media, prefix, isRequestable });
185-
}
183+
}
184+
185+
return { url, media, prefix, isRequestable };
186+
})()
187+
);
186188
}
187189

188190
const results = await Promise.all(tasks);
189191

190192
for (let index = 0; index <= results.length - 1; index++) {
191-
const { url, isRequestable, media } = results[index];
193+
const item = results[index];
194+
195+
if (item === null) {
196+
// eslint-disable-next-line no-continue
197+
continue;
198+
}
199+
200+
const { url, isRequestable, media } = item;
192201

193202
if (isRequestable) {
194-
const { prefix } = results[index];
203+
const { prefix } = item;
195204
const newUrl = prefix ? `${prefix}!${url}` : url;
196205
const importKey = newUrl;
197206
let importName = imports.get(importKey);

src/plugins/postcss-url-parser.js

+33-27
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,6 @@ const plugin = (options = {}) => {
244244
const imports = new Map();
245245
const replacements = new Map();
246246

247-
let hasUrlImportHelper = false;
248-
249247
for (const parsedResult of parsedResults) {
250248
const { url, isStringValue } = parsedResult.rule;
251249

@@ -261,34 +259,21 @@ const plugin = (options = {}) => {
261259

262260
normalizedUrl = normalizeUrl(normalizedUrl, isStringValue);
263261

264-
const processUrl = await options.filter(normalizedUrl);
265-
if (!processUrl) {
266-
// eslint-disable-next-line no-continue
267-
continue;
268-
}
269-
270-
if (!hasUrlImportHelper) {
271-
options.imports.push({
272-
importName: "___CSS_LOADER_GET_URL_IMPORT___",
273-
url: options.urlHandler(
274-
require.resolve("../runtime/getUrl.js")
275-
),
276-
index: -1,
277-
});
278-
279-
hasUrlImportHelper = true;
280-
}
262+
tasks.push(
263+
(async () => {
264+
const processUrl = await options.filter(normalizedUrl);
265+
if (!processUrl) {
266+
return null;
267+
}
281268

282-
const splittedUrl = normalizedUrl.split(/(\?)?#/);
283-
const [pathname, query, hashOrQuery] = splittedUrl;
269+
const splittedUrl = normalizedUrl.split(/(\?)?#/);
270+
const [pathname, query, hashOrQuery] = splittedUrl;
284271

285-
let hash = query ? "?" : "";
286-
hash += hashOrQuery ? `#${hashOrQuery}` : "";
272+
let hash = query ? "?" : "";
273+
hash += hashOrQuery ? `#${hashOrQuery}` : "";
287274

288-
const request = requestify(pathname, options.rootContext);
275+
const request = requestify(pathname, options.rootContext);
289276

290-
tasks.push(
291-
(async () => {
292277
const { resolver, context } = options;
293278
const resolvedUrl = await resolveRequests(resolver, context, [
294279
...new Set([request, normalizedUrl]),
@@ -301,13 +286,34 @@ const plugin = (options = {}) => {
301286

302287
const results = await Promise.all(tasks);
303288

289+
let hasUrlImportHelper = false;
290+
304291
for (let index = 0; index <= results.length - 1; index++) {
292+
const item = results[index];
293+
294+
if (item === null) {
295+
// eslint-disable-next-line no-continue
296+
continue;
297+
}
298+
299+
if (!hasUrlImportHelper) {
300+
options.imports.push({
301+
importName: "___CSS_LOADER_GET_URL_IMPORT___",
302+
url: options.urlHandler(
303+
require.resolve("../runtime/getUrl.js")
304+
),
305+
index: -1,
306+
});
307+
308+
hasUrlImportHelper = true;
309+
}
310+
305311
const {
306312
url,
307313
prefix,
308314
hash,
309315
parsedResult: { node, rule, parsed },
310-
} = results[index];
316+
} = item;
311317
const newUrl = prefix ? `${prefix}!${url}` : url;
312318
const importKey = newUrl;
313319
let importName = imports.get(importKey);

0 commit comments

Comments
 (0)