|
1 |
| -import postcss from 'postcss' |
| 1 | +/* eslint-env node */ |
| 2 | +import postcss from "postcss"; |
2 | 3 | import {
|
3 | 4 | replaceSymbols,
|
4 | 5 | replaceValueSymbols,
|
5 | 6 | extractICSS,
|
6 | 7 | createICSSRules
|
7 |
| -} from 'icss-utils' |
| 8 | +} from "icss-utils"; |
8 | 9 |
|
9 |
| -const matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/ |
10 |
| -const matchValueDefinition = /(?:\s+|^)([\w-]+):?\s+(.+?)\s*$/g |
11 |
| -const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/ |
| 10 | +const plugin = "postcss-modules-values"; |
| 11 | + |
| 12 | +const matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/; |
| 13 | +const matchValueDefinition = /(?:\s+|^)([\w-]+):?\s+(.+?)\s*$/g; |
| 14 | +const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/; |
12 | 15 |
|
13 | 16 | // 'i' prefix to prevent postcss parsing "_" as css hook
|
14 | 17 | const getAliasName = (name, index) =>
|
15 |
| - `i__value_${name.replace(/\W/g, '_')}_${index}` |
| 18 | + `__value__${name.replace(/\W/g, "_")}__${index}`; |
16 | 19 |
|
17 |
| -module.exports = postcss.plugin('postcss-modules-values', () => ( |
18 |
| - css, |
19 |
| - result |
20 |
| -) => { |
21 |
| - const { icssImports, icssExports } = extractICSS(css) |
22 |
| - let importIndex = 0 |
| 20 | +module.exports = postcss.plugin(plugin, () => (css, result) => { |
| 21 | + const { icssImports, icssExports } = extractICSS(css); |
| 22 | + let importIndex = 0; |
23 | 23 | const createImportedName = (path, name) => {
|
24 |
| - const importedName = getAliasName(name, importIndex) |
| 24 | + const importedName = getAliasName(name, importIndex); |
25 | 25 | if (icssImports[path] && icssImports[path][importedName]) {
|
26 |
| - importIndex += 1 |
27 |
| - return createImportedName(path, name) |
| 26 | + importIndex += 1; |
| 27 | + return createImportedName(path, name); |
28 | 28 | }
|
29 |
| - importIndex += 1 |
30 |
| - return importedName |
31 |
| - } |
| 29 | + importIndex += 1; |
| 30 | + return importedName; |
| 31 | + }; |
32 | 32 |
|
33 | 33 | const addDefinition = atRule => {
|
34 |
| - let matches |
| 34 | + let matches; |
35 | 35 | while ((matches = matchValueDefinition.exec(atRule.params))) {
|
36 |
| - let [, key, value] = matches |
| 36 | + let [, key, value] = matches; |
37 | 37 | // Add to the definitions, knowing that values can refer to each other
|
38 |
| - icssExports[key] = replaceValueSymbols(value, icssExports) |
39 |
| - atRule.remove() |
| 38 | + icssExports[key] = replaceValueSymbols(value, icssExports); |
| 39 | + atRule.remove(); |
40 | 40 | }
|
41 |
| - } |
| 41 | + }; |
42 | 42 |
|
43 | 43 | const addImport = atRule => {
|
44 |
| - let matches = matchImports.exec(atRule.params) |
| 44 | + let matches = matchImports.exec(atRule.params); |
45 | 45 | if (matches) {
|
46 |
| - let [, aliasesString, path] = matches |
47 |
| - // We can use constants for path names |
48 |
| - if (icssExports[path]) path = icssExports[path] |
| 46 | + const aliasesString = matches[1]; |
| 47 | + let path = matches[2]; |
| 48 | + path = path[0] === "'" || path[0] === '"' ? path.slice(1, -1) : path; |
49 | 49 | let aliases = aliasesString
|
50 |
| - .replace(/^\(\s*([\s\S]+)\s*\)$/, '$1') |
| 50 | + .replace(/^\(\s*([\s\S]+)\s*\)$/, "$1") |
51 | 51 | .split(/\s*,\s*/)
|
52 | 52 | .map(alias => {
|
53 |
| - let tokens = matchImport.exec(alias) |
| 53 | + let tokens = matchImport.exec(alias); |
54 | 54 | if (tokens) {
|
55 |
| - let [, theirName, myName = theirName] = tokens |
56 |
| - let importedName = createImportedName(path, myName) |
57 |
| - icssExports[myName] = importedName |
58 |
| - return { theirName, importedName } |
| 55 | + let [, theirName, myName = theirName] = tokens; |
| 56 | + let importedName = createImportedName(path, myName); |
| 57 | + icssExports[myName] = importedName; |
| 58 | + return { theirName, importedName }; |
59 | 59 | } else {
|
60 |
| - throw new Error(`@import statement "${alias}" is invalid!`) |
| 60 | + throw new Error(`@import statement "${alias}" is invalid!`); |
61 | 61 | }
|
62 | 62 | })
|
63 | 63 | .reduce((acc, { theirName, importedName }) => {
|
64 |
| - acc[importedName] = theirName |
65 |
| - return acc |
66 |
| - }, {}) |
67 |
| - icssImports[path] = Object.assign({}, icssImports[path], aliases) |
68 |
| - atRule.remove() |
| 64 | + acc[importedName] = theirName; |
| 65 | + return acc; |
| 66 | + }, {}); |
| 67 | + icssImports[path] = Object.assign({}, icssImports[path], aliases); |
| 68 | + atRule.remove(); |
69 | 69 | }
|
70 |
| - } |
| 70 | + }; |
71 | 71 |
|
72 | 72 | /* Look at all the @value statements and treat them as locals or as imports */
|
73 |
| - css.walkAtRules('value', atRule => { |
| 73 | + css.walkAtRules("value", atRule => { |
74 | 74 | if (matchImports.exec(atRule.params)) {
|
75 |
| - addImport(atRule) |
| 75 | + addImport(atRule); |
76 | 76 | } else {
|
77 |
| - if (atRule.params.indexOf('@value') !== -1) { |
78 |
| - result.warn('Invalid value definition: ' + atRule.params) |
| 77 | + if (atRule.params.indexOf("@value") !== -1) { |
| 78 | + result.warn("Invalid value definition: " + atRule.params); |
79 | 79 | }
|
80 | 80 |
|
81 |
| - addDefinition(atRule) |
| 81 | + addDefinition(atRule); |
82 | 82 | }
|
83 |
| - }) |
84 |
| - |
85 |
| - if (Object.keys(icssExports).length === 0) return |
| 83 | + }); |
86 | 84 |
|
87 |
| - replaceSymbols(css, icssExports) |
| 85 | + replaceSymbols(css, icssExports); |
88 | 86 |
|
89 |
| - css.prepend(createICSSRules(icssImports, icssExports)) |
90 |
| -}) |
| 87 | + css.prepend(createICSSRules(icssImports, icssExports)); |
| 88 | +}); |
0 commit comments