|
1 | | -import postcss from 'postcss'; |
2 | 1 | import { extractICSS, replaceValueSymbols, replaceSymbols } from 'icss-utils'; |
3 | 2 |
|
4 | 3 | import { normalizeUrl, resolveRequests, requestify } from '../utils'; |
5 | 4 |
|
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()); |
21 | 47 | } |
22 | 48 |
|
23 | | - let normalizedUrl = url; |
24 | | - let prefix = ''; |
| 49 | + const results = await Promise.all(tasks); |
25 | 50 |
|
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); |
27 | 56 |
|
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); |
32 | 60 |
|
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 | + }); |
70 | 67 |
|
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 | + } |
74 | 70 |
|
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]; |
76 | 74 |
|
77 | | - options.replacements.push({ replacementName, importName, localName }); |
| 75 | + importReplacements[token] = replacementName; |
| 76 | + |
| 77 | + options.replacements.push({ replacementName, importName, localName }); |
| 78 | + } |
78 | 79 | } |
79 | | - } |
80 | 80 |
|
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 | +}; |
84 | 96 |
|
85 | | - for (const name of Object.keys(icssExports)) { |
86 | | - const value = replaceValueSymbols(icssExports[name], importReplacements); |
| 97 | +plugin.postcss = true; |
87 | 98 |
|
88 | | - options.exports.push({ name, value }); |
89 | | - } |
90 | | - } |
91 | | -); |
| 99 | +export default plugin; |
0 commit comments