Skip to content

Commit ac3e758

Browse files
committed
Re loading fix
Now the plugin should work properly when PostCSS detects configuration change
1 parent 126e3a4 commit ac3e758

File tree

3 files changed

+74
-62
lines changed

3 files changed

+74
-62
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# PostCSS Variable Compress
22

3-
[postcss-variable-compress] is a [PostCSS] plugin minifies variable names and saves space. It can safely convert for css variable fallback cases as well. It replaces css variables via a counter and converts that counter into base 36 string as javascript inherent implementation is limited to this but beyond such no one would notice a difference as right now even if you have 1295 css variables they will not exceed two characters. It will transform any css variable it encounters without breaking your stylesheet. If you want it doesn't modify some css variables pass them as an array
3+
[postcss-variable-compress] is a [PostCSS] plugin minifies variable names and saves space. Even if you have 1295 css variables still they will not exceed by two characters. It will transform css variable without breaking your stylesheet.
44

5-
[PostCSS]: https://github.com/postcss/postcss
5+
If you want it not modify some css variables, then pass them `--{variable-name}` as an array to the plugin.
6+
7+
[postcss]: https://github.com/postcss/postcss
68
[postcss-variable-compress]: https://github.com/navanshu/postcss-variable-compress
79

810
```css
@@ -63,7 +65,7 @@ module.exports = {
6365
}
6466
```
6567

66-
**Step 4:** Pass configuration to the plugin. It takes an array of css variables that you don't need to transform:
68+
**Step 4:** Pass configuration to the plugin. It takes an array of css variables that you don't need to transform.
6769

6870
```diff
6971
module.export = {
@@ -78,7 +80,7 @@ module.export = {
7880
+ '--vh',
7981
+ '--r'
8082
+ ])
81-
]
83+
]
8284
}
8385
```
8486

index.js

Lines changed: 62 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,81 @@
1-
const VariablesMap = new Map();
2-
const History = [];
3-
let Variables = -1;
4-
let Skips = [];
51
/**
6-
* @param {string} val
2+
* @param {string[]} opts prefix your input with -- like any css variable
73
*/
8-
function shouldRun (val) {
9-
if (History.indexOf(val) > -1) {
10-
return false;
11-
}
12-
return true;
13-
}
14-
15-
function increase () {
16-
Variables = Variables + 1;
17-
let temp = Variables.toString(36);
18-
Skips.forEach(E => {
19-
if (E === temp) {
20-
temp = Variables.toString(36);
21-
Variables = Variables + 1;
4+
module.exports = (opts = []) => {
5+
const VariablesMap = new Map();
6+
7+
const processed = Symbol('processed');
8+
9+
const History = [];
10+
let Variables = -1;
11+
let Skips = [];
12+
/**
13+
* @param {string} val
14+
*/
15+
function shouldRun (val) {
16+
if (History.indexOf(val) > -1) {
17+
return false;
2218
}
23-
});
24-
}
25-
26-
/**
27-
* @param {string} match
28-
*/
29-
function replacer (match) {
30-
31-
if (!shouldRun(match)) return match;
32-
let exist = VariablesMap.get(match);
19+
return true;
20+
}
3321

34-
if (!exist) {
35-
exist = '--' + (Variables).toString(36);
36-
increase();
37-
VariablesMap.set(match, exist);
38-
History.push(exist);
22+
function increase () {
23+
Variables = Variables + 1;
24+
let temp = Variables.toString(36);
25+
Skips.forEach(E => {
26+
if (E === temp) {
27+
temp = Variables.toString(36);
28+
Variables = Variables + 1;
29+
}
30+
});
3931
}
4032

41-
return exist;
42-
}
33+
/**
34+
* @param {string} match
35+
*/
36+
function replacer (match) {
4337

44-
/**
45-
* @param {import('postcss').Declaration} j
46-
*/
47-
function looker (j) {
38+
if (!shouldRun(match)) return match;
39+
let exist = VariablesMap.get(match);
4840

49-
let prop = j.prop;
50-
51-
if (j.variable && shouldRun(prop)) {
52-
let old = prop;
53-
let exist = VariablesMap.get(old);
5441
if (!exist) {
55-
exist = '--' + Variables.toString(36);
42+
exist = '--' + (Variables).toString(36);
5643
increase();
57-
VariablesMap.set(old, exist);
44+
VariablesMap.set(match, exist);
5845
History.push(exist);
5946
}
60-
prop = exist;
47+
48+
return exist;
6149
}
6250

63-
let value = j.value.replace(/--[\w-_]{1,1000}/g, replacer);
51+
/**
52+
* @param {import('postcss').Declaration} j
53+
*/
54+
function map (j) {
55+
56+
let prop = j.prop;
57+
if (prop && j.variable && shouldRun(prop)) {
58+
let old = prop;
59+
let exist = VariablesMap.get(old);
60+
if (!exist) {
61+
exist = '--' + Variables.toString(36);
62+
increase();
63+
VariablesMap.set(old, exist);
64+
History.push(exist);
65+
}
66+
prop = exist;
67+
j.prop = prop;
68+
}
6469

65-
if (prop) j.prop = prop;
66-
if (value) j.value = value;
70+
let value = j.value;
71+
if (value && value.includes('var(--') && value.length <= 1000) {
72+
value = value.replace(/--[\w-_]{1,1000}/g, replacer);
73+
j.value = value;
74+
}
6775

68-
}
76+
j[processed] = true;
6977

70-
/**
71-
* @param {string[]} opts
72-
*/
73-
module.exports = (opts = []) => {
78+
}
7479

7580
opts.forEach(E => VariablesMap.set(E, E));
7681
opts.forEach(E => Skips.push(E.replace('--', '')));
@@ -80,7 +85,7 @@ module.exports = (opts = []) => {
8085
return {
8186
postcssPlugin: 'postcss-variable-compress',
8287
Declaration: {
83-
'*': looker
88+
'*': map
8489
}
8590
};
8691
};

index.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const postcss = require('postcss');
22

33
const plugin = require('./index');
44

5-
async function run (input, output, opts = {}) {
5+
async function run (input, output, opts = []) {
66
let result = await postcss([plugin(opts)]).process(input, { from: undefined });
77
expect(result.css).toEqual(output);
88
expect(result.warnings()).toHaveLength(0);
@@ -72,4 +72,9 @@ it('Shorten css variables', async () => {
7272
'--2'
7373
]
7474
);
75+
});
76+
77+
it('Support reloading. Now the plugin will reset mapped variables', async () => {
78+
await run(`:root{--first-color: #16f;--second-color: #ff7;}`, `:root{--0: #16f;--1: #ff7;}`, []);
79+
await run(`:root{--second-color: #ff7;--first-color: #16f;}`, `:root{--0: #ff7;--1: #16f;}`, []);
7580
});

0 commit comments

Comments
 (0)