Skip to content

Commit 333d90b

Browse files
refactor: code (#13)
1 parent b0514f2 commit 333d90b

File tree

3 files changed

+413
-401
lines changed

3 files changed

+413
-401
lines changed

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@
3737
"homepage": "https://github.com/css-modules/postcss-modules-values#readme",
3838
"devDependencies": {
3939
"coveralls": "^3.1.0",
40-
"eslint": "^7.9.0",
40+
"eslint": "^7.10.0",
4141
"husky": "^4.3.0",
42-
"jest": "^26.4.2",
42+
"jest": "^26.5.2",
4343
"lint-staged": "^10.4.0",
44-
"postcss": "^8.0.7",
44+
"postcss": "^8.1.0",
4545
"prettier": "^2.1.2"
4646
},
4747
"dependencies": {
4848
"icss-utils": "^5.0.0-rc.0"
4949
},
5050
"peerDependencies": {
51-
"postcss": "^8.0.0"
51+
"postcss": "^8.1.0"
5252
}
5353
}

src/index.js

+58-54
Original file line numberDiff line numberDiff line change
@@ -19,66 +19,78 @@ module.exports = () => {
1919
prepare(result) {
2020
const importAliases = [];
2121
const definitions = {};
22-
const addDefinition = (atRule) => {
23-
let matches;
24-
25-
while ((matches = matchValueDefinition.exec(atRule.params))) {
26-
let [, /*match*/ key, value] = matches;
27-
28-
// Add to the definitions, knowing that values can refer to each other
29-
definitions[key] = ICSSUtils.replaceValueSymbols(value, definitions);
30-
atRule.remove();
31-
}
32-
};
33-
const addImport = (atRule) => {
34-
const matches = matchImports.exec(atRule.params);
35-
36-
if (matches) {
37-
let [, /*match*/ aliases, path] = matches;
38-
39-
// We can use constants for path names
40-
if (definitions[path]) {
41-
path = definitions[path];
42-
}
43-
44-
const imports = aliases
45-
.replace(/^\(\s*([\s\S]+)\s*\)$/, "$1")
46-
.split(/\s*,\s*/)
47-
.map((alias) => {
48-
const tokens = matchImport.exec(alias);
49-
50-
if (tokens) {
51-
const [, /*match*/ theirName, myName = theirName] = tokens;
52-
const importedName = createImportedName(myName);
53-
definitions[myName] = importedName;
54-
return { theirName, importedName };
55-
} else {
56-
throw new Error(`@import statement "${alias}" is invalid!`);
57-
}
58-
});
59-
60-
importAliases.push({ path, imports });
61-
62-
atRule.remove();
63-
}
64-
};
6522

6623
return {
6724
/* Look at all the @value statements and treat them as locals or as imports */
6825
AtRule: {
6926
value(atRule) {
7027
if (matchImports.exec(atRule.params)) {
71-
addImport(atRule);
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+
}
7264
} else {
7365
if (atRule.params.indexOf("@value") !== -1) {
7466
result.warn("Invalid value definition: " + atRule.params);
7567
}
7668

77-
addDefinition(atRule);
69+
let matches;
70+
71+
while ((matches = matchValueDefinition.exec(atRule.params))) {
72+
let [, /*match*/ key, value] = matches;
73+
74+
// Add to the definitions, knowing that values can refer to each other
75+
definitions[key] = ICSSUtils.replaceValueSymbols(
76+
value,
77+
definitions
78+
);
79+
80+
atRule.remove();
81+
}
7882
}
7983
},
8084
},
81-
RootExit(root, postcss) {
85+
OnceExit(root, postcss) {
86+
/* If we have no definitions, don't continue */
87+
if (!Object.keys(definitions).length) {
88+
return;
89+
}
90+
91+
/* Perform replacements */
92+
ICSSUtils.replaceSymbols(root, definitions);
93+
8294
/* We want to export anything defined by now, but don't add it to the CSS yet or it well get picked up by the replacement stuff */
8395
const exportDeclarations = Object.keys(definitions).map((key) =>
8496
postcss.decl({
@@ -88,14 +100,6 @@ module.exports = () => {
88100
})
89101
);
90102

91-
/* If we have no definitions, don't continue */
92-
if (!Object.keys(definitions).length) {
93-
return;
94-
}
95-
96-
/* Perform replacements */
97-
ICSSUtils.replaceSymbols(root, definitions);
98-
99103
/* Add export rules if any */
100104
if (exportDeclarations.length > 0) {
101105
const exportRule = postcss.rule({

0 commit comments

Comments
 (0)