|
1 | 1 | const postcss = require('postcss');
|
2 | 2 | const valueParser = require('postcss-value-parser');
|
3 | 3 | const icssUtils = require('icss-utils');
|
4 |
| -const Tokenizer = require('css-selector-tokenizer'); |
5 | 4 | const loaderUtils = require('loader-utils');
|
6 | 5 |
|
| 6 | +const pluginName = 'postcss-icss-parser'; |
| 7 | + |
7 | 8 | module.exports = postcss.plugin(
|
8 |
| - 'postcss-parser', |
| 9 | + pluginName, |
9 | 10 | (options) =>
|
10 | 11 | function process(css) {
|
11 | 12 | const importItems = options.importItems || [];
|
12 | 13 | const urlItems = options.urlItems || [];
|
13 |
| - const icss = icssUtils.extractICSS(css); |
14 |
| - |
15 | 14 | const imports = {};
|
| 15 | + const icss = icssUtils.extractICSS(css); |
16 | 16 | const exports = icss.icssExports;
|
17 | 17 |
|
18 | 18 | Object.keys(icss.icssImports).forEach((key) => {
|
@@ -50,46 +50,21 @@ module.exports = postcss.plugin(
|
50 | 50 | return str;
|
51 | 51 | }
|
52 | 52 |
|
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 |
77 | 54 | css.walkDecls((decl) => {
|
78 |
| - const values = Tokenizer.parseValues(decl.value); |
79 |
| - |
80 |
| - values.nodes.forEach((value) => { |
81 |
| - value.nodes.forEach(processNode); |
82 |
| - }); |
83 |
| - |
84 | 55 | // eslint-disable-next-line no-param-reassign
|
85 |
| - decl.value = Tokenizer.stringifyValues(values); |
| 56 | + decl.value = replaceImportsInString(decl.value.toString()); |
86 | 57 | });
|
87 | 58 |
|
| 59 | + // Replace tokens in at-rules |
88 | 60 | 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]); |
93 | 68 | });
|
94 | 69 |
|
95 | 70 | /* eslint-disable no-param-reassign */
|
|
0 commit comments