Skip to content

Commit e0baf2f

Browse files
committed
fix: values should be replaced in selectors as well
1 parent 1383b62 commit e0baf2f

File tree

4 files changed

+141
-107
lines changed

4 files changed

+141
-107
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"autotest": "chokidar src test -c 'npm test'",
1414
"cover": "nyc mocha",
1515
"travis": "yarn lint && yarn cover",
16-
"prepublish": "yarn test"
16+
"prepublishOnly": "yarn test"
1717
},
1818
"repository": {
1919
"type": "git",
@@ -34,12 +34,12 @@
3434
"chokidar-cli": "^1.0.1",
3535
"codecov.io": "^0.1.2",
3636
"coveralls": "^3.0.2",
37-
"mocha": "^5.2.0",
3837
"eslint": "^5.9.0",
38+
"mocha": "^5.2.0",
3939
"nyc": "^13.1.0"
4040
},
4141
"dependencies": {
42-
"icss-replace-symbols": "^1.1.0",
42+
"icss-utils": "^4.0.0",
4343
"postcss": "^7.0.6"
4444
}
4545
}

src/index.js

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,118 @@
1-
'use strict';
1+
'use strict'
22

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')
65

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-]+))?/
109

11-
let options = {};
12-
let importIndex = 0;
10+
let options = {}
11+
let importIndex = 0
1312
let createImportedName =
1413
(options && options.createImportedName) ||
1514
((importName /*, path*/) =>
16-
`i__const_${importName.replace(/\W/g, '_')}_${importIndex++}`);
15+
`i__const_${importName.replace(/\W/g, '_')}_${importIndex++}`)
1716

1817
module.exports = postcss.plugin(
1918
'postcss-modules-values',
2019
() => (css, result) => {
21-
const importAliases = [];
22-
const definitions = {};
20+
const importAliases = []
21+
const definitions = {}
2322

2423
const addDefinition = atRule => {
25-
let matches;
24+
let matches
2625
while ((matches = matchValueDefinition.exec(atRule.params))) {
27-
let [, /*match*/ key, value] = matches;
26+
let [, /*match*/ key, value] = matches
2827
// 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()
3130
}
32-
};
31+
}
3332

3433
const addImport = atRule => {
35-
const matches = matchImports.exec(atRule.params);
34+
const matches = matchImports.exec(atRule.params)
3635
if (matches) {
37-
let [, /*match*/ aliases, path] = matches;
36+
let [, /*match*/ aliases, path] = matches
3837
// We can use constants for path names
3938
if (definitions[path]) {
40-
path = definitions[path];
39+
path = definitions[path]
4140
}
4241
const imports = aliases
4342
.replace(/^\(\s*([\s\S]+)\s*\)$/, '$1')
4443
.split(/\s*,\s*/)
4544
.map(alias => {
46-
const tokens = matchImport.exec(alias);
45+
const tokens = matchImport.exec(alias)
4746
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 }
5251
} else {
53-
throw new Error(`@import statement "${alias}" is invalid!`);
52+
throw new Error(`@import statement "${alias}" is invalid!`)
5453
}
55-
});
56-
importAliases.push({ path, imports });
57-
atRule.remove();
54+
})
55+
importAliases.push({ path, imports })
56+
atRule.remove()
5857
}
59-
};
58+
}
6059

6160
/* Look at all the @value statements and treat them as locals or as imports */
6261
css.walkAtRules('value', atRule => {
6362
if (matchImports.exec(atRule.params)) {
64-
addImport(atRule);
63+
addImport(atRule)
6564
} else {
6665
if (atRule.params.indexOf('@value') !== -1) {
67-
result.warn('Invalid value definition: ' + atRule.params);
66+
result.warn('Invalid value definition: ' + atRule.params)
6867
}
6968

70-
addDefinition(atRule);
69+
addDefinition(atRule)
7170
}
72-
});
71+
})
7372

7473
/* We want to export anything defined by now, but don't add it to the CSS yet or
7574
it well get picked up by the replacement stuff */
7675
const exportDeclarations = Object.keys(definitions).map(key =>
7776
postcss.decl({
7877
value: definitions[key],
7978
prop: key,
80-
raws: { before: '\n ' }
79+
raws: { before: '\n ' },
8180
})
82-
);
81+
)
8382

8483
/* If we have no definitions, don't continue */
8584
if (!Object.keys(definitions).length) {
86-
return;
85+
return
8786
}
8887

8988
/* Perform replacements */
90-
ICSSReplaceSymbols.default(css, definitions);
89+
ICSSUtils.replaceSymbols(css, definitions)
9190

9291
/* Add export rules if any */
9392
if (exportDeclarations.length > 0) {
9493
const exportRule = postcss.rule({
9594
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)
10099
}
101100

102101
/* Add import rules */
103102
importAliases.reverse().forEach(({ path, imports }) => {
104103
const importRule = postcss.rule({
105104
selector: `:import(${path})`,
106-
raws: { after: '\n' }
107-
});
105+
raws: { after: '\n' },
106+
})
108107
imports.forEach(({ theirName, importedName }) => {
109108
importRule.append({
110109
value: theirName,
111110
prop: importedName,
112-
raws: { before: '\n ' }
113-
});
114-
});
111+
raws: { before: '\n ' },
112+
})
113+
})
115114

116-
css.prepend(importRule);
117-
});
115+
css.prepend(importRule)
116+
})
118117
}
119-
);
118+
)

0 commit comments

Comments
 (0)