Skip to content

Commit 4f833e1

Browse files
committed
typescript based code
1 parent 1dd4dae commit 4f833e1

File tree

9 files changed

+8617
-1036
lines changed

9 files changed

+8617
-1036
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ coverage/
77
*.log
88
.npm
99

10-
.DS_Store
10+
.DS_Store

README.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,29 @@ module.exports = {
6565
}
6666
```
6767

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

70-
```diff
71-
module.export = {
70+
```javascript
71+
72+
module.exports = {
7273
plugins: [
7374
require('cssnano'),
74-
+ require('postcss-variable-compress')([
75-
+ '--colorPrimary',
76-
+ '--colorPrimaryAlpha',
77-
+ '--light',
78-
+ '--dark',
79-
+ '--font',
80-
+ '--vh',
81-
+ '--r'
82-
+ ])
75+
require('postcss-variable-compress')(
76+
// pass in css variables to avoid
77+
'colorPrimary', 'colorPrimaryAlpha', 'light', 'dark', 'font', 'vh', 'r'
78+
// or pass in as many function as your want
79+
// they take single param which is a the name of css variable
80+
// you can do checks on it and
81+
// return true if you want it to be skipped
82+
// for example
83+
(name) => name.includes('skip')
84+
// avoid regex if you can they are bad
85+
)
8386
]
8487
}
88+
8589
```
8690

91+
92+
8793
[official docs]: https://github.com/postcss/postcss#usage

index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export declare namespace variableCompress {
2+
type skip = (variableName: string) => boolean | undefined;
3+
type parameters = skip | string;
4+
}

index.js

Lines changed: 77 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,91 @@
1-
/**
2-
* @param {string[]} opts prefix your input with -- like any css variable
3-
*/
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;
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const postcssPlugin = 'postcss-variable-compress';
4+
let processed = Symbol('processed');
5+
let renamedVariables = [];
6+
let cssVariables = -1;
7+
let pureSkips = [];
8+
let scriptBasedSkips = [];
9+
let cssVariablesMap = new Map();
10+
function scriptCheck(val) {
11+
const should = scriptBasedSkips.findIndex(E => E(val));
12+
return (should > -1);
13+
}
14+
function shouldRun(val) {
15+
let should = true;
16+
if (renamedVariables.indexOf(val) > -1) {
17+
should = false;
1818
}
19-
return true;
20-
}
21-
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-
}
19+
if (should && scriptCheck(val)) {
20+
should = false;
21+
}
22+
return should;
23+
}
24+
function increase() {
25+
cssVariables = cssVariables + 1;
26+
let temp = cssVariables.toString(36);
27+
pureSkips.forEach(E => {
28+
if (E === temp) {
29+
temp = cssVariables.toString(36);
30+
cssVariables = cssVariables + 1;
31+
}
3032
});
31-
}
32-
33-
/**
34-
* @param {string} match
35-
*/
36-
function replacer (match) {
37-
38-
if (!shouldRun(match)) return match;
39-
let exist = VariablesMap.get(match);
40-
33+
}
34+
function replacer(match) {
35+
if (!shouldRun(match))
36+
return match;
37+
let exist = cssVariablesMap.get(match);
4138
if (!exist) {
42-
exist = '--' + (Variables).toString(36);
43-
increase();
44-
VariablesMap.set(match, exist);
45-
History.push(exist);
39+
exist = '--' + (cssVariables).toString(36);
40+
increase();
41+
cssVariablesMap.set(match, exist);
42+
renamedVariables.push(exist);
4643
}
47-
4844
return exist;
49-
}
50-
51-
/**
52-
* @param {import('postcss').Declaration} j
53-
*/
54-
function map (j) {
55-
45+
}
46+
function map(j) {
5647
let prop = j.prop;
5748
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;
49+
let old = prop;
50+
let exist = cssVariablesMap.get(old);
51+
if (!exist) {
52+
exist = '--' + cssVariables.toString(36);
53+
increase();
54+
cssVariablesMap.set(old, exist);
55+
renamedVariables.push(exist);
56+
}
57+
prop = exist;
58+
j.prop = prop;
6859
}
69-
7060
let value = j.value;
7161
if (value && value.includes('var(--') && value.length <= 1000) {
72-
value = value.replace(/--[\w-_]{1,1000}/g, replacer);
73-
j.value = value;
62+
value = value.replace(/--[\w-_]{1,1000}/g, replacer);
63+
j.value = value;
7464
}
75-
65+
// @ts-ignore
7666
j[processed] = true;
77-
78-
}
79-
80-
opts.forEach(E => VariablesMap.set(E, E));
81-
opts.forEach(E => Skips.push(E.replace('--', '')));
82-
83-
increase();
84-
85-
return {
86-
postcssPlugin: 'postcss-variable-compress',
87-
Declaration: {
88-
'*': map
89-
}
90-
};
67+
}
68+
module.exports = function variableCompress(opts) {
69+
processed = Symbol('processed');
70+
renamedVariables = [];
71+
cssVariables = -1;
72+
pureSkips = [];
73+
scriptBasedSkips = [];
74+
cssVariablesMap = new Map();
75+
opts.forEach(E => {
76+
if (typeof E === 'string') {
77+
pureSkips.push(E.replace('--', ''));
78+
cssVariablesMap.set(E, E);
79+
}
80+
else
81+
scriptBasedSkips.push(E);
82+
});
83+
increase();
84+
return {
85+
postcssPlugin,
86+
Declaration: {
87+
'*': map
88+
}
89+
};
9190
};
92-
9391
module.exports.postcss = true;

index.test.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
const postcss = require('postcss');
22

3-
const plugin = require('./index');
3+
const variableCompress = require('./index');
4+
5+
async function run (input, output, opts) {
6+
let result = await postcss(
7+
[variableCompress(opts)]
8+
).process(input, { from: undefined });
49

5-
async function run (input, output, opts = []) {
6-
let result = await postcss([plugin(opts)]).process(input, { from: undefined });
710
expect(result.css).toEqual(output);
811
expect(result.warnings()).toHaveLength(0);
12+
913
}
1014

1115

@@ -38,6 +42,10 @@ it('Shorten css variables', async () => {
3842
3943
.section-title {
4044
color: var(--primary-color, var(--black, #222));
45+
}
46+
47+
code {
48+
--5: #555;
4149
}`,
4250
`:root {
4351
--0: #16f;
@@ -66,15 +74,21 @@ it('Shorten css variables', async () => {
6674
6775
.section-title {
6876
color: var(--primary-color, var(--3, #222));
77+
}
78+
79+
code {
80+
--5: #555;
6981
}`,
7082
[
7183
'--primary-color',
72-
'--2'
84+
'--2',
85+
(e) => e.includes('special'),
86+
(e) => e === '--5'
7387
]
7488
);
7589
});
7690

7791
it('Support reloading. Now the plugin will reset mapped variables', async () => {
7892
await run(`:root{--first-color: #16f;--second-color: #ff7;}`, `:root{--0: #16f;--1: #ff7;}`, []);
7993
await run(`:root{--second-color: #ff7;--first-color: #16f;}`, `:root{--0: #ff7;--1: #16f;}`, []);
80-
});
94+
});

0 commit comments

Comments
 (0)