Skip to content

Commit 3b12c87

Browse files
refactor: code (#1017)
1 parent c80c39f commit 3b12c87

19 files changed

+645
-367
lines changed

src/index.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,6 @@ export default function loader(content, map, meta) {
3131

3232
const callback = this.async();
3333
const sourceMap = options.sourceMap || false;
34-
35-
// Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
36-
// eslint-disable-next-line no-param-reassign
37-
map = sourceMap && map ? normalizeSourceMap(map) : null;
38-
39-
// Reuse CSS AST (PostCSS AST e.g 'postcss-loader') to avoid reparsing
40-
if (meta) {
41-
const { ast } = meta;
42-
43-
if (ast && ast.type === 'postcss' && ast.version === postcssPkg.version) {
44-
// eslint-disable-next-line no-param-reassign
45-
content = ast.root;
46-
}
47-
}
48-
4934
const plugins = [];
5035

5136
if (options.modules) {
@@ -74,12 +59,27 @@ export default function loader(content, map, meta) {
7459
);
7560
}
7661

62+
// Reuse CSS AST (PostCSS AST e.g 'postcss-loader') to avoid reparsing
63+
if (meta) {
64+
const { ast } = meta;
65+
66+
if (ast && ast.type === 'postcss' && ast.version === postcssPkg.version) {
67+
// eslint-disable-next-line no-param-reassign
68+
content = ast.root;
69+
}
70+
}
71+
7772
postcss(plugins)
7873
.process(content, {
7974
from: this.remainingRequest.split('!').pop(),
8075
to: this.currentRequest.split('!').pop(),
8176
map: options.sourceMap
82-
? { prev: map, inline: false, annotation: false }
77+
? {
78+
// Some loaders (example `"postcss-loader": "1.x.x"`) always generates source map, we should remove it
79+
prev: sourceMap && map ? normalizeSourceMap(map) : null,
80+
inline: false,
81+
annotation: false,
82+
}
8383
: false,
8484
})
8585
.then((result) => {

src/plugins/postcss-icss-parser.js

+36-31
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,67 @@ import { urlToRequest } from 'loader-utils';
44

55
const pluginName = 'postcss-icss-parser';
66

7+
function normalizeIcssImports(icssImports) {
8+
return Object.keys(icssImports).reduce((accumulator, url) => {
9+
const tokensMap = icssImports[url];
10+
const tokens = Object.keys(tokensMap);
11+
12+
if (tokens.length === 0) {
13+
return accumulator;
14+
}
15+
16+
const normalizedUrl = urlToRequest(url);
17+
18+
if (!accumulator[normalizedUrl]) {
19+
// eslint-disable-next-line no-param-reassign
20+
accumulator[normalizedUrl] = tokensMap;
21+
} else {
22+
// eslint-disable-next-line no-param-reassign
23+
accumulator[normalizedUrl] = {
24+
...accumulator[normalizedUrl],
25+
...tokensMap,
26+
};
27+
}
28+
29+
return accumulator;
30+
}, {});
31+
}
32+
733
export default postcss.plugin(
834
pluginName,
935
() =>
1036
function process(css, result) {
1137
const importReplacements = Object.create(null);
1238
const { icssImports, icssExports } = extractICSS(css);
13-
14-
const normalizedIcssImports = Object.keys(icssImports).reduce(
15-
(accumulator, url) => {
16-
const tokensMap = icssImports[url];
17-
const tokens = Object.keys(tokensMap);
18-
19-
if (tokens.length === 0) {
20-
return accumulator;
21-
}
22-
23-
const normalizedUrl = urlToRequest(url);
24-
25-
if (!accumulator[normalizedUrl]) {
26-
// eslint-disable-next-line no-param-reassign
27-
accumulator[normalizedUrl] = tokensMap;
28-
} else {
29-
// eslint-disable-next-line no-param-reassign
30-
accumulator[normalizedUrl] = {
31-
...accumulator[normalizedUrl],
32-
...tokensMap,
33-
};
34-
}
35-
36-
return accumulator;
37-
},
38-
{}
39-
);
39+
const normalizedIcssImports = normalizeIcssImports(icssImports);
4040

4141
Object.keys(normalizedIcssImports).forEach((url, importIndex) => {
4242
const importName = `___CSS_LOADER_ICSS_IMPORT_${importIndex}___`;
4343

4444
result.messages.push({
4545
pluginName,
4646
type: 'import',
47-
value: { type: 'icss-import', name: importName, url },
47+
value: { type: 'icss-import', importName, url },
4848
});
4949

5050
const tokenMap = normalizedIcssImports[url];
5151
const tokens = Object.keys(tokenMap);
5252

5353
tokens.forEach((token, replacementIndex) => {
54-
const name = `___CSS_LOADER_ICSS_IMPORT_${importIndex}_REPLACEMENT_${replacementIndex}___`;
54+
const replacementName = `___CSS_LOADER_ICSS_IMPORT_${importIndex}_REPLACEMENT_${replacementIndex}___`;
5555
const localName = tokenMap[token];
5656

57-
importReplacements[token] = name;
57+
importReplacements[token] = replacementName;
5858

5959
result.messages.push({
6060
pluginName,
6161
type: 'replacer',
62-
value: { type: 'icss-import', name, importName, localName },
62+
value: {
63+
type: 'icss-import',
64+
importName,
65+
replacementName,
66+
localName,
67+
},
6368
});
6469
});
6570
});

src/plugins/postcss-import-parser.js

+10-28
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function parseImport(params) {
5959
}
6060

6161
function walkAtRules(css, result, filter) {
62-
const items = new Map();
62+
const items = [];
6363

6464
css.walkAtRules(/^import$/i, (atRule) => {
6565
// Convert only top-level @import
@@ -91,14 +91,7 @@ function walkAtRules(css, result, filter) {
9191

9292
atRule.remove();
9393

94-
const { url, media } = parsed;
95-
const value = items.get(url);
96-
97-
if (!value) {
98-
items.set(url, new Set([media]));
99-
} else {
100-
value.add(media);
101-
}
94+
items.push(parsed);
10295
});
10396

10497
return items;
@@ -110,25 +103,14 @@ export default postcss.plugin(
110103
function process(css, result) {
111104
const items = walkAtRules(css, result, options.filter);
112105

113-
[...items]
114-
.reduce((accumulator, currentValue) => {
115-
const [url, medias] = currentValue;
116-
117-
medias.forEach((media) => {
118-
accumulator.push({ url, media });
119-
});
120-
121-
return accumulator;
122-
}, [])
123-
.forEach((item, index) => {
124-
const { url, media } = item;
125-
const name = `___CSS_LOADER_AT_RULE_IMPORT_${index}___`;
126-
127-
result.messages.push({
128-
pluginName,
129-
type: 'import',
130-
value: { type: '@import', name, url, media },
131-
});
106+
items.forEach((item) => {
107+
const { url, media } = item;
108+
109+
result.messages.push({
110+
pluginName,
111+
type: 'import',
112+
value: { type: '@import', url, media },
132113
});
114+
});
133115
}
134116
);

src/plugins/postcss-url-parser.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -139,45 +139,44 @@ export default postcss.plugin(
139139
(options) =>
140140
function process(css, result) {
141141
const traversed = walkDecls(css, result, options.filter);
142-
const paths = collectUniqueUrlsWithNodes(
143-
flatten(traversed.map((item) => item.urls))
144-
);
142+
const flattenTraversed = flatten(traversed.map((item) => item.urls));
143+
const urlsWithNodes = collectUniqueUrlsWithNodes(flattenTraversed);
145144
const replacers = new Map();
146145

147-
paths.forEach((path, index) => {
148-
const { url, hash, needQuotes, nodes } = path;
149-
const name = `___CSS_LOADER_URL_IMPORT_${index}___`;
146+
urlsWithNodes.forEach((urlWithNodes, index) => {
147+
const { url, hash, needQuotes, nodes } = urlWithNodes;
148+
const replacementName = `___CSS_LOADER_URL_REPLACEMENT_${index}___`;
150149

151150
result.messages.push(
152151
{
153152
pluginName,
154153
type: 'import',
155-
value: { type: 'url', name, url, needQuotes, hash, index },
154+
value: { type: 'url', replacementName, url, needQuotes, hash },
156155
},
157156
{
158157
pluginName,
159158
type: 'replacer',
160-
value: { type: 'url', name },
159+
value: { type: 'url', replacementName },
161160
}
162161
);
163162

164163
nodes.forEach((node) => {
165-
replacers.set(node, name);
164+
replacers.set(node, replacementName);
166165
});
167166
});
168167

169168
traversed.forEach((item) => {
170169
walkUrls(item.parsed, (node) => {
171-
const name = replacers.get(node);
170+
const replacementName = replacers.get(node);
172171

173-
if (!name) {
172+
if (!replacementName) {
174173
return;
175174
}
176175

177176
// eslint-disable-next-line no-param-reassign
178177
node.type = 'word';
179178
// eslint-disable-next-line no-param-reassign
180-
node.value = name;
179+
node.value = replacementName;
181180
});
182181

183182
// eslint-disable-next-line no-param-reassign

0 commit comments

Comments
 (0)