Skip to content

Commit b5c9379

Browse files
feat: postcss@8 (#1204)
1 parent 92fe103 commit b5c9379

File tree

5 files changed

+490
-448
lines changed

5 files changed

+490
-448
lines changed

src/plugins/postcss-icss-parser.js

+83-75
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,99 @@
1-
import postcss from 'postcss';
21
import { extractICSS, replaceValueSymbols, replaceSymbols } from 'icss-utils';
32

43
import { normalizeUrl, resolveRequests, requestify } from '../utils';
54

6-
export default postcss.plugin(
7-
'postcss-icss-parser',
8-
(options) => async (css) => {
9-
const importReplacements = Object.create(null);
10-
const { icssImports, icssExports } = extractICSS(css);
11-
const imports = new Map();
12-
const tasks = [];
13-
14-
// eslint-disable-next-line guard-for-in
15-
for (const url in icssImports) {
16-
const tokens = icssImports[url];
17-
18-
if (Object.keys(tokens).length === 0) {
19-
// eslint-disable-next-line no-continue
20-
continue;
5+
const plugin = (options = {}) => {
6+
return {
7+
postcssPlugin: 'postcss-icss-parser',
8+
async OnceExit(root) {
9+
const importReplacements = Object.create(null);
10+
const { icssImports, icssExports } = extractICSS(root);
11+
const imports = new Map();
12+
const tasks = [];
13+
14+
// eslint-disable-next-line guard-for-in
15+
for (const url in icssImports) {
16+
const tokens = icssImports[url];
17+
18+
if (Object.keys(tokens).length === 0) {
19+
// eslint-disable-next-line no-continue
20+
continue;
21+
}
22+
23+
let normalizedUrl = url;
24+
let prefix = '';
25+
26+
const queryParts = normalizedUrl.split('!');
27+
28+
if (queryParts.length > 1) {
29+
normalizedUrl = queryParts.pop();
30+
prefix = queryParts.join('!');
31+
}
32+
33+
const request = requestify(
34+
normalizeUrl(normalizedUrl, true),
35+
options.rootContext
36+
);
37+
const doResolve = async () => {
38+
const { resolver, context } = options;
39+
const resolvedUrl = await resolveRequests(resolver, context, [
40+
...new Set([normalizedUrl, request]),
41+
]);
42+
43+
return { url: resolvedUrl, prefix, tokens };
44+
};
45+
46+
tasks.push(doResolve());
2147
}
2248

23-
let normalizedUrl = url;
24-
let prefix = '';
49+
const results = await Promise.all(tasks);
2550

26-
const queryParts = normalizedUrl.split('!');
51+
for (let index = 0; index <= results.length - 1; index++) {
52+
const { url, prefix, tokens } = results[index];
53+
const newUrl = prefix ? `${prefix}!${url}` : url;
54+
const importKey = newUrl;
55+
let importName = imports.get(importKey);
2756

28-
if (queryParts.length > 1) {
29-
normalizedUrl = queryParts.pop();
30-
prefix = queryParts.join('!');
31-
}
57+
if (!importName) {
58+
importName = `___CSS_LOADER_ICSS_IMPORT_${imports.size}___`;
59+
imports.set(importKey, importName);
3260

33-
const request = requestify(
34-
normalizeUrl(normalizedUrl, true),
35-
options.rootContext
36-
);
37-
const doResolve = async () => {
38-
const { resolver, context } = options;
39-
const resolvedUrl = await resolveRequests(resolver, context, [
40-
...new Set([normalizedUrl, request]),
41-
]);
42-
43-
return { url: resolvedUrl, prefix, tokens };
44-
};
45-
46-
tasks.push(doResolve());
47-
}
48-
49-
const results = await Promise.all(tasks);
50-
51-
for (let index = 0; index <= results.length - 1; index++) {
52-
const { url, prefix, tokens } = results[index];
53-
const newUrl = prefix ? `${prefix}!${url}` : url;
54-
const importKey = newUrl;
55-
let importName = imports.get(importKey);
56-
57-
if (!importName) {
58-
importName = `___CSS_LOADER_ICSS_IMPORT_${imports.size}___`;
59-
imports.set(importKey, importName);
60-
61-
options.imports.push({
62-
importName,
63-
url: options.urlHandler(newUrl),
64-
icss: true,
65-
index,
66-
});
67-
68-
options.api.push({ importName, dedupe: true, index });
69-
}
61+
options.imports.push({
62+
importName,
63+
url: options.urlHandler(newUrl),
64+
icss: true,
65+
index,
66+
});
7067

71-
for (const [replacementIndex, token] of Object.keys(tokens).entries()) {
72-
const replacementName = `___CSS_LOADER_ICSS_IMPORT_${index}_REPLACEMENT_${replacementIndex}___`;
73-
const localName = tokens[token];
68+
options.api.push({ importName, dedupe: true, index });
69+
}
7470

75-
importReplacements[token] = replacementName;
71+
for (const [replacementIndex, token] of Object.keys(tokens).entries()) {
72+
const replacementName = `___CSS_LOADER_ICSS_IMPORT_${index}_REPLACEMENT_${replacementIndex}___`;
73+
const localName = tokens[token];
7674

77-
options.replacements.push({ replacementName, importName, localName });
75+
importReplacements[token] = replacementName;
76+
77+
options.replacements.push({ replacementName, importName, localName });
78+
}
7879
}
79-
}
8080

81-
if (Object.keys(importReplacements).length > 0) {
82-
replaceSymbols(css, importReplacements);
83-
}
81+
if (Object.keys(importReplacements).length > 0) {
82+
replaceSymbols(root, importReplacements);
83+
}
84+
85+
for (const name of Object.keys(icssExports)) {
86+
const value = replaceValueSymbols(
87+
icssExports[name],
88+
importReplacements
89+
);
90+
91+
options.exports.push({ name, value });
92+
}
93+
},
94+
};
95+
};
8496

85-
for (const name of Object.keys(icssExports)) {
86-
const value = replaceValueSymbols(icssExports[name], importReplacements);
97+
plugin.postcss = true;
8798

88-
options.exports.push({ name, value });
89-
}
90-
}
91-
);
99+
export default plugin;

0 commit comments

Comments
 (0)