Skip to content

Commit c5062db

Browse files
feat: support async functions for url and import options (#1277)
1 parent c86ff94 commit c5062db

File tree

2 files changed

+63
-44
lines changed

2 files changed

+63
-44
lines changed

src/plugins/postcss-import-parser.js

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

160-
if (options.filter && !options.filter(normalizedUrl, media)) {
161-
// eslint-disable-next-line no-continue
162-
continue;
163-
}
164-
165-
node.remove();
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+
);
166176

167-
if (isRequestable) {
168-
const request = requestify(normalizedUrl, options.rootContext);
169-
170-
tasks.push(
171-
(async () => {
172177
const { resolver, context } = options;
173178
const resolvedUrl = await resolveRequests(resolver, context, [
174179
...new Set([request, normalizedUrl]),
175180
]);
176181

177182
return { url: resolvedUrl, media, prefix, isRequestable };
178-
})()
179-
);
180-
} else {
181-
tasks.push({ url, media, prefix, isRequestable });
182-
}
183+
}
184+
185+
return { url, media, prefix, isRequestable };
186+
})()
187+
);
183188
}
184189

185190
const results = await Promise.all(tasks);
186191

187192
for (let index = 0; index <= results.length - 1; index++) {
188-
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;
189201

190202
if (isRequestable) {
191-
const { prefix } = results[index];
203+
const { prefix } = item;
192204
const newUrl = prefix ? `${prefix}!${url}` : url;
193205
const importKey = newUrl;
194206
let importName = imports.get(importKey);

src/plugins/postcss-url-parser.js

+33-26
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,33 +259,21 @@ const plugin = (options = {}) => {
261259

262260
normalizedUrl = normalizeUrl(normalizedUrl, isStringValue);
263261

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

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

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

287-
const request = requestify(pathname, options.rootContext);
275+
const request = requestify(pathname, options.rootContext);
288276

289-
tasks.push(
290-
(async () => {
291277
const { resolver, context } = options;
292278
const resolvedUrl = await resolveRequests(resolver, context, [
293279
...new Set([request, normalizedUrl]),
@@ -300,13 +286,34 @@ const plugin = (options = {}) => {
300286

301287
const results = await Promise.all(tasks);
302288

289+
let hasUrlImportHelper = false;
290+
303291
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+
304311
const {
305312
url,
306313
prefix,
307314
hash,
308315
parsedResult: { node, rule, parsed },
309-
} = results[index];
316+
} = item;
310317
const newUrl = prefix ? `${prefix}!${url}` : url;
311318
const importKey = newUrl;
312319
let importName = imports.get(importKey);

0 commit comments

Comments
 (0)