Skip to content

Commit d004d4b

Browse files
committed
Update
1 parent c3cca1d commit d004d4b

File tree

7 files changed

+1208
-1231
lines changed

7 files changed

+1208
-1231
lines changed

index.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,11 @@ export declare namespace variableCompress {
22
type skip = (variableName: string) => boolean | undefined;
33
type parameters = skip | string;
44
}
5+
declare function map(j: import("postcss").Declaration): void;
6+
export declare function variableCompress(opts?: variableCompress.parameters[]): {
7+
postcssPlugin: string;
8+
Declaration: {
9+
"*": typeof map;
10+
};
11+
};
12+
export {};

index.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
const postcssPlugin = 'postcss-variable-compress';
4-
let processed = Symbol('processed');
3+
exports.variableCompress = void 0;
4+
const postcssPlugin = "postcss-variable-compress";
5+
let processed = Symbol("processed");
56
let renamedVariables = [];
67
let cssVariables = -1;
78
let pureSkips = [];
89
let scriptBasedSkips = [];
910
let cssVariablesMap = new Map();
1011
function scriptCheck(val) {
11-
const should = scriptBasedSkips.findIndex(E => E(val));
12-
return (should > -1);
12+
const should = scriptBasedSkips.findIndex((E) => E(val));
13+
return should > -1;
1314
}
1415
function shouldRun(val) {
1516
let should = true;
@@ -24,7 +25,7 @@ function shouldRun(val) {
2425
function increase() {
2526
cssVariables = cssVariables + 1;
2627
let temp = cssVariables.toString(36);
27-
pureSkips.forEach(E => {
28+
pureSkips.forEach((E) => {
2829
if (E === temp) {
2930
temp = cssVariables.toString(36);
3031
cssVariables = cssVariables + 1;
@@ -36,7 +37,7 @@ function replacer(match) {
3637
return match;
3738
let exist = cssVariablesMap.get(match);
3839
if (!exist) {
39-
exist = '--' + (cssVariables).toString(36);
40+
exist = "--" + cssVariables.toString(36);
4041
increase();
4142
cssVariablesMap.set(match, exist);
4243
renamedVariables.push(exist);
@@ -49,7 +50,7 @@ function map(j) {
4950
let old = prop;
5051
let exist = cssVariablesMap.get(old);
5152
if (!exist) {
52-
exist = '--' + cssVariables.toString(36);
53+
exist = "--" + cssVariables.toString(36);
5354
increase();
5455
cssVariablesMap.set(old, exist);
5556
renamedVariables.push(exist);
@@ -58,29 +59,29 @@ function map(j) {
5859
j.prop = prop;
5960
}
6061
let value = j.value;
61-
if (value && value.includes('var(--') && value.length <= 1000) {
62+
if (value && value.includes("var(--") && value.length <= 1000) {
6263
value = value.replace(/--[\w-_]{1,1000}/g, replacer);
6364
j.value = value;
6465
}
6566
// @ts-ignore
6667
j[processed] = true;
6768
}
68-
module.exports = function variableCompress(opts) {
69-
processed = Symbol('processed');
69+
function variableCompress(opts) {
70+
processed = Symbol("processed");
7071
renamedVariables = [];
7172
cssVariables = -1;
7273
pureSkips = [];
7374
scriptBasedSkips = [];
7475
cssVariablesMap = new Map();
75-
opts === null || opts === void 0 ? void 0 : opts.forEach(E => {
76-
if (typeof E === 'string') {
76+
opts === null || opts === void 0 ? void 0 : opts.forEach((E) => {
77+
if (typeof E === "string") {
7778
let name = E;
7879
let cssName = E;
79-
if (E.slice(0, 2) === '--') {
80+
if (E.slice(0, 2) === "--") {
8081
name = E.slice(2);
8182
}
8283
else {
83-
cssName = '--' + E;
84+
cssName = "--" + E;
8485
}
8586
pureSkips.push(name);
8687
cssVariablesMap.set(cssName, cssName);
@@ -92,8 +93,10 @@ module.exports = function variableCompress(opts) {
9293
return {
9394
postcssPlugin,
9495
Declaration: {
95-
'*': map
96-
}
96+
"*": map,
97+
},
9798
};
98-
};
99+
}
100+
exports.variableCompress = variableCompress;
101+
module.exports = variableCompress;
99102
module.exports.postcss = true;

index.ts

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
export namespace variableCompress {
2-
32
export type skip = (variableName: string) => boolean | undefined;
43

54
export type parameters = skip | string;
6-
75
}
8-
const postcssPlugin = 'postcss-variable-compress';
6+
const postcssPlugin = "postcss-variable-compress";
97

10-
let processed = Symbol('processed');
8+
let processed = Symbol("processed");
119
let renamedVariables: string[] = [];
1210
let cssVariables = -1;
1311
let pureSkips: string[] = [];
1412
let scriptBasedSkips: variableCompress.skip[] = [];
1513
let cssVariablesMap = new Map();
1614

1715
function scriptCheck(val: string) {
18-
19-
const should = scriptBasedSkips.findIndex(E => E(val));
20-
return (should > -1);
21-
16+
const should = scriptBasedSkips.findIndex((E) => E(val));
17+
return should > -1;
2218
}
2319

2420
function shouldRun(val: string) {
25-
2621
let should = true;
2722

2823
if (renamedVariables.indexOf(val) > -1) {
@@ -37,27 +32,24 @@ function shouldRun(val: string) {
3732
}
3833

3934
function increase() {
40-
4135
cssVariables = cssVariables + 1;
4236

4337
let temp = cssVariables.toString(36);
4438

45-
pureSkips.forEach(E => {
39+
pureSkips.forEach((E) => {
4640
if (E === temp) {
4741
temp = cssVariables.toString(36);
4842
cssVariables = cssVariables + 1;
4943
}
5044
});
51-
5245
}
5346

5447
function replacer(match: string) {
55-
5648
if (!shouldRun(match)) return match;
5749
let exist = cssVariablesMap.get(match);
5850

5951
if (!exist) {
60-
exist = '--' + (cssVariables).toString(36);
52+
exist = "--" + cssVariables.toString(36);
6153
increase();
6254
cssVariablesMap.set(match, exist);
6355
renamedVariables.push(exist);
@@ -66,14 +58,13 @@ function replacer(match: string) {
6658
return exist;
6759
}
6860

69-
function map(j: import('postcss').Declaration) {
70-
61+
function map(j: import("postcss").Declaration) {
7162
let prop = j.prop;
7263
if (prop && j.variable && shouldRun(prop)) {
7364
let old = prop;
7465
let exist = cssVariablesMap.get(old);
7566
if (!exist) {
76-
exist = '--' + cssVariables.toString(36);
67+
exist = "--" + cssVariables.toString(36);
7768
increase();
7869
cssVariablesMap.set(old, exist);
7970
renamedVariables.push(exist);
@@ -83,54 +74,49 @@ function map(j: import('postcss').Declaration) {
8374
}
8475

8576
let value = j.value;
86-
if (value && value.includes('var(--') && value.length <= 1000) {
77+
if (value && value.includes("var(--") && value.length <= 1000) {
8778
value = value.replace(/--[\w-_]{1,1000}/g, replacer);
8879
j.value = value;
8980
}
9081

9182
// @ts-ignore
9283
j[processed] = true;
93-
9484
}
9585

96-
97-
module.exports = function variableCompress(opts?: variableCompress.parameters[]) {
98-
99-
processed = Symbol('processed');
86+
export function variableCompress(opts?: variableCompress.parameters[]) {
87+
processed = Symbol("processed");
10088

10189
renamedVariables = [];
10290
cssVariables = -1;
10391
pureSkips = [];
10492
scriptBasedSkips = [];
10593
cssVariablesMap = new Map();
10694

107-
opts?.forEach(E => {
108-
if (typeof E === 'string') {
109-
95+
opts?.forEach((E) => {
96+
if (typeof E === "string") {
11097
let name = E;
11198
let cssName = E;
11299

113-
if (E.slice(0, 2) === '--') {
100+
if (E.slice(0, 2) === "--") {
114101
name = E.slice(2);
115102
} else {
116-
cssName = '--' + E;
103+
cssName = "--" + E;
117104
}
118105

119106
pureSkips.push(name);
120107
cssVariablesMap.set(cssName, cssName);
121-
}
122-
else
123-
scriptBasedSkips.push(E);
108+
} else scriptBasedSkips.push(E);
124109
});
125110

126111
increase();
127112

128113
return {
129114
postcssPlugin,
130115
Declaration: {
131-
'*': map
132-
}
116+
"*": map,
117+
},
133118
};
134-
};
119+
}
135120

121+
module.exports = variableCompress;
136122
module.exports.postcss = true;

0 commit comments

Comments
 (0)