@lesjoursfr/postcss-extract-css-variables 1.0.0
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 +7 -0
- package/LICENSE +20 -0
- package/README.md +59 -0
- package/index.js +88 -0
- package/package.json +63 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright 2022 Adrien ERAUD <adrien@lebondrive.fr>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
[](https://badge.fury.io/js/@lesjoursfr%2Fpostcss-extract-css-variables)
|
|
2
|
+
|
|
3
|
+
postcss-extract-css-variables
|
|
4
|
+
================
|
|
5
|
+
[PostCSS] plugin to extract rules with CSS variables.
|
|
6
|
+
|
|
7
|
+
[PostCSS]: https://github.com/postcss/postcss
|
|
8
|
+
|
|
9
|
+
```css
|
|
10
|
+
/* Input example */
|
|
11
|
+
.lj-color-35 {
|
|
12
|
+
--lj-color-main: #fda92a;
|
|
13
|
+
--lj-color-alpha: rgba(253,169,42,.75);
|
|
14
|
+
--lj-color-light: #fdde2a;
|
|
15
|
+
--lj-color-mixed: #fdc42a
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
.selector-1 {
|
|
19
|
+
background-color: var(--lj-color-main);
|
|
20
|
+
content: "";
|
|
21
|
+
display: inline-block;
|
|
22
|
+
height: 5px;
|
|
23
|
+
margin-right: 10px;
|
|
24
|
+
width: 18px
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```css
|
|
29
|
+
/* Output example */
|
|
30
|
+
.lj-color-35 .selector-1 { background-color: #fda92a; }
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Usage
|
|
34
|
+
|
|
35
|
+
**Step 1:** Install plugin:
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
npm install --save-dev postcss postcss-extract-css-variables
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
**Step 2:** Check you project for existed PostCSS config: `postcss.config.js`
|
|
42
|
+
in the project root, `"postcss"` section in `package.json`
|
|
43
|
+
or `postcss` in bundle config.
|
|
44
|
+
|
|
45
|
+
If you do not use PostCSS, add it according to [official docs]
|
|
46
|
+
and set this plugin in settings.
|
|
47
|
+
|
|
48
|
+
**Step 3:** Add the plugin add the end of the plugins list:
|
|
49
|
+
|
|
50
|
+
```diff
|
|
51
|
+
module.exports = {
|
|
52
|
+
plugins: [
|
|
53
|
+
require('autoprefixer'),
|
|
54
|
+
+ require('postcss-extract-css-variables')({ output: './output.css' })
|
|
55
|
+
]
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
[official docs]: https://github.com/postcss/postcss#usage
|
package/index.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
const os = require('os');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const CSS_VARIABLE_DECLARATION = /^--/;
|
|
4
|
+
const CSS_VARIABLE_USE = /(var\([a-zA-Z0-9-]+\))/;
|
|
5
|
+
|
|
6
|
+
class CSSVariable {
|
|
7
|
+
constructor(name, selector, value) {
|
|
8
|
+
this.name = name;
|
|
9
|
+
this.selector = selector;
|
|
10
|
+
this.value = value;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
isUsed(value) {
|
|
14
|
+
return value.includes(`var(${this.name})`);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
wrapAndReplace(selector, prop, value) {
|
|
18
|
+
let newSelector = selector.split(',').map((el) => `${this.selector} ${el.trim()}`).join(',');
|
|
19
|
+
let newValue = value.replaceAll(`var(${this.name})`, this.value);
|
|
20
|
+
|
|
21
|
+
return `${newSelector} { ${prop}: ${newValue}; }`;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @type {import('postcss').PluginCreator}
|
|
27
|
+
*/
|
|
28
|
+
module.exports = (opts = {}) => {
|
|
29
|
+
// Check if we have an output path
|
|
30
|
+
if (opts.output === undefined) {
|
|
31
|
+
// Throw an error if we haven't an output parameter
|
|
32
|
+
throw new Error('Missing output parameter');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Return the PostCSS plugin
|
|
36
|
+
return {
|
|
37
|
+
postcssPlugin: 'postcss-extract-css-variables',
|
|
38
|
+
|
|
39
|
+
Root(root) {
|
|
40
|
+
let cssVariablesHolder = [];
|
|
41
|
+
|
|
42
|
+
// Look for CSS variable declaration
|
|
43
|
+
for (const rule of root.nodes) {
|
|
44
|
+
// Check the type of node
|
|
45
|
+
if (rule.type === 'atrule' || rule.type === 'comment') {
|
|
46
|
+
// Skip this rule
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Check nodes
|
|
51
|
+
for (const declaration of rule.nodes) {
|
|
52
|
+
if (CSS_VARIABLE_DECLARATION.test(declaration.prop)) {
|
|
53
|
+
// Add the CSS variable
|
|
54
|
+
cssVariablesHolder.push(new CSSVariable(declaration.prop, rule.selector, declaration.value));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Open a stream for the destination file
|
|
60
|
+
const generatedRules = [];
|
|
61
|
+
|
|
62
|
+
// Look for CSS variable use
|
|
63
|
+
for (const rule of root.nodes) {
|
|
64
|
+
// Check the type of node
|
|
65
|
+
if (rule.type === 'atrule' || rule.type === 'comment') {
|
|
66
|
+
// Skip this rule
|
|
67
|
+
continue;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Check nodes
|
|
71
|
+
for (const declaration of rule.nodes) {
|
|
72
|
+
if (CSS_VARIABLE_USE.test(declaration.value)) {
|
|
73
|
+
for (const cssVariable of cssVariablesHolder) {
|
|
74
|
+
if (cssVariable.isUsed(declaration.value)) {
|
|
75
|
+
generatedRules.push(cssVariable.wrapAndReplace(rule.selector, declaration.prop, declaration.value));
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Write the generated rules to the output file
|
|
83
|
+
fs.writeFileSync(opts.output, generatedRules.join(os.EOL), { encoding: 'utf8', mode: 0o666, flag: 'w' });
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
module.exports.postcss = true;
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lesjoursfr/postcss-extract-css-variables",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "PostCSS plugin to extract rules with CSS variables",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": "lesjoursfr/postcss-extract-css-variables",
|
|
7
|
+
"homepage": "https://github.com/lesjoursfr/postcss-extract-css-variables#readme",
|
|
8
|
+
"bugs": {
|
|
9
|
+
"url": "https://github.com/lesjoursfr/postcss-extract-css-variables/issues"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"postcss",
|
|
16
|
+
"css",
|
|
17
|
+
"postcss-plugin",
|
|
18
|
+
"postcss-extract-css-variables"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"test": "jest --coverage && eslint ."
|
|
22
|
+
},
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=16"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"postcss": "^8.4.5"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"eslint": "^8.7.0",
|
|
31
|
+
"eslint-plugin-jest": "^25.7.0",
|
|
32
|
+
"jest": "^27.4.7",
|
|
33
|
+
"postcss": "^8.4.5"
|
|
34
|
+
},
|
|
35
|
+
"eslintConfig": {
|
|
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
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"jest": {
|
|
56
|
+
"coverageThreshold": {
|
|
57
|
+
"global": {
|
|
58
|
+
"statements": 100
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
"packageManager": "yarn@3.1.1"
|
|
63
|
+
}
|