Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support asyncronous functions in config.url and config.import. #1277

Merged
merged 2 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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