Skip to content

Commit

Permalink
feat: support async functions for url and import options (#1277)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidArchibald authored Mar 24, 2021
1 parent c86ff94 commit c5062db
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 44 deletions.
48 changes: 30 additions & 18 deletions src/plugins/postcss-import-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,38 +157,50 @@ const plugin = (options = {}) => {
media = valueParser.stringify(mediaNodes).trim().toLowerCase();
}

if (options.filter && !options.filter(normalizedUrl, media)) {
// eslint-disable-next-line no-continue
continue;
}

node.remove();
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
);

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);
Expand Down
59 changes: 33 additions & 26 deletions src/plugins/postcss-url-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -261,33 +259,21 @@ const plugin = (options = {}) => {

normalizedUrl = normalizeUrl(normalizedUrl, isStringValue);

if (!options.filter(normalizedUrl)) {
// 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]),
Expand All @@ -300,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);
Expand Down

0 comments on commit c5062db

Please sign in to comment.