|
1 | 1 | const postcss = require('postcss') |
2 | 2 | const { default: replaceSymbols, replaceAll } = require('icss-replace-symbols') |
| 3 | +const { genICSSRules } = require('./icss.js') |
3 | 4 |
|
4 | 5 | const matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/ |
5 | 6 | const matchValueDefinition = /(?:\s+|^)([\w-]+):?\s+(.+?)\s*$/g |
6 | 7 | const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/ |
7 | 8 |
|
8 | | -const addImportsRules = (css, imports) => { |
9 | | - const rules = imports.map(({ path, aliases }) => { |
10 | | - const declarations = Object.keys(aliases).map(key => |
11 | | - postcss.decl({ |
12 | | - prop: key, |
13 | | - value: aliases[key], |
14 | | - raws: { before: '\n ' } |
15 | | - }) |
16 | | - ) |
17 | | - return postcss |
18 | | - .rule({ |
19 | | - selector: `:import(${path})`, |
20 | | - raws: { after: '\n' } |
21 | | - }) |
22 | | - .append(declarations) |
23 | | - }) |
24 | | - css.prepend(rules) |
25 | | -} |
26 | | - |
27 | | -const addExportsRule = (css, exports) => { |
28 | | - const declarations = Object.keys(exports).map(key => |
29 | | - postcss.decl({ |
30 | | - prop: key, |
31 | | - value: exports[key], |
32 | | - raws: { before: '\n ' } |
33 | | - }) |
34 | | - ) |
35 | | - const rule = postcss |
36 | | - .rule({ |
37 | | - selector: `:export`, |
38 | | - raws: { after: '\n' } |
39 | | - }) |
40 | | - .append(declarations) |
41 | | - css.prepend(rule) |
42 | | -} |
43 | | - |
44 | 9 | let importIndex = 0 |
45 | 10 | const createImportedName = importName => |
46 | 11 | `i__const_${importName.replace(/\W/g, '_')}_${importIndex++}` |
@@ -74,7 +39,7 @@ module.exports = postcss.plugin('postcss-modules-values', () => ( |
74 | 39 | .map(alias => { |
75 | 40 | let tokens = matchImport.exec(alias) |
76 | 41 | if (tokens) { |
77 | | - let [, /*match*/ theirName, myName = theirName] = tokens |
| 42 | + let [, theirName, myName = theirName] = tokens |
78 | 43 | let importedName = createImportedName(myName) |
79 | 44 | definitions[myName] = importedName |
80 | 45 | return { theirName, importedName } |
@@ -110,7 +75,5 @@ module.exports = postcss.plugin('postcss-modules-values', () => ( |
110 | 75 | /* Perform replacements */ |
111 | 76 | replaceSymbols(css, definitions) |
112 | 77 |
|
113 | | - addExportsRule(css, definitions) |
114 | | - |
115 | | - addImportsRules(css, importAliases) |
| 78 | + css.prepend(genICSSRules(importAliases, definitions)) |
116 | 79 | }) |
0 commit comments