Skip to content

Commit 6701cbd

Browse files
refactor: postcss-icss-parser (webpack-contrib#943)
1 parent 37a40bb commit 6701cbd

File tree

4 files changed

+41
-87
lines changed

4 files changed

+41
-87
lines changed

package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"dependencies": {
4444
"camelcase": "^5.2.0",
4545
"cssesc": "^3.0.0",
46-
"icss-utils": "^4.1.0",
46+
"icss-utils": "^4.1.1",
4747
"loader-utils": "^1.2.3",
4848
"normalize-path": "^3.0.0",
4949
"postcss": "^7.0.14",

src/plugins/postcss-icss-parser.js

+30-66
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import postcss from 'postcss';
2-
import valueParser from 'postcss-value-parser';
3-
import { extractICSS, replaceValueSymbols } from 'icss-utils';
2+
import { extractICSS, replaceValueSymbols, replaceSymbols } from 'icss-utils';
43
import loaderUtils from 'loader-utils';
54

65
const pluginName = 'postcss-icss-parser';
76

7+
function hasImportMessage(messages, url) {
8+
return messages.find(
9+
(message) =>
10+
message.pluginName === pluginName &&
11+
message.type === 'import' &&
12+
message.item.url === url &&
13+
message.item.media === ''
14+
);
15+
}
16+
817
export default postcss.plugin(
918
pluginName,
1019
() =>
@@ -14,88 +23,43 @@ export default postcss.plugin(
1423

1524
let index = 0;
1625

17-
Object.keys(icssImports).forEach((key) => {
18-
const url = loaderUtils.parseString(key);
26+
for (const importUrl of Object.keys(icssImports)) {
27+
const url = loaderUtils.parseString(importUrl);
1928

20-
Object.keys(icssImports[key]).forEach((prop) => {
29+
for (const token of Object.keys(icssImports[importUrl])) {
2130
index += 1;
22-
23-
importReplacements[prop] = `___CSS_LOADER_IMPORT___${index}___`;
31+
importReplacements[token] = `___CSS_LOADER_IMPORT___${index}___`;
2432

2533
result.messages.push({
2634
pluginName,
2735
type: 'icss-import',
28-
item: { url, export: icssImports[key][prop], index },
36+
item: { url, export: icssImports[importUrl][token], index },
2937
});
3038

31-
const alreadyIncluded = result.messages.find(
32-
(message) =>
33-
message.pluginName === pluginName &&
34-
message.type === 'import' &&
35-
message.item.url === url &&
36-
message.item.media === ''
37-
);
38-
39-
if (alreadyIncluded) {
40-
return;
39+
if (!hasImportMessage(result.messages, url)) {
40+
result.messages.push({
41+
pluginName,
42+
type: 'import',
43+
item: { url, media: '' },
44+
});
4145
}
42-
43-
result.messages.push({
44-
pluginName,
45-
type: 'import',
46-
item: { url, media: '' },
47-
});
48-
});
49-
});
50-
51-
function replaceImportsInString(str) {
52-
const tokens = valueParser(str);
53-
54-
tokens.walk((node) => {
55-
if (node.type !== 'word') {
56-
return;
57-
}
58-
59-
const token = node.value;
60-
const replacement = importReplacements[token];
61-
62-
if (replacement) {
63-
// eslint-disable-next-line no-param-reassign
64-
node.value = replacement;
65-
}
66-
});
67-
68-
return tokens.toString();
46+
}
6947
}
7048

71-
// Replace tokens
72-
css.walk((node) => {
73-
// Due reusing `ast` from `postcss-loader` some plugins may remove `value`, `selector` or `params` properties
74-
if (node.type === 'decl' && node.value) {
75-
// eslint-disable-next-line no-param-reassign
76-
node.value = replaceImportsInString(node.value.toString());
77-
} else if (node.type === 'rule' && node.selector) {
78-
// eslint-disable-next-line no-param-reassign
79-
node.selector = replaceValueSymbols(
80-
node.selector.toString(),
81-
importReplacements
82-
);
83-
} else if (node.type === 'atrule' && node.params) {
84-
// eslint-disable-next-line no-param-reassign
85-
node.params = replaceImportsInString(node.params.toString());
86-
}
87-
});
49+
replaceSymbols(css, importReplacements);
8850

89-
// Replace tokens in export
90-
Object.keys(icssExports).forEach((exportName) => {
51+
for (const exportName of Object.keys(icssExports)) {
9152
result.messages.push({
9253
pluginName,
9354
type: 'export',
9455
item: {
9556
key: exportName,
96-
value: replaceImportsInString(icssExports[exportName]),
57+
value: replaceValueSymbols(
58+
icssExports[exportName],
59+
importReplacements
60+
),
9761
},
9862
});
99-
});
63+
}
10064
}
10165
);

0 commit comments

Comments
 (0)