Skip to content

Commit ce98591

Browse files
committed
Add auto-prefix to opts.variables with -- v0.2.2
1 parent 36fd154 commit ce98591

File tree

7 files changed

+26
-16
lines changed

7 files changed

+26
-16
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11

2+
3+
# 0.2.2 - 2015-5-1
4+
5+
- Automatically prefix any variables defined in `options.variables` with `--` (according to CSS custom property syntax).
6+
27
# 0.2.1 - 2015-4-30
38

49
- Added support for descendant selector nesting instead of just physical space nesting

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CSS variables or CSS Custom Properties limited subset polyfill/shim.
66

77
We strive for the most complete transformation but we/no plugin can achieve true complete parity according to the [speficification](http://dev.w3.org/csswg/css-variables/) because of the DOM cascade unknowns.
88

9-
## Latest Version: 0.2.1
9+
## Latest Version: 0.2.2
1010
### [Changelog](https://github.com/MadLittleMods/postcss-css-variables/blob/master/CHANGELOG.md)
1111

1212

@@ -245,10 +245,12 @@ Allowed values:
245245

246246
### `variables` (default: `{}`)
247247

248-
Define variables in JavaScript.
248+
Define an object map of variables in JavaScript that will be declared at the `:root` scope.
249249

250250
Can be a simple key-value pair or an object with a `value` property and an optional `isImportant` bool property
251251

252+
The object keys are automatically prefixed with `--` (according to CSS custom property syntax) if you do not provide it.
253+
252254

253255
```
254256
var postcss = require('postcss');
@@ -257,12 +259,12 @@ var cssvariables = require('postcss-css-variables');
257259
postcss([
258260
cssvariables({
259261
variables: {
260-
'--some-var': 100pxpx,
262+
'--some-var': '100px',
261263
'--other-var': {
262-
value: #00ff00
264+
value: '#00ff00'
263265
},
264266
'--important-var': {
265-
value: #ff0000,
267+
value: '#ff0000',
266268
isImportant: true
267269
}
268270
}

index.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// PostCSS CSS Variables (postcss-css-variables)
2-
// v0.2.1
2+
// v0.2.2
33
//
44
// https://github.com/MadLittleMods/postcss-css-variables
55

@@ -79,14 +79,6 @@ function generateScopeList(node, /*optional*/includeSelf) {
7979
currentNodeParent = currentNodeParent.parent;
8080
}
8181

82-
83-
/* * /
84-
// Turn it into a string
85-
var selectorScopeStringList = selectorScopeList.map(function(scopeStringPieces) {
86-
return scopeStringPieces.join(' ');
87-
})
88-
/* */
89-
9082
return selectorScopeList;
9183
}
9284

@@ -186,6 +178,9 @@ var resolveValue = function(decl, map, /*optional*/mimicChildOf) {
186178
};
187179
};
188180

181+
182+
183+
189184
module.exports = postcss.plugin('postcss-css-variables', function(options) {
190185
var defaults = {
191186
// Allows you to preserve custom properties & var() usage in output.
@@ -223,6 +218,10 @@ module.exports = postcss.plugin('postcss-css-variables', function(options) {
223218
Object.keys(opts.variables)
224219
.reduce(function(prevVariableMap, variableName) {
225220
var variableValue = opts.variables[variableName];
221+
// Automatically prefix any variable with `--` (CSS custom property syntax) if it doesn't have it already
222+
variableName = variableName.slice(0, 2) === '--' ? variableName : '--' + variableName;
223+
224+
226225
// If they didn't pass a object, lets construct one
227226
if(typeof variableValue !== 'object') {
228227
variableValue = {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-css-variables",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"description": "PostCSS plugin to transform CSS Custom Properties(CSS variables) syntax into a static representation",
55
"keywords": [
66
"postcss",

test/fixtures/js-defined.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.box1 {
22
width: var(--js-defined1);
33
height: var(--js-defined2);
4+
background: var(--js-defined-no-prefix);
45
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.box1 {
22
width: 75px;
33
height: 80px;
4+
background: #ff0000;
45
}

test/test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@ describe('postcss-css-variables', function() {
9999
'--js-defined1': '75px',
100100
'--js-defined2': {
101101
value: '80px'
102-
}
102+
},
103+
// Should be automatically prefixed with `--`
104+
'js-defined-no-prefix': '#ff0000'
103105
}
104106
}
105107
);

0 commit comments

Comments
 (0)