From b22e689863773461126e74f122d853e1173236bb Mon Sep 17 00:00:00 2001 From: David Archibald Date: Sat, 20 Mar 2021 13:53:23 -0600 Subject: [PATCH 1/2] Support asyncronous functions in config.url and config.import. --- src/plugins/postcss-import-parser.js | 9 ++++++--- src/plugins/postcss-url-parser.js | 3 ++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/plugins/postcss-import-parser.js b/src/plugins/postcss-import-parser.js index 6f418de1..e593ed9d 100644 --- a/src/plugins/postcss-import-parser.js +++ b/src/plugins/postcss-import-parser.js @@ -157,9 +157,12 @@ const plugin = (options = {}) => { media = valueParser.stringify(mediaNodes).trim().toLowerCase(); } - if (options.filter && !options.filter(normalizedUrl, media)) { - // eslint-disable-next-line no-continue - continue; + if (options.filter) { + const processURL = await options.filter(normalizedUrl, media); + if (!processURL) { + // eslint-disable-next-line no-continue + continue; + } } node.remove(); diff --git a/src/plugins/postcss-url-parser.js b/src/plugins/postcss-url-parser.js index 9587c3c1..ab24cd46 100644 --- a/src/plugins/postcss-url-parser.js +++ b/src/plugins/postcss-url-parser.js @@ -261,7 +261,8 @@ const plugin = (options = {}) => { normalizedUrl = normalizeUrl(normalizedUrl, isStringValue); - if (!options.filter(normalizedUrl)) { + const processUrl = await options.filter(normalizedUrl); + if (!processUrl) { // eslint-disable-next-line no-continue continue; } From f23b110ff4ab430cacb2f5efe492848e89044ef8 Mon Sep 17 00:00:00 2001 From: DavidArchibald Date: Tue, 23 Mar 2021 05:44:48 -0600 Subject: [PATCH 2/2] fix: move await out of loop --- src/plugins/postcss-import-parser.js | 51 +++++++++++++---------- src/plugins/postcss-url-parser.js | 60 +++++++++++++++------------- 2 files changed, 63 insertions(+), 48 deletions(-) diff --git a/src/plugins/postcss-import-parser.js b/src/plugins/postcss-import-parser.js index e593ed9d..eccd7bb8 100644 --- a/src/plugins/postcss-import-parser.js +++ b/src/plugins/postcss-import-parser.js @@ -157,41 +157,50 @@ const plugin = (options = {}) => { media = valueParser.stringify(mediaNodes).trim().toLowerCase(); } - if (options.filter) { - const processURL = await options.filter(normalizedUrl, media); - if (!processURL) { - // eslint-disable-next-line no-continue - continue; - } - } - - node.remove(); - - if (isRequestable) { - const request = requestify(normalizedUrl, options.rootContext); + tasks.push( + (async () => { + if (options.filter) { + const processURL = await options.filter(normalizedUrl, media); + if (!processURL) { + return null; + } + } + + node.remove(); + + if (isRequestable) { + const request = requestify( + normalizedUrl, + options.rootContext + ); - tasks.push( - (async () => { const { resolver, context } = options; const resolvedUrl = await resolveRequests(resolver, context, [ ...new Set([request, normalizedUrl]), ]); return { url: resolvedUrl, media, prefix, isRequestable }; - })() - ); - } else { - tasks.push({ url, media, prefix, isRequestable }); - } + } + + return { url, media, prefix, isRequestable }; + })() + ); } const results = await Promise.all(tasks); for (let index = 0; index <= results.length - 1; index++) { - const { url, isRequestable, media } = results[index]; + const item = results[index]; + + if (item === null) { + // eslint-disable-next-line no-continue + continue; + } + + const { url, isRequestable, media } = item; if (isRequestable) { - const { prefix } = results[index]; + const { prefix } = item; const newUrl = prefix ? `${prefix}!${url}` : url; const importKey = newUrl; let importName = imports.get(importKey); diff --git a/src/plugins/postcss-url-parser.js b/src/plugins/postcss-url-parser.js index ab24cd46..97cf5a48 100644 --- a/src/plugins/postcss-url-parser.js +++ b/src/plugins/postcss-url-parser.js @@ -244,8 +244,6 @@ const plugin = (options = {}) => { const imports = new Map(); const replacements = new Map(); - let hasUrlImportHelper = false; - for (const parsedResult of parsedResults) { const { url, isStringValue } = parsedResult.rule; @@ -261,34 +259,21 @@ const plugin = (options = {}) => { normalizedUrl = normalizeUrl(normalizedUrl, isStringValue); - const processUrl = await options.filter(normalizedUrl); - if (!processUrl) { - // eslint-disable-next-line no-continue - continue; - } - - if (!hasUrlImportHelper) { - options.imports.push({ - importName: "___CSS_LOADER_GET_URL_IMPORT___", - url: options.urlHandler( - require.resolve("../runtime/getUrl.js") - ), - index: -1, - }); - - hasUrlImportHelper = true; - } + tasks.push( + (async () => { + const processUrl = await options.filter(normalizedUrl); + if (!processUrl) { + return null; + } - const splittedUrl = normalizedUrl.split(/(\?)?#/); - const [pathname, query, hashOrQuery] = splittedUrl; + const splittedUrl = normalizedUrl.split(/(\?)?#/); + const [pathname, query, hashOrQuery] = splittedUrl; - let hash = query ? "?" : ""; - hash += hashOrQuery ? `#${hashOrQuery}` : ""; + let hash = query ? "?" : ""; + hash += hashOrQuery ? `#${hashOrQuery}` : ""; - const request = requestify(pathname, options.rootContext); + const request = requestify(pathname, options.rootContext); - tasks.push( - (async () => { const { resolver, context } = options; const resolvedUrl = await resolveRequests(resolver, context, [ ...new Set([request, normalizedUrl]), @@ -301,13 +286,34 @@ const plugin = (options = {}) => { const results = await Promise.all(tasks); + let hasUrlImportHelper = false; + for (let index = 0; index <= results.length - 1; index++) { + const item = results[index]; + + if (item === null) { + // eslint-disable-next-line no-continue + continue; + } + + if (!hasUrlImportHelper) { + options.imports.push({ + importName: "___CSS_LOADER_GET_URL_IMPORT___", + url: options.urlHandler( + require.resolve("../runtime/getUrl.js") + ), + index: -1, + }); + + hasUrlImportHelper = true; + } + const { url, prefix, hash, parsedResult: { node, rule, parsed }, - } = results[index]; + } = item; const newUrl = prefix ? `${prefix}!${url}` : url; const importKey = newUrl; let importName = imports.get(importKey);