Skip to content

Commit

Permalink
fix: performance (#1144)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Jul 29, 2020
1 parent f9ba0ce commit 4f1baa2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
11 changes: 6 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ export default async function loader(content, map, meta) {

if (shouldUseImportPlugin(options)) {
const resolver = this.getResolve({
conditionNames: ['style'],
extensions: ['.css'],
mainFields: ['css', 'style', 'main', '...'],
mainFiles: ['index', '...'],
extensions: ['.css'],
restrictions: [/\.css$/i],
conditionNames: ['style'],
});

plugins.push(
Expand All @@ -90,8 +90,9 @@ export default async function loader(content, map, meta) {

if (shouldUseURLPlugin(options)) {
const urlResolver = this.getResolve({
mainFields: ['asset'],
conditionNames: ['asset'],
mainFields: ['asset'],
mainFiles: [],
extensions: [],
});

Expand All @@ -113,10 +114,10 @@ export default async function loader(content, map, meta) {

if (needUseModulesPlugins || options.icss) {
const icssResolver = this.getResolve({
conditionNames: ['style'],
extensions: [],
mainFields: ['css', 'style', 'main', '...'],
mainFiles: ['index', '...'],
extensions: [],
conditionNames: ['style'],
});

plugins.push(
Expand Down
31 changes: 16 additions & 15 deletions src/plugins/postcss-import-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,16 @@ export default postcss.plugin(pluginName, (options) => async (css, result) => {
let normalizedUrl = url;
let prefix = '';

const queryParts = normalizedUrl.split('!');

if (queryParts.length > 1) {
normalizedUrl = queryParts.pop();
prefix = queryParts.join('!');
}

const isRequestable = isUrlRequestable(normalizedUrl);

if (isRequestable) {
const queryParts = normalizedUrl.split('!');

if (queryParts.length > 1) {
normalizedUrl = queryParts.pop();
prefix = queryParts.join('!');
}

normalizedUrl = normalizeUrl(normalizedUrl, isStringValue);

// Empty url after normalize - `@import '\
Expand Down Expand Up @@ -149,16 +149,17 @@ export default postcss.plugin(pluginName, (options) => async (css, result) => {

if (isRequestable) {
const request = requestify(normalizedUrl, options.rootContext);
const doResolve = async () => {
const { resolver, context } = options;
const resolvedUrl = await resolveRequests(resolver, context, [
...new Set([request, normalizedUrl]),
]);

return { url: resolvedUrl, media, prefix, isRequestable };
};
tasks.push(
(async () => {
const { resolver, context } = options;
const resolvedUrl = await resolveRequests(resolver, context, [
...new Set([request, normalizedUrl]),
]);

tasks.push(doResolve());
return { url: resolvedUrl, media, prefix, isRequestable };
})()
);
} else {
tasks.push({ url, media, prefix, isRequestable });
}
Expand Down
19 changes: 11 additions & 8 deletions src/plugins/postcss-url-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,17 @@ export default postcss.plugin(pluginName, (options) => async (css, result) => {
hash += hashOrQuery ? `#${hashOrQuery}` : '';

const request = requestify(pathname, options.rootContext);
const doResolve = async () => {
const { resolver, context } = options;
const resolvedUrl = await resolveRequests(resolver, context, [
...new Set([request, normalizedUrl]),
]);

return { url: resolvedUrl, prefix, hash, parsedResult };
};
tasks.push(
(async () => {
const { resolver, context } = options;
const resolvedUrl = await resolveRequests(resolver, context, [
...new Set([request, normalizedUrl]),
]);

tasks.push(doResolve());
return { url: resolvedUrl, prefix, hash, parsedResult };
})()
);
}

const results = await Promise.all(tasks);
Expand Down Expand Up @@ -230,5 +231,7 @@ export default postcss.plugin(pluginName, (options) => async (css, result) => {
decl.value = parsed.toString();
}

console.timeEnd('URL');

return Promise.resolve();
});

0 comments on commit 4f1baa2

Please sign in to comment.