|
1 |
| -'use strict'; |
| 1 | +'use strict' |
2 | 2 |
|
3 |
| -const postcss = require('postcss'); |
4 |
| -const ICSSReplaceSymbols = require('icss-replace-symbols'); |
5 |
| -const replaceSymbols = require('icss-replace-symbols'); |
| 3 | +const postcss = require('postcss') |
| 4 | +const ICSSUtils = require('icss-utils') |
6 | 5 |
|
7 |
| -const matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/; |
8 |
| -const matchValueDefinition = /(?:\s+|^)([\w-]+):?\s+(.+?)\s*$/g; |
9 |
| -const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/; |
| 6 | +const matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/ |
| 7 | +const matchValueDefinition = /(?:\s+|^)([\w-]+):?\s+(.+?)\s*$/g |
| 8 | +const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/ |
10 | 9 |
|
11 |
| -let options = {}; |
12 |
| -let importIndex = 0; |
| 10 | +let options = {} |
| 11 | +let importIndex = 0 |
13 | 12 | let createImportedName =
|
14 | 13 | (options && options.createImportedName) ||
|
15 | 14 | ((importName /*, path*/) =>
|
16 |
| - `i__const_${importName.replace(/\W/g, '_')}_${importIndex++}`); |
| 15 | + `i__const_${importName.replace(/\W/g, '_')}_${importIndex++}`) |
17 | 16 |
|
18 | 17 | module.exports = postcss.plugin(
|
19 | 18 | 'postcss-modules-values',
|
20 | 19 | () => (css, result) => {
|
21 |
| - const importAliases = []; |
22 |
| - const definitions = {}; |
| 20 | + const importAliases = [] |
| 21 | + const definitions = {} |
23 | 22 |
|
24 | 23 | const addDefinition = atRule => {
|
25 |
| - let matches; |
| 24 | + let matches |
26 | 25 | while ((matches = matchValueDefinition.exec(atRule.params))) {
|
27 |
| - let [, /*match*/ key, value] = matches; |
| 26 | + let [, /*match*/ key, value] = matches |
28 | 27 | // Add to the definitions, knowing that values can refer to each other
|
29 |
| - definitions[key] = replaceSymbols.replaceAll(definitions, value); |
30 |
| - atRule.remove(); |
| 28 | + definitions[key] = ICSSUtils.replaceValueSymbols(value, definitions) |
| 29 | + atRule.remove() |
31 | 30 | }
|
32 |
| - }; |
| 31 | + } |
33 | 32 |
|
34 | 33 | const addImport = atRule => {
|
35 |
| - const matches = matchImports.exec(atRule.params); |
| 34 | + const matches = matchImports.exec(atRule.params) |
36 | 35 | if (matches) {
|
37 |
| - let [, /*match*/ aliases, path] = matches; |
| 36 | + let [, /*match*/ aliases, path] = matches |
38 | 37 | // We can use constants for path names
|
39 | 38 | if (definitions[path]) {
|
40 |
| - path = definitions[path]; |
| 39 | + path = definitions[path] |
41 | 40 | }
|
42 | 41 | const imports = aliases
|
43 | 42 | .replace(/^\(\s*([\s\S]+)\s*\)$/, '$1')
|
44 | 43 | .split(/\s*,\s*/)
|
45 | 44 | .map(alias => {
|
46 |
| - const tokens = matchImport.exec(alias); |
| 45 | + const tokens = matchImport.exec(alias) |
47 | 46 | if (tokens) {
|
48 |
| - const [, /*match*/ theirName, myName = theirName] = tokens; |
49 |
| - const importedName = createImportedName(myName); |
50 |
| - definitions[myName] = importedName; |
51 |
| - return { theirName, importedName }; |
| 47 | + const [, /*match*/ theirName, myName = theirName] = tokens |
| 48 | + const importedName = createImportedName(myName) |
| 49 | + definitions[myName] = importedName |
| 50 | + return { theirName, importedName } |
52 | 51 | } else {
|
53 |
| - throw new Error(`@import statement "${alias}" is invalid!`); |
| 52 | + throw new Error(`@import statement "${alias}" is invalid!`) |
54 | 53 | }
|
55 |
| - }); |
56 |
| - importAliases.push({ path, imports }); |
57 |
| - atRule.remove(); |
| 54 | + }) |
| 55 | + importAliases.push({ path, imports }) |
| 56 | + atRule.remove() |
58 | 57 | }
|
59 |
| - }; |
| 58 | + } |
60 | 59 |
|
61 | 60 | /* Look at all the @value statements and treat them as locals or as imports */
|
62 | 61 | css.walkAtRules('value', atRule => {
|
63 | 62 | if (matchImports.exec(atRule.params)) {
|
64 |
| - addImport(atRule); |
| 63 | + addImport(atRule) |
65 | 64 | } else {
|
66 | 65 | if (atRule.params.indexOf('@value') !== -1) {
|
67 |
| - result.warn('Invalid value definition: ' + atRule.params); |
| 66 | + result.warn('Invalid value definition: ' + atRule.params) |
68 | 67 | }
|
69 | 68 |
|
70 |
| - addDefinition(atRule); |
| 69 | + addDefinition(atRule) |
71 | 70 | }
|
72 |
| - }); |
| 71 | + }) |
73 | 72 |
|
74 | 73 | /* We want to export anything defined by now, but don't add it to the CSS yet or
|
75 | 74 | it well get picked up by the replacement stuff */
|
76 | 75 | const exportDeclarations = Object.keys(definitions).map(key =>
|
77 | 76 | postcss.decl({
|
78 | 77 | value: definitions[key],
|
79 | 78 | prop: key,
|
80 |
| - raws: { before: '\n ' } |
| 79 | + raws: { before: '\n ' }, |
81 | 80 | })
|
82 |
| - ); |
| 81 | + ) |
83 | 82 |
|
84 | 83 | /* If we have no definitions, don't continue */
|
85 | 84 | if (!Object.keys(definitions).length) {
|
86 |
| - return; |
| 85 | + return |
87 | 86 | }
|
88 | 87 |
|
89 | 88 | /* Perform replacements */
|
90 |
| - ICSSReplaceSymbols.default(css, definitions); |
| 89 | + ICSSUtils.replaceSymbols(css, definitions) |
91 | 90 |
|
92 | 91 | /* Add export rules if any */
|
93 | 92 | if (exportDeclarations.length > 0) {
|
94 | 93 | const exportRule = postcss.rule({
|
95 | 94 | selector: ':export',
|
96 |
| - raws: { after: '\n' } |
97 |
| - }); |
98 |
| - exportRule.append(exportDeclarations); |
99 |
| - css.prepend(exportRule); |
| 95 | + raws: { after: '\n' }, |
| 96 | + }) |
| 97 | + exportRule.append(exportDeclarations) |
| 98 | + css.prepend(exportRule) |
100 | 99 | }
|
101 | 100 |
|
102 | 101 | /* Add import rules */
|
103 | 102 | importAliases.reverse().forEach(({ path, imports }) => {
|
104 | 103 | const importRule = postcss.rule({
|
105 | 104 | selector: `:import(${path})`,
|
106 |
| - raws: { after: '\n' } |
107 |
| - }); |
| 105 | + raws: { after: '\n' }, |
| 106 | + }) |
108 | 107 | imports.forEach(({ theirName, importedName }) => {
|
109 | 108 | importRule.append({
|
110 | 109 | value: theirName,
|
111 | 110 | prop: importedName,
|
112 |
| - raws: { before: '\n ' } |
113 |
| - }); |
114 |
| - }); |
| 111 | + raws: { before: '\n ' }, |
| 112 | + }) |
| 113 | + }) |
115 | 114 |
|
116 |
| - css.prepend(importRule); |
117 |
| - }); |
| 115 | + css.prepend(importRule) |
| 116 | + }) |
118 | 117 | }
|
119 |
| -); |
| 118 | +) |
0 commit comments