Skip to content

Commit 5d98a9f

Browse files
committed
first commit
0 parents  commit 5d98a9f

File tree

12 files changed

+6247
-0
lines changed

12 files changed

+6247
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text eol=lf

.github/workflows/test.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Test
2+
on:
3+
push:
4+
pull_request:
5+
env:
6+
FORCE_COLOR: 2
7+
jobs:
8+
full:
9+
name: Node.js 15 Full
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout the repository
13+
uses: actions/checkout@v2
14+
- name: Install Node.js
15+
uses: actions/setup-node@v2
16+
with:
17+
node-version: 15
18+
- name: Install dependencies
19+
uses: bahmutov/npm-install@v1
20+
- name: Run tests
21+
run: yarn test
22+
short:
23+
runs-on: ubuntu-latest
24+
strategy:
25+
matrix:
26+
node-version:
27+
- 14
28+
- 12
29+
- 10
30+
name: Node.js ${{ matrix.node-version }} Quick
31+
steps:
32+
- name: Checkout the repository
33+
uses: actions/checkout@v2
34+
- name: Install Node.js ${{ matrix.node-version }}
35+
uses: actions/setup-node@v2
36+
with:
37+
node-version: ${{ matrix.node-version }}
38+
- name: Install dependencies
39+
uses: bahmutov/npm-install@v1
40+
- name: Run unit tests
41+
run: npx jest

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
node_modules/
2+
npm-debug.log
3+
yarn-error.log
4+
npm-debug.log*
5+
6+
coverage/
7+
*.log
8+
.npm
9+
10+
.DS_Store

.npmignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
yarn-error.log
2+
npm-debug.log
3+
package-lock.json
4+
yarn.lock
5+
6+
*.test.js
7+
.travis.yml
8+
.editorconfig
9+
coverage/
10+
.gitignore
11+
12+
node_modules/

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Change Log

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright 2021 Navanshu Rastogi <navanshu.rastogi@gmail.com>
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.

README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# PostCSS Variable Compress
2+
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.
4+
5+
[PostCSS]: https://github.com/postcss/postcss
6+
[postcss-variable-compress]: https://github.com/navanshu/postcss-variable-compress
7+
8+
```css
9+
:root {
10+
--first-color: #16f;
11+
--second-color: #ff7;
12+
}
13+
14+
#firstParagraph {
15+
background-color: var(--first-color);
16+
color: var(--second-color);
17+
}
18+
19+
#secondParagraph {
20+
background-color: var(--second-color);
21+
color: var(--first-color);
22+
}
23+
24+
#container {
25+
--first-color: #290;
26+
}
27+
28+
.thirdParagraph {
29+
background-color: var(--first-color);
30+
color: var(--second-color);
31+
}
32+
```
33+
34+
```css
35+
:root {
36+
--0: #16f;
37+
--1: #ff7;
38+
--2: #000;
39+
}
40+
41+
#firstParagraph {
42+
background-color: var(--0);
43+
color: var(--1);
44+
}
45+
46+
#secondParagraph {
47+
background-color: var(--1);
48+
color: var(--0);
49+
}
50+
51+
#container {
52+
--0: #290;
53+
}
54+
55+
.thirdParagraph {
56+
background-color: var(--0);
57+
color: var(--1);
58+
}
59+
```
60+
61+
## Usage
62+
63+
**Step 1:** Install plugin:
64+
65+
```sh
66+
npm install --save-dev postcss postcss-variable-compress
67+
```
68+
69+
**Step 2:** Check you project for existed PostCSS config: `postcss.config.js`
70+
in the project root, `"postcss"` section in `package.json`
71+
or `postcss` in bundle config.
72+
73+
If you do not use PostCSS, add it according to [official docs]
74+
and set this plugin in settings.
75+
76+
**Step 3:** Add the plugin at the end of the plugins list:
77+
78+
```diff
79+
module.exports = {
80+
plugins: [
81+
require('cssnano'),
82+
+ require('postcss-variable-compress')
83+
]
84+
}
85+
```
86+
87+
[official docs]: https://github.com/postcss/postcss#usage

index.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
const VariablesMap = new Map();
2+
const History = [];
3+
let Variables = -1;
4+
let Skips = [];
5+
/**
6+
* @param {string} val
7+
*/
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;
22+
}
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);
33+
34+
if (!exist) {
35+
exist = '--' + (Variables).toString(36);
36+
increase();
37+
VariablesMap.set(match, exist);
38+
History.push(exist);
39+
}
40+
41+
return exist;
42+
}
43+
44+
/**
45+
* @param {import('postcss').Declaration} j
46+
*/
47+
function looker (j) {
48+
49+
let prop = j.prop;
50+
51+
if (j.variable && shouldRun(prop)) {
52+
let old = prop;
53+
let exist = VariablesMap.get(old);
54+
if (!exist) {
55+
exist = '--' + Variables.toString(36);
56+
increase();
57+
VariablesMap.set(old, exist);
58+
History.push(exist);
59+
}
60+
prop = exist;
61+
}
62+
63+
let value = j.value.replace(/--[\w-_]{1,1000}/g, replacer);
64+
65+
if (prop) j.prop = prop;
66+
if (value) j.value = value;
67+
68+
}
69+
70+
/**
71+
* @param {string[]} opts
72+
*/
73+
module.exports = (opts = []) => {
74+
75+
opts.forEach(E => VariablesMap.set(E, E));
76+
opts.forEach(E => Skips.push(E.replace('--', '')));
77+
78+
increase();
79+
80+
return {
81+
postcssPlugin: 'postcss-variable-compress',
82+
Declaration: {
83+
'*': looker
84+
}
85+
};
86+
};
87+
88+
module.exports.postcss = true;

index.test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const postcss = require('postcss');
2+
3+
const plugin = require('./index');
4+
5+
async function run (input, output, opts = {}) {
6+
let result = await postcss([plugin(opts)]).process(input, { from: undefined });
7+
expect(result.css).toEqual(output);
8+
expect(result.warnings()).toHaveLength(0);
9+
}
10+
11+
12+
it('Shorten css variables', async () => {
13+
await run(
14+
`:root {
15+
--first-color: #16f;
16+
--second-color: #ff7;
17+
--2: #000;
18+
}
19+
20+
#firstParagraph {
21+
background-color: var(--first-color);
22+
color: var(--second-color);
23+
}
24+
25+
#secondParagraph {
26+
background-color: var(--second-color);
27+
color: var(--first-color);
28+
}
29+
30+
#container {
31+
--first-color: #290;
32+
}
33+
34+
#thirdParagraph {
35+
background-color: var(--first-color);
36+
color: var(--second-color);
37+
}
38+
39+
.section-title {
40+
color: var(--primary-color, var(--black, #222));
41+
}`,
42+
`:root {
43+
--0: #16f;
44+
--1: #ff7;
45+
--2: #000;
46+
}
47+
48+
#firstParagraph {
49+
background-color: var(--0);
50+
color: var(--1);
51+
}
52+
53+
#secondParagraph {
54+
background-color: var(--1);
55+
color: var(--0);
56+
}
57+
58+
#container {
59+
--0: #290;
60+
}
61+
62+
#thirdParagraph {
63+
background-color: var(--0);
64+
color: var(--1);
65+
}
66+
67+
.section-title {
68+
color: var(--primary-color, var(--3, #222));
69+
}`,
70+
[
71+
'--primary-color',
72+
'--2'
73+
]
74+
);
75+
});

0 commit comments

Comments
 (0)