-
-
Notifications
You must be signed in to change notification settings - Fork 608
/
Copy pathpostcss-icss-parser.js
147 lines (119 loc) · 3.94 KB
/
postcss-icss-parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import postcss from 'postcss';
import { extractICSS, replaceValueSymbols, replaceSymbols } from 'icss-utils';
import { normalizeUrl, resolveRequests, isUrlRequestable } from '../utils';
function makeRequestableIcssImports(icssImports, rootContext) {
return Object.keys(icssImports).reduce((accumulator, url) => {
const tokensMap = icssImports[url];
const tokens = Object.keys(tokensMap);
if (tokens.length === 0) {
return accumulator;
}
const isRequestable = isUrlRequestable(url);
let normalizedUrl;
if (isRequestable) {
normalizedUrl = normalizeUrl(url, true, rootContext);
}
const key = typeof normalizedUrl !== 'undefined' ? normalizedUrl : url;
if (!accumulator[key]) {
// eslint-disable-next-line no-param-reassign
accumulator[key] = { url, tokenMap: tokensMap };
} else {
// eslint-disable-next-line no-param-reassign
accumulator[key] = {
url,
tokenMap: {
...accumulator[key].tokenMap,
...tokensMap,
},
};
}
return accumulator;
}, {});
}
export default postcss.plugin(
'postcss-icss-parser',
(options) => (css, result) => {
return new Promise(async (resolve, reject) => {
const importReplacements = Object.create(null);
const extractedICSS = extractICSS(css);
const icssImports = makeRequestableIcssImports(
extractedICSS.icssImports,
options.rootContext
);
const tasks = [];
let index = 0;
for (const [importIndex, normalizedUrl] of Object.keys(
icssImports
).entries()) {
const { url } = icssImports[normalizedUrl];
index += 1;
tasks.push(
Promise.resolve(index).then(async (currentIndex) => {
const importName = `___CSS_LOADER_ICSS_IMPORT_${importIndex}___`;
const { resolver, context } = options;
let resolvedUrl;
try {
resolvedUrl = await resolveRequests(resolver, context, [
...new Set([normalizedUrl, url]),
]);
} catch (error) {
throw error;
}
result.messages.push(
{
type: 'import',
value: {
// 'CSS_LOADER_ICSS_IMPORT'
order: 0,
icss: true,
importName,
url: options.urlHandler(resolvedUrl),
index: currentIndex,
},
},
{
type: 'api-import',
value: {
// 'CSS_LOADER_ICSS_IMPORT'
order: 0,
type: 'internal',
importName,
dedupe: true,
index: currentIndex,
},
}
);
const { tokenMap } = icssImports[normalizedUrl];
const tokens = Object.keys(tokenMap);
for (const [replacementIndex, token] of tokens.entries()) {
const replacementName = `___CSS_LOADER_ICSS_IMPORT_${importIndex}_REPLACEMENT_${replacementIndex}___`;
const localName = tokenMap[token];
importReplacements[token] = replacementName;
result.messages.push({
type: 'icss-replacement',
value: { replacementName, importName, localName },
});
}
})
);
}
try {
await Promise.all(tasks);
} catch (error) {
reject(error);
}
if (Object.keys(importReplacements).length > 0) {
replaceSymbols(css, importReplacements);
}
const { icssExports } = extractedICSS;
for (const name of Object.keys(icssExports)) {
const value = replaceValueSymbols(
icssExports[name],
importReplacements
);
result.messages.push({ type: 'export', value: { name, value } });
}
resolve();
});
}
);