Skip to content

Commit 50628f0

Browse files
authored
Merge pull request #36 from navanshu/revert-35-master
Revert "Adds ability to predictably map known variables"
2 parents 4146466 + 80fbb89 commit 50628f0

File tree

7 files changed

+37
-146
lines changed

7 files changed

+37
-146
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ coverage/
88
.npm
99

1010
.DS_Store
11-
.idea

README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,6 @@ module.exports = {
8787
// for example
8888
(name) => name.includes('skip') // name prefixed css variable example --height
8989
// avoid regex if you can they are bad
90-
// To compress varaiables to a known ma you can pass in a known object to map against
91-
// for example
92-
{
93-
'--height': '--h',
94-
'--font': '--f'
95-
}
9690
)
9791
]
9892
}

index.d.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
export declare namespace variableCompress {
22
type skip = (variableName: string) => boolean | undefined;
3-
type map = {
4-
[key: string]: string;
5-
};
6-
type parameters = skip | map | string;
3+
type parameters = skip | string;
74
}

index.js

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,28 +73,20 @@ module.exports = function variableCompress(opts) {
7373
scriptBasedSkips = [];
7474
cssVariablesMap = new Map();
7575
opts === null || opts === void 0 ? void 0 : opts.forEach(E => {
76-
switch (typeof E) {
77-
case 'string':
78-
const name = E.startsWith('--')
79-
? E.slice(2)
80-
: E;
81-
const cssName = E.startsWith('--')
82-
? E
83-
: `--${E}`;
84-
pureSkips.push(name);
85-
cssVariablesMap.set(cssName, cssName);
86-
break;
87-
case 'object':
88-
Object.entries(E)
89-
.forEach(([key, value]) => {
90-
renamedVariables.push(value);
91-
cssVariablesMap.set(key, value);
92-
});
93-
break;
94-
default:
95-
scriptBasedSkips.push(E);
96-
break;
76+
if (typeof E === 'string') {
77+
let name = E;
78+
let cssName = E;
79+
if (E.slice(0, 2) === '--') {
80+
name = E.slice(2);
81+
}
82+
else {
83+
cssName = '--' + E;
84+
}
85+
pureSkips.push(name);
86+
cssVariablesMap.set(cssName, cssName);
9787
}
88+
else
89+
scriptBasedSkips.push(E);
9890
});
9991
increase();
10092
return {

index.test.js

Lines changed: 2 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -83,88 +83,7 @@ code {
8383
'--primary-color',
8484
'2',
8585
(e) => e.includes('special'),
86-
(e) => e === '--5',
87-
]
88-
);
89-
});
90-
91-
it('Shorten known css variables', async () => {
92-
const knownClassNames = {
93-
'--first-color': '--a',
94-
'--second-color': '--b',
95-
}
96-
97-
await run(
98-
`:root {
99-
--first-color: #16f;
100-
--second-color: #ff7;
101-
--2: #000;
102-
}
103-
104-
#firstParagraph {
105-
background-color: var(--first-color);
106-
color: var(--second-color);
107-
}
108-
109-
#secondParagraph {
110-
background-color: var(--second-color);
111-
color: var(--first-color);
112-
}
113-
114-
#container {
115-
--first-color: #290;
116-
}
117-
118-
#thirdParagraph {
119-
background-color: var(--first-color);
120-
color: var(--second-color);
121-
}
122-
123-
.section-title {
124-
color: var(--primary-color, var(--black, #222));
125-
}
126-
127-
code {
128-
--5: #555;
129-
}`,
130-
`:root {
131-
--a: #16f;
132-
--b: #ff7;
133-
--2: #000;
134-
}
135-
136-
#firstParagraph {
137-
background-color: var(--a);
138-
color: var(--b);
139-
}
140-
141-
#secondParagraph {
142-
background-color: var(--b);
143-
color: var(--a);
144-
}
145-
146-
#container {
147-
--a: #290;
148-
}
149-
150-
#thirdParagraph {
151-
background-color: var(--a);
152-
color: var(--b);
153-
}
154-
155-
.section-title {
156-
color: var(--primary-color, var(--0, #222));
157-
}
158-
159-
code {
160-
--5: #555;
161-
}`,
162-
[
163-
'--primary-color',
164-
'2',
165-
(e) => e.includes('special'),
166-
(e) => e === '--5',
167-
knownClassNames,
86+
(e) => e === '--5'
16887
]
16988
);
17089
});
@@ -174,6 +93,7 @@ it('Support reloading. Now the plugin will reset mapped variables', async () =>
17493
await run(`:root{--second-color: #ff7;--first-color: #16f;}`, `:root{--0: #ff7;--1: #16f;}`, []);
17594
});
17695

96+
17797
it('Base array check or no array', async () => {
17898
await run(`:root{}`, `:root{}`);
17999
});

index.ts

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
export namespace variableCompress {
2+
23
export type skip = (variableName: string) => boolean | undefined;
3-
export type map = { [key: string]: string };
4-
export type parameters = skip | map | string;
5-
}
64

5+
export type parameters = skip | string;
6+
7+
}
78
const postcssPlugin = 'postcss-variable-compress';
89

910
let processed = Symbol('processed');
1011
let renamedVariables: string[] = [];
1112
let cssVariables = -1;
1213
let pureSkips: string[] = [];
1314
let scriptBasedSkips: variableCompress.skip[] = [];
14-
let cssVariablesMap = new Map<string, string>();
15+
let cssVariablesMap = new Map();
1516

1617
function scriptCheck(val: string) {
1718

@@ -104,33 +105,22 @@ module.exports = function variableCompress(opts?: variableCompress.parameters[])
104105
cssVariablesMap = new Map();
105106

106107
opts?.forEach(E => {
107-
switch (typeof E) {
108-
case 'string':
109-
110-
const name = E.startsWith('--')
111-
? E.slice(2)
112-
: E;
113-
114-
const cssName = E.startsWith('--')
115-
? E
116-
: `--${E}`;
117-
118-
pureSkips.push(name);
119-
cssVariablesMap.set(cssName, cssName);
120-
break;
121-
122-
case 'object':
123-
Object.entries(E)
124-
.forEach(([key, value]) => {
125-
renamedVariables.push(value);
126-
cssVariablesMap.set(key, value);
127-
});
128-
break;
129-
default:
130-
scriptBasedSkips.push(E);
131-
break;
132-
}
108+
if (typeof E === 'string') {
109+
110+
let name = E;
111+
let cssName = E;
133112

113+
if (E.slice(0, 2) === '--') {
114+
name = E.slice(2);
115+
} else {
116+
cssName = '--' + E;
117+
}
118+
119+
pureSkips.push(name);
120+
cssVariablesMap.set(cssName, cssName);
121+
}
122+
else
123+
scriptBasedSkips.push(E);
134124
});
135125

136126
increase();

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@
5454
"plugin:jest/recommended"
5555
],
5656
"rules": {
57-
"jest/expect-expect": "off",
58-
"no-case-declarations": "off"
57+
"jest/expect-expect": "off"
5958
}
6059
},
6160
"jest": {

0 commit comments

Comments
 (0)