Skip to content

Commit fdc802f

Browse files
chore(release): 4.0.0-rc.4 (#14)
1 parent 44d4e85 commit fdc802f

File tree

4 files changed

+198
-159
lines changed

4 files changed

+198
-159
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
All notable changes to this project will be documented in this file.
44
This project adheres to [Semantic Versioning](http://semver.org/).
55

6+
## [4.0.0-rc.4](https://github.com/postcss-modules-local-by-default/compare/v4.0.0-rc.3...v4.0.0-rc.4) - 2020-10-08
7+
8+
### Fixes
9+
10+
- perf
11+
- compatibility with empty custom properties
12+
- works with `options.createImportedName`
13+
614
## [4.0.0-rc.3](https://github.com/postcss-modules-local-by-default/compare/v4.0.0-rc.2...v4.0.0-rc.3) - 2020-10-08
715

816
### BREAKING CHANGE

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-modules-values",
3-
"version": "4.0.0-rc.3",
3+
"version": "4.0.0-rc.4",
44
"description": "PostCSS plugin for CSS Modules to pass arbitrary values between your module files",
55
"main": "src/index.js",
66
"files": [

src/index.js

+65-59
Original file line numberDiff line numberDiff line change
@@ -3,83 +3,89 @@
33
const ICSSUtils = require("icss-utils");
44

55
const matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/;
6-
const matchValueDefinition = /(?:\s+|^)([\w-]+):?\s+(.+?)\s*$/g;
6+
const matchValueDefinition = /(?:\s+|^)([\w-]+):?(.*?)$/;
77
const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/;
88

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++}`);
1515

16-
module.exports = () => {
1716
return {
1817
postcssPlugin: "postcss-modules-values",
1918
prepare(result) {
2019
const importAliases = [];
2120
const definitions = {};
2221

2322
return {
24-
/* Look at all the @value statements and treat them as locals or as imports */
2523
AtRule: {
2624
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];
6733
}
6834

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);
7040

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+
});
7350

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 });
7952

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;
8274
}
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();
8389
},
8490
},
8591
OnceExit(root, postcss) {

0 commit comments

Comments
 (0)