@lesjoursfr/postcss-extract-css-variables 1.0.3 → 1.0.4
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/.eslintrc.js +15 -0
- package/.prettierignore +9 -0
- package/.prettierrc.js +12 -0
- package/README.md +17 -14
- package/index.js +12 -17
- package/package.json +15 -28
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/* eslint-disable no-undef */
|
|
2
|
+
module.exports = {
|
|
3
|
+
parserOptions: {
|
|
4
|
+
ecmaVersion: 2017,
|
|
5
|
+
},
|
|
6
|
+
env: {
|
|
7
|
+
node: true,
|
|
8
|
+
es6: true,
|
|
9
|
+
},
|
|
10
|
+
extends: ["eslint:recommended", "plugin:jest/recommended"],
|
|
11
|
+
rules: {
|
|
12
|
+
"jest/expect-expect": "off",
|
|
13
|
+
semi: ["error", "always"],
|
|
14
|
+
},
|
|
15
|
+
};
|
package/.prettierignore
ADDED
package/.prettierrc.js
ADDED
package/README.md
CHANGED
|
@@ -1,33 +1,36 @@
|
|
|
1
1
|
[](https://badge.fury.io/js/@lesjoursfr%2Fpostcss-extract-css-variables)
|
|
2
|
+
[](https://github.com/lesjoursfr/postcss-extract-css-variables/actions/workflows/quality-control.yml)
|
|
3
|
+
|
|
4
|
+
# postcss-extract-css-variables
|
|
2
5
|
|
|
3
|
-
postcss-extract-css-variables
|
|
4
|
-
================
|
|
5
6
|
[PostCSS] plugin to extract rules with CSS variables.
|
|
6
7
|
|
|
7
|
-
[
|
|
8
|
+
[postcss]: https://github.com/postcss/postcss
|
|
8
9
|
|
|
9
10
|
```css
|
|
10
11
|
/* Input example */
|
|
11
12
|
.lj-color-35 {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
--lj-color-main: #fda92a;
|
|
14
|
+
--lj-color-alpha: rgba(253, 169, 42, 0.75);
|
|
15
|
+
--lj-color-light: #fdde2a;
|
|
16
|
+
--lj-color-mixed: #fdc42a;
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
.selector-1 {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
background-color: var(--lj-color-main);
|
|
21
|
+
content: "";
|
|
22
|
+
display: inline-block;
|
|
23
|
+
height: 5px;
|
|
24
|
+
margin-right: 10px;
|
|
25
|
+
width: 18px;
|
|
25
26
|
}
|
|
26
27
|
```
|
|
27
28
|
|
|
28
29
|
```css
|
|
29
30
|
/* Output example */
|
|
30
|
-
.lj-color-35 .selector-1 {
|
|
31
|
+
.lj-color-35 .selector-1 {
|
|
32
|
+
background-color: #fda92a;
|
|
33
|
+
}
|
|
31
34
|
```
|
|
32
35
|
|
|
33
36
|
## Usage
|
package/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const os = require(
|
|
2
|
-
const fs = require(
|
|
1
|
+
const os = require("os");
|
|
2
|
+
const fs = require("fs");
|
|
3
3
|
const CSS_VARIABLE_DECLARATION = /^--/;
|
|
4
4
|
const CSS_VARIABLE_USE = /(var\([a-zA-Z0-9-]+\))/;
|
|
5
5
|
const HTML_NODE = /^html([#.:[][#.:_[\]\-a-zA-Z0-9]+)?(?:\s|$)/;
|
|
@@ -17,11 +17,11 @@ class CSSVariable {
|
|
|
17
17
|
|
|
18
18
|
wrapAndReplace(selector, prop, value, important) {
|
|
19
19
|
// Get new selector & value
|
|
20
|
-
const newSelector = this._mixSelectors(this.selector, selector.split(
|
|
20
|
+
const newSelector = this._mixSelectors(this.selector, selector.split(",")).join(",");
|
|
21
21
|
const newValue = value.replaceAll(`var(${this.name})`, this.value);
|
|
22
22
|
|
|
23
23
|
// Return the rule
|
|
24
|
-
return
|
|
24
|
+
return important === true
|
|
25
25
|
? `${newSelector} { ${prop}: ${newValue}!important; }`
|
|
26
26
|
: `${newSelector} { ${prop}: ${newValue}; }`;
|
|
27
27
|
}
|
|
@@ -32,9 +32,9 @@ class CSSVariable {
|
|
|
32
32
|
const varHtmlMatch = HTML_NODE.exec(varSelector);
|
|
33
33
|
const ruleHtmlMatch = HTML_NODE.exec(ruleSelector);
|
|
34
34
|
if (varHtmlMatch !== null) {
|
|
35
|
-
return `html${varHtmlMatch[1] ||
|
|
35
|
+
return `html${varHtmlMatch[1] || ""}${ruleHtmlMatch[1] || ""} ${ruleSelector.replace(HTML_NODE, "").trim()}`;
|
|
36
36
|
} else {
|
|
37
|
-
return ruleSelector.replace(`html${ruleHtmlMatch[1] ||
|
|
37
|
+
return ruleSelector.replace(`html${ruleHtmlMatch[1] || ""}`, `html${ruleHtmlMatch[1] || ""} ${varSelector}`);
|
|
38
38
|
}
|
|
39
39
|
} else {
|
|
40
40
|
return `${varSelector} ${ruleSelector.trim()}`;
|
|
@@ -50,12 +50,12 @@ module.exports = (opts = {}) => {
|
|
|
50
50
|
// Check if we have an output path
|
|
51
51
|
if (opts.output === undefined) {
|
|
52
52
|
// Throw an error if we haven't an output parameter
|
|
53
|
-
throw new Error(
|
|
53
|
+
throw new Error("Missing output parameter");
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
// Return the PostCSS plugin
|
|
57
57
|
return {
|
|
58
|
-
postcssPlugin:
|
|
58
|
+
postcssPlugin: "postcss-extract-css-variables",
|
|
59
59
|
|
|
60
60
|
Root(root) {
|
|
61
61
|
let cssVariablesHolder = [];
|
|
@@ -63,7 +63,7 @@ module.exports = (opts = {}) => {
|
|
|
63
63
|
// Look for CSS variable declaration
|
|
64
64
|
for (const rule of root.nodes) {
|
|
65
65
|
// Check the type of node
|
|
66
|
-
if (rule.type ===
|
|
66
|
+
if (rule.type === "atrule" || rule.type === "comment") {
|
|
67
67
|
// Skip this rule
|
|
68
68
|
continue;
|
|
69
69
|
}
|
|
@@ -83,7 +83,7 @@ module.exports = (opts = {}) => {
|
|
|
83
83
|
// Look for CSS variable use
|
|
84
84
|
for (const rule of root.nodes) {
|
|
85
85
|
// Check the type of node
|
|
86
|
-
if (rule.type ===
|
|
86
|
+
if (rule.type === "atrule" || rule.type === "comment") {
|
|
87
87
|
// Skip this rule
|
|
88
88
|
continue;
|
|
89
89
|
}
|
|
@@ -94,12 +94,7 @@ module.exports = (opts = {}) => {
|
|
|
94
94
|
for (const cssVariable of cssVariablesHolder) {
|
|
95
95
|
if (cssVariable.isUsed(declaration.value)) {
|
|
96
96
|
generatedRules.push(
|
|
97
|
-
cssVariable.wrapAndReplace(
|
|
98
|
-
rule.selector,
|
|
99
|
-
declaration.prop,
|
|
100
|
-
declaration.value,
|
|
101
|
-
declaration.important
|
|
102
|
-
)
|
|
97
|
+
cssVariable.wrapAndReplace(rule.selector, declaration.prop, declaration.value, declaration.important)
|
|
103
98
|
);
|
|
104
99
|
}
|
|
105
100
|
}
|
|
@@ -108,7 +103,7 @@ module.exports = (opts = {}) => {
|
|
|
108
103
|
}
|
|
109
104
|
|
|
110
105
|
// Write the generated rules to the output file
|
|
111
|
-
fs.writeFileSync(opts.output, generatedRules.join(os.EOL), { encoding:
|
|
106
|
+
fs.writeFileSync(opts.output, generatedRules.join(os.EOL), { encoding: "utf8", mode: 0o666, flag: "w" });
|
|
112
107
|
},
|
|
113
108
|
};
|
|
114
109
|
};
|
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.4",
|
|
4
4
|
"description": "PostCSS plugin to extract rules with CSS variables",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "lesjoursfr/postcss-extract-css-variables",
|
|
@@ -18,39 +18,26 @@
|
|
|
18
18
|
"postcss-extract-css-variables"
|
|
19
19
|
],
|
|
20
20
|
"scripts": {
|
|
21
|
-
"
|
|
21
|
+
"freshlock": "rm -rf node_modules/ && rm .yarn/install-state.gz && rm -r .yarn/cache/ && rm yarn.lock && yarn",
|
|
22
|
+
"check-lint": "eslint . --ext .js,.jsx,.ts,.tsx",
|
|
23
|
+
"check-format": "prettier --check .",
|
|
24
|
+
"lint": "eslint . --fix --ext .js,.jsx,.ts,.tsx",
|
|
25
|
+
"format": "prettier --write .",
|
|
26
|
+
"test": "jest --coverage"
|
|
22
27
|
},
|
|
23
28
|
"engines": {
|
|
24
|
-
"node": ">=
|
|
29
|
+
"node": ">=16.15.1 || >=18.4.0"
|
|
25
30
|
},
|
|
26
31
|
"peerDependencies": {
|
|
27
32
|
"postcss": "^8.4.14"
|
|
28
33
|
},
|
|
29
34
|
"devDependencies": {
|
|
30
|
-
"eslint": "^8.
|
|
31
|
-
"eslint-
|
|
32
|
-
"jest": "^
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"parserOptions": {
|
|
37
|
-
"ecmaVersion": 2017
|
|
38
|
-
},
|
|
39
|
-
"env": {
|
|
40
|
-
"node": true,
|
|
41
|
-
"es6": true
|
|
42
|
-
},
|
|
43
|
-
"extends": [
|
|
44
|
-
"eslint:recommended",
|
|
45
|
-
"plugin:jest/recommended"
|
|
46
|
-
],
|
|
47
|
-
"rules": {
|
|
48
|
-
"jest/expect-expect": "off",
|
|
49
|
-
"semi": [
|
|
50
|
-
"error",
|
|
51
|
-
"always"
|
|
52
|
-
]
|
|
53
|
-
}
|
|
35
|
+
"eslint": "^8.21.0",
|
|
36
|
+
"eslint-config-prettier": "^8.5.0",
|
|
37
|
+
"eslint-plugin-jest": "^26.7.0",
|
|
38
|
+
"jest": "^28.1.3",
|
|
39
|
+
"postcss": "^8.4.14",
|
|
40
|
+
"prettier": "^2.7.1"
|
|
54
41
|
},
|
|
55
42
|
"jest": {
|
|
56
43
|
"coverageThreshold": {
|
|
@@ -59,5 +46,5 @@
|
|
|
59
46
|
}
|
|
60
47
|
}
|
|
61
48
|
},
|
|
62
|
-
"packageManager": "yarn@3.2.
|
|
49
|
+
"packageManager": "yarn@3.2.2"
|
|
63
50
|
}
|