@lesjoursfr/postcss-extract-css-variables 1.0.0 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -0
- package/index.js +33 -5
- package/package.json +8 -8
package/CHANGELOG.md
CHANGED
package/index.js
CHANGED
|
@@ -2,6 +2,7 @@ const os = require('os');
|
|
|
2
2
|
const fs = require('fs');
|
|
3
3
|
const CSS_VARIABLE_DECLARATION = /^--/;
|
|
4
4
|
const CSS_VARIABLE_USE = /(var\([a-zA-Z0-9-]+\))/;
|
|
5
|
+
const HTML_NODE = /^html([#.:[][#.:_[\]\-a-zA-Z0-9]+)?(?:\s|$)/;
|
|
5
6
|
|
|
6
7
|
class CSSVariable {
|
|
7
8
|
constructor(name, selector, value) {
|
|
@@ -14,11 +15,31 @@ class CSSVariable {
|
|
|
14
15
|
return value.includes(`var(${this.name})`);
|
|
15
16
|
}
|
|
16
17
|
|
|
17
|
-
wrapAndReplace(selector, prop, value) {
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
wrapAndReplace(selector, prop, value, important) {
|
|
19
|
+
// Get new selector & value
|
|
20
|
+
const newSelector = this._mixSelectors(this.selector, selector.split(',')).join(',');
|
|
21
|
+
const newValue = value.replaceAll(`var(${this.name})`, this.value);
|
|
20
22
|
|
|
21
|
-
|
|
23
|
+
// Return the rule
|
|
24
|
+
return (important === true)
|
|
25
|
+
? `${newSelector} { ${prop}: ${newValue}!important; }`
|
|
26
|
+
: `${newSelector} { ${prop}: ${newValue}; }`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
_mixSelectors(varSelector, ruleSelectors) {
|
|
30
|
+
return ruleSelectors.map((ruleSelector) => {
|
|
31
|
+
if (HTML_NODE.test(ruleSelector)) {
|
|
32
|
+
const varHtmlMatch = HTML_NODE.exec(varSelector);
|
|
33
|
+
const ruleHtmlMatch = HTML_NODE.exec(ruleSelector);
|
|
34
|
+
if (varHtmlMatch !== null) {
|
|
35
|
+
return `html${varHtmlMatch[1] || ''}${ruleHtmlMatch[1] || ''} ${ruleSelector.replace(HTML_NODE, '').trim()}`;
|
|
36
|
+
} else {
|
|
37
|
+
return ruleSelector.replace(`html${ruleHtmlMatch[1] || ''}`, `html${ruleHtmlMatch[1] || ''} ${varSelector}`);
|
|
38
|
+
}
|
|
39
|
+
} else {
|
|
40
|
+
return `${varSelector} ${ruleSelector.trim()}`;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
22
43
|
}
|
|
23
44
|
}
|
|
24
45
|
|
|
@@ -72,7 +93,14 @@ module.exports = (opts = {}) => {
|
|
|
72
93
|
if (CSS_VARIABLE_USE.test(declaration.value)) {
|
|
73
94
|
for (const cssVariable of cssVariablesHolder) {
|
|
74
95
|
if (cssVariable.isUsed(declaration.value)) {
|
|
75
|
-
generatedRules.push(
|
|
96
|
+
generatedRules.push(
|
|
97
|
+
cssVariable.wrapAndReplace(
|
|
98
|
+
rule.selector,
|
|
99
|
+
declaration.prop,
|
|
100
|
+
declaration.value,
|
|
101
|
+
declaration.important
|
|
102
|
+
)
|
|
103
|
+
);
|
|
76
104
|
}
|
|
77
105
|
}
|
|
78
106
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lesjoursfr/postcss-extract-css-variables",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "PostCSS plugin to extract rules with CSS variables",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "lesjoursfr/postcss-extract-css-variables",
|
|
@@ -21,16 +21,16 @@
|
|
|
21
21
|
"test": "jest --coverage && eslint ."
|
|
22
22
|
},
|
|
23
23
|
"engines": {
|
|
24
|
-
"node": ">=
|
|
24
|
+
"node": ">=v16.15.0"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
|
-
"postcss": "^8.4.
|
|
27
|
+
"postcss": "^8.4.14"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"eslint": "^8.
|
|
31
|
-
"eslint-plugin-jest": "^
|
|
32
|
-
"jest": "^
|
|
33
|
-
"postcss": "^8.4.
|
|
30
|
+
"eslint": "^8.16.0",
|
|
31
|
+
"eslint-plugin-jest": "^26.4.6",
|
|
32
|
+
"jest": "^28.1.0",
|
|
33
|
+
"postcss": "^8.4.14"
|
|
34
34
|
},
|
|
35
35
|
"eslintConfig": {
|
|
36
36
|
"parserOptions": {
|
|
@@ -59,5 +59,5 @@
|
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
},
|
|
62
|
-
"packageManager": "yarn@3.
|
|
62
|
+
"packageManager": "yarn@3.2.1"
|
|
63
63
|
}
|