|
3 | 3 | const ICSSUtils = require("icss-utils");
|
4 | 4 |
|
5 | 5 | const matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/;
|
6 |
| -const matchValueDefinition = /(?:\s+|^)([\w-]+):?\s+(.+?)\s*$/g; |
| 6 | +const matchValueDefinition = /(?:\s+|^)([\w-]+):?(.*?)$/; |
7 | 7 | const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/;
|
8 | 8 |
|
9 |
| -let options = {}; |
10 |
| -let importIndex = 0; |
11 |
| -let createImportedName = |
12 |
| - (options && options.createImportedName) || |
13 |
| - ((importName /*, path*/) => |
14 |
| - `i__const_${importName.replace(/\W/g, "_")}_${importIndex++}`); |
| 9 | +module.exports = (options) => { |
| 10 | + let importIndex = 0; |
| 11 | + let createImportedName = |
| 12 | + (options && options.createImportedName) || |
| 13 | + ((importName /*, path*/) => |
| 14 | + `i__const_${importName.replace(/\W/g, "_")}_${importIndex++}`); |
15 | 15 |
|
16 |
| -module.exports = () => { |
17 | 16 | return {
|
18 | 17 | postcssPlugin: "postcss-modules-values",
|
19 | 18 | prepare(result) {
|
20 | 19 | const importAliases = [];
|
21 | 20 | const definitions = {};
|
22 | 21 |
|
23 | 22 | return {
|
24 |
| - /* Look at all the @value statements and treat them as locals or as imports */ |
25 | 23 | AtRule: {
|
26 | 24 | value(atRule) {
|
27 |
| - if (matchImports.exec(atRule.params)) { |
28 |
| - const matches = matchImports.exec(atRule.params); |
29 |
| - |
30 |
| - if (matches) { |
31 |
| - let [, /*match*/ aliases, path] = matches; |
32 |
| - |
33 |
| - // We can use constants for path names |
34 |
| - if (definitions[path]) { |
35 |
| - path = definitions[path]; |
36 |
| - } |
37 |
| - |
38 |
| - const imports = aliases |
39 |
| - .replace(/^\(\s*([\s\S]+)\s*\)$/, "$1") |
40 |
| - .split(/\s*,\s*/) |
41 |
| - .map((alias) => { |
42 |
| - const tokens = matchImport.exec(alias); |
43 |
| - |
44 |
| - if (tokens) { |
45 |
| - const [ |
46 |
| - , |
47 |
| - /*match*/ theirName, |
48 |
| - myName = theirName, |
49 |
| - ] = tokens; |
50 |
| - const importedName = createImportedName(myName); |
51 |
| - definitions[myName] = importedName; |
52 |
| - return { theirName, importedName }; |
53 |
| - } else { |
54 |
| - throw new Error( |
55 |
| - `@import statement "${alias}" is invalid!` |
56 |
| - ); |
57 |
| - } |
58 |
| - }); |
59 |
| - |
60 |
| - importAliases.push({ path, imports }); |
61 |
| - |
62 |
| - atRule.remove(); |
63 |
| - } |
64 |
| - } else { |
65 |
| - if (atRule.params.indexOf("@value") !== -1) { |
66 |
| - result.warn("Invalid value definition: " + atRule.params); |
| 25 | + const matches = atRule.params.match(matchImports); |
| 26 | + |
| 27 | + if (matches) { |
| 28 | + let [, /*match*/ aliases, path] = matches; |
| 29 | + |
| 30 | + // We can use constants for path names |
| 31 | + if (definitions[path]) { |
| 32 | + path = definitions[path]; |
67 | 33 | }
|
68 | 34 |
|
69 |
| - let matches; |
| 35 | + const imports = aliases |
| 36 | + .replace(/^\(\s*([\s\S]+)\s*\)$/, "$1") |
| 37 | + .split(/\s*,\s*/) |
| 38 | + .map((alias) => { |
| 39 | + const tokens = matchImport.exec(alias); |
70 | 40 |
|
71 |
| - while ((matches = matchValueDefinition.exec(atRule.params))) { |
72 |
| - let [, /*match*/ key, value] = matches; |
| 41 | + if (tokens) { |
| 42 | + const [, /*match*/ theirName, myName = theirName] = tokens; |
| 43 | + const importedName = createImportedName(myName); |
| 44 | + definitions[myName] = importedName; |
| 45 | + return { theirName, importedName }; |
| 46 | + } else { |
| 47 | + throw new Error(`@import statement "${alias}" is invalid!`); |
| 48 | + } |
| 49 | + }); |
73 | 50 |
|
74 |
| - // Add to the definitions, knowing that values can refer to each other |
75 |
| - definitions[key] = ICSSUtils.replaceValueSymbols( |
76 |
| - value, |
77 |
| - definitions |
78 |
| - ); |
| 51 | + importAliases.push({ path, imports }); |
79 | 52 |
|
80 |
| - atRule.remove(); |
81 |
| - } |
| 53 | + atRule.remove(); |
| 54 | + |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + if (atRule.params.indexOf("@value") !== -1) { |
| 59 | + result.warn("Invalid value definition: " + atRule.params); |
| 60 | + } |
| 61 | + |
| 62 | + let [, key, value] = `${atRule.params}${atRule.raws.between}`.match( |
| 63 | + matchValueDefinition |
| 64 | + ); |
| 65 | + |
| 66 | + const normalizedValue = value.replace(/\/\*((?!\*\/).*?)\*\//g, ""); |
| 67 | + |
| 68 | + if (normalizedValue.length === 0) { |
| 69 | + result.warn("Invalid value definition: " + atRule.params); |
| 70 | + |
| 71 | + atRule.remove(); |
| 72 | + |
| 73 | + return; |
82 | 74 | }
|
| 75 | + |
| 76 | + let isOnlySpace = /^\s+$/.test(normalizedValue); |
| 77 | + |
| 78 | + if (!isOnlySpace) { |
| 79 | + value = value.trim(); |
| 80 | + } |
| 81 | + |
| 82 | + // Add to the definitions, knowing that values can refer to each other |
| 83 | + definitions[key] = ICSSUtils.replaceValueSymbols( |
| 84 | + value, |
| 85 | + definitions |
| 86 | + ); |
| 87 | + |
| 88 | + atRule.remove(); |
83 | 89 | },
|
84 | 90 | },
|
85 | 91 | OnceExit(root, postcss) {
|
|
0 commit comments