Skip to content

Commit 3b573a9

Browse files
refactor: postcss-parser (#821)
1 parent eef3c26 commit 3b573a9

File tree

11 files changed

+380
-52
lines changed

11 files changed

+380
-52
lines changed

lib/plugins/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const importParser = require('./postcss-import-parser');
2-
const parser = require('./postcss-parser');
2+
const icssParser = require('./postcss-icss-parser');
33
const urlParser = require('./postcss-url-parser');
44

55
module.exports = {
66
importParser,
7-
parser,
7+
icssParser,
88
urlParser,
99
};

lib/plugins/postcss-parser.js renamed to lib/plugins/postcss-icss-parser.js

+14-39
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
const postcss = require('postcss');
22
const valueParser = require('postcss-value-parser');
33
const icssUtils = require('icss-utils');
4-
const Tokenizer = require('css-selector-tokenizer');
54
const loaderUtils = require('loader-utils');
65

6+
const pluginName = 'postcss-icss-parser';
7+
78
module.exports = postcss.plugin(
8-
'postcss-parser',
9+
pluginName,
910
(options) =>
1011
function process(css) {
1112
const importItems = options.importItems || [];
1213
const urlItems = options.urlItems || [];
13-
const icss = icssUtils.extractICSS(css);
14-
1514
const imports = {};
15+
const icss = icssUtils.extractICSS(css);
1616
const exports = icss.icssExports;
1717

1818
Object.keys(icss.icssImports).forEach((key) => {
@@ -50,46 +50,21 @@ module.exports = postcss.plugin(
5050
return str;
5151
}
5252

53-
Object.keys(exports).forEach((exportName) => {
54-
exports[exportName] = replaceImportsInString(exports[exportName]);
55-
});
56-
57-
function processNode(item) {
58-
switch (item.type) {
59-
case 'value':
60-
item.nodes.forEach(processNode);
61-
break;
62-
case 'nested-item':
63-
item.nodes.forEach(processNode);
64-
break;
65-
case 'item': {
66-
const importIndex = imports[`$${item.name}`];
67-
if (typeof importIndex === 'number') {
68-
// eslint-disable-next-line no-param-reassign
69-
item.name = `___CSS_LOADER_IMPORT___${importIndex}___`;
70-
}
71-
break;
72-
}
73-
// no default
74-
}
75-
}
76-
53+
// Replace tokens in declarations
7754
css.walkDecls((decl) => {
78-
const values = Tokenizer.parseValues(decl.value);
79-
80-
values.nodes.forEach((value) => {
81-
value.nodes.forEach(processNode);
82-
});
83-
8455
// eslint-disable-next-line no-param-reassign
85-
decl.value = Tokenizer.stringifyValues(values);
56+
decl.value = replaceImportsInString(decl.value.toString());
8657
});
8758

59+
// Replace tokens in at-rules
8860
css.walkAtRules((atrule) => {
89-
if (typeof atrule.params === 'string') {
90-
// eslint-disable-next-line no-param-reassign
91-
atrule.params = replaceImportsInString(atrule.params);
92-
}
61+
// eslint-disable-next-line no-param-reassign
62+
atrule.params = replaceImportsInString(atrule.params.toString());
63+
});
64+
65+
// Replace tokens in export
66+
Object.keys(exports).forEach((exportName) => {
67+
exports[exportName] = replaceImportsInString(exports[exportName]);
9368
});
9469

9570
/* eslint-disable no-param-reassign */

lib/processCss.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const extractImports = require('postcss-modules-extract-imports');
1010
const modulesScope = require('postcss-modules-scope');
1111
const modulesValues = require('postcss-modules-values');
1212

13-
const { importParser, parser, urlParser } = require('./plugins');
13+
const { importParser, icssParser, urlParser } = require('./plugins');
1414

1515
const Warning = require('./Warning');
1616
const CssSyntaxError = require('./CssSyntaxError');
@@ -73,7 +73,7 @@ module.exports = function processCss(content, map, options, callback) {
7373
plugins.push(urlParser(parserOptions));
7474
}
7575

76-
plugins.push(parser(parserOptions));
76+
plugins.push(icssParser(parserOptions));
7777

7878
postcss(plugins)
7979
.process(content, {

package-lock.json

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

0 commit comments

Comments
 (0)