Skip to content

Commit 741d6cf

Browse files
committed
A new, cleaner, tilde interface.
1 parent 5d6f780 commit 741d6cf

File tree

6 files changed

+93
-41
lines changed

6 files changed

+93
-41
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"no-extra-parens": [2],
88
"no-unused-vars": [2],
99
"no-loop-func": [0],
10+
"no-eval": [0],
1011
"key-spacing": [0],
11-
"max-len": [2, 200],
1212
"strict": [0],
1313
"indent": [2],
1414
"quotes": [2, "single", "avoid-escape"],

README.md

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,24 @@
66
[ci-img]: https://travis-ci.org/ojame/postcss-local-vars.svg
77
[ci]: https://travis-ci.org/ojame/postcss-local-vars
88

9-
```css
10-
/* var.css */
11-
local.primaryGreen: #8EE7D3;
9+
**vars.json**
10+
```js
11+
[{
12+
colors: {
13+
primary: '#8EE7D3',
14+
},
15+
}]
1216
```
1317

18+
**input**
1419
```css
20+
~colors: "./vars.json";
1521
.foo {
16-
color: import local.primaryGreen from './vars.css';
22+
color: primary from ~colors;
1723
}
1824
```
1925

26+
**output**
2027
```css
2128
.foo {
2229
color: #8EE7D3;
@@ -25,36 +32,52 @@ local.primaryGreen: #8EE7D3;
2532

2633
#### Within static values
2734

28-
```css
29-
/* var.css */
30-
local.primaryGreen: #8EE7D3;
35+
**vars.json**
36+
```js
37+
[{
38+
borders: {
39+
weight: '2px',
40+
style: 'solid',
41+
},
42+
}]
3143
```
3244

45+
**input**
3346
```css
47+
~borders: "./vars.json";
3448
.foo {
35-
border: 2px solid import local.primaryGreen from './vars.css';
49+
border: weight from ~borders style from ~borders black;
3650
}
3751
```
3852

53+
**output**
3954
```css
4055
.foo {
41-
border: 2px solid #8EE7D3;
56+
border: 2px solid black;
4257
}
4358
```
4459

4560
#### @ Rules
4661

47-
```css
48-
/* var.css */
49-
local.query: 200px;
62+
**vars.json**
63+
```js
64+
[{
65+
queries: {
66+
maxWidth: '200px',
67+
},
68+
}]
5069
```
5170

71+
**input**
5272
```css
53-
@media (max-width: import local.query from './vars.css') {
73+
~queries: "./vars.json";
74+
75+
@media (max-width: maxWidth from ~queries) {
5476
color: blue;
5577
}
5678
```
5779

80+
**output**
5881
```css
5982
@media (max-width: 200px) {
6083
color: blue;

index.js

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,53 @@ var postcss = require('postcss');
22
var fs = require('fs');
33

44
module.exports = postcss.plugin('postcss-local-vars', function () {
5-
var importRegex = /((import)(\s+)(local)(\.))((?:[A-z]+))( )(from)( )(".*?"|'.*?')/g;
6-
var nameRegex = /.*?((local)(\.))((?:[A-z]+))/g;
5+
var sets = {};
6+
var regex = /((?:[A-z]+))( )(from)(\s+)(~)((?:[A-z]+))/g;
77

88
var readFile = function(file) {
99
return fs.readFileSync(file, 'utf8');
1010
};
1111

12-
var requiresAction = function(context) {
13-
return context.indexOf('import local.') !== -1;
12+
var getVariables = function(name, path) {
13+
var file = readFile(path.replace(/'|"/g, ''));
14+
var requiredSet = name.replace(/~/g, '');
15+
var variableSets = eval(file)[0];
16+
sets[requiredSet] = variableSets[requiredSet];
1417
};
1518

16-
var getPath = function(context) {
17-
var pathMatches = importRegex.exec(context);
18-
return pathMatches[pathMatches.length - 1].replace(/'|"/g, '');
19+
var requiresAction = function(context) {
20+
return context.indexOf(' ~') !== -1;
1921
};
2022

21-
var getName = function(context) {
22-
var nameMatches = nameRegex.exec(context);
23-
return nameMatches[nameMatches.length - 1];
23+
var getValue = function(variable, parent) {
24+
return sets[parent][variable];
2425
};
2526

2627
var strip = function(context) {
2728
if (!requiresAction(context)) {
2829
return context;
2930
}
3031

31-
var variablesFile = readFile(getPath(context));
32-
var variablesRegex = new RegExp('(local\.' + getName(context) + ')(:).*?(.*?)(;)', 'g');
33-
var variables = variablesRegex.exec(variablesFile);
34-
var value = variables[variables.length - 2].trim();
32+
var requires = context.match(regex);
3533

36-
return context.replace(importRegex, value);
34+
requires.forEach(function(require) {
35+
var matches = regex.exec(require);
36+
regex.lastIndex = 0;
37+
var variable = matches[1];
38+
var variableSet = matches[matches.length - 1];
39+
40+
context = context.replace(require, getValue(variable, variableSet));
41+
});
42+
43+
return context;
3744
};
3845

3946
return function (css) {
4047
css.eachInside(function (node) {
48+
if (node.prop && node.prop.indexOf('~') > -1) {
49+
getVariables(node.prop, node.value);
50+
}
51+
4152
if (node.type === 'decl') {
4253
node.value = strip(node.value);
4354
}

test/test.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,38 @@ describe('postcss-local-vars', function () {
1919
});
2020

2121
it('replaces variables in values', function (done) {
22-
test('a{color: import local.green from "./test/vars.css"}', 'a{color: #8EE7D3}', { }, done);
22+
test('~colors: "test/vars.json"; a{color: primary from ~colors;}', '~colors: "test/vars.json"; a{color: #8EE7D3;}', { }, done);
2323
});
2424

25-
it('replaces variables in values with semi-colons', function (done) {
26-
test('a{color: import local.green from "./test/vars.css";}', 'a{color: #8EE7D3;}', { }, done);
25+
it('replaces variables in values without semi-colons', function (done) {
26+
test('~colors: "test/vars.json"; a{background: blue; color: primary from ~colors}', '~colors: "test/vars.json"; a{background: blue; color: #8EE7D3}', { }, done);
2727
});
2828

2929
it('replaces variables in values with single quotes', function (done) {
30-
test('a{color: import local.green from \'./test/vars.css\';}', 'a{color: #8EE7D3;}', { }, done);
30+
test('~colors: \'test/vars.json\'; a{color: primary from ~colors;}', '~colors: \'test/vars.json\'; a{color: #8EE7D3;}', { }, done);
3131
});
3232

3333
it('replaces variables at start of value', function (done) {
34-
test('a{border: import local.borderWeight from "./test/vars.css" solid #8EE7D3;}', 'a{border: 2px solid #8EE7D3;}', { }, done);
34+
test('~borders: "test/vars.json"; a{border: weight from ~borders solid #8EE7D3;}', '~borders: "test/vars.json"; a{border: 2px solid #8EE7D3;}', { }, done);
3535
});
3636

3737
it('replaces variables in middle of value', function (done) {
38-
test('a{border: 2px import local.borderStyle from "./test/vars.css" #8EE7D3;}', 'a{border: 2px solid #8EE7D3;}', { }, done);
38+
test('~borders: "test/vars.json"; a{border: 2px style from ~borders #8EE7D3;}', '~borders: "test/vars.json"; a{border: 2px solid #8EE7D3;}', { }, done);
3939
});
4040

4141
it('replaces variables at end of value', function (done) {
42-
test('a{border: 2px solid import local.green from "./test/vars.css";}', 'a{border: 2px solid #8EE7D3;}', { }, done);
42+
test('~colors: "test/vars.json"; a{border: 2px solid primary from ~colors;}', '~colors: "test/vars.json"; a{border: 2px solid #8EE7D3;}', { }, done);
4343
});
4444

4545
it('replaces variables in @ rules', function (done) {
46-
test('@media (max-width: import local.mediaQuery from "./test/vars.css") {color: red;}', '@media (max-width: 200px) {color: red;}', { }, done);
46+
test('~queries: "test/vars.json"; @media (max-width: maxWidth from ~queries) {color: red;}', '~queries: "test/vars.json"; @media (max-width: 200px) {color: red;}', { }, done);
47+
});
48+
49+
it('multiple sources', function (done) {
50+
test('~colors: "test/vars.json"; ~borders: "test/vars.json"; a{color: primary from ~colors; border: 2px style from ~borders black}', '~colors: "test/vars.json"; ~borders: "test/vars.json"; a{color: #8EE7D3; border: 2px solid black}', { }, done);
51+
});
52+
53+
it('multiple variables in a single value', function (done) {
54+
test('~borders: "test/vars.json"; a{border: weight from ~borders style from ~borders black}', '~borders: "test/vars.json"; a{border: 2px solid black}', { }, done);
4755
});
4856
});

test/vars.css

Lines changed: 0 additions & 4 deletions
This file was deleted.

test/vars.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[{
2+
colors: {
3+
primary: '#8EE7D3',
4+
},
5+
6+
queries: {
7+
maxWidth: '200px',
8+
},
9+
10+
borders: {
11+
weight: '2px',
12+
style: 'solid',
13+
},
14+
}]

0 commit comments

Comments
 (0)