Skip to content
This repository has been archived by the owner on Feb 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #21 from bloodyowl/feat.auto-double-dash
Browse files Browse the repository at this point in the history
Added: variables defined in JS are now automatically prefixed with `--`
  • Loading branch information
MoOx committed Mar 16, 2015
2 parents 8bba058 + c1e1907 commit 8289bd5
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 3.1.0 - 2015-03-16

- Added: variables defined in JS are now automatically prefixed with `--`
([0691784](https://github.com/postcss/postcss-custom-properties/commit/0691784ed2218d7e6b16da8c4df03e2ca0c4798c))

# 3.0.1 - 2015-02-06

- Fixed: logs now have filename back ([#19](https://github.com/postcss/postcss-custom-properties/issues/19))
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ var out = postcss()
#### `variables` (default: `{}`)

Allow you to pass an object of variables for `:root`. These definitions will override any that exist in the CSS.
The keys are automatically prefixed with the CSS `--` to make it easier to share
variables in your codebase.

---

Expand Down
11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ var RE_VAR = /([\w-]+)(?:\s*,\s*)?(.*)?/ // matches `name[, fallback]`, captures
module.exports = function(options) {
return function(style) {
options = options || {}
var variables = options.variables || {}
var userVariables = options.variables || {}
var variables =
Object.keys(userVariables)
.reduce(function(acc, key) {
if (key.indexOf("--") !== 0) {
acc["--" + key] = userVariables[key]
}
acc[key] = userVariables[key]
return acc
}, {})
var preserve = (options.preserve === true ? true : false)
var map = {}
var importantMap = {}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-custom-properties",
"version": "3.0.1",
"version": "3.1.0",
"description": "PostCSS plugin to polyfill W3C CSS Custom Properties for cascading variables",
"keywords": [
"css",
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/automatic-variable-prefix.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
div {
color: var(--unprefixed);
background: var(--prefixed);
}
4 changes: 4 additions & 0 deletions test/fixtures/automatic-variable-prefix.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
div {
color: blue;
background: white;
}
10 changes: 10 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ test("accepts variables defined from JavaScript, and overrides local definitions
t.end()
})

test("prefixes js defined variabled with a double dash automatically", function(t) {
compareFixtures(t, "automatic-variable-prefix", {
variables: {
unprefixed: "blue",
"--prefixed": "white"
}
})
t.end()
})

test("removes variable properties from the output", function(t) {
compareFixtures(t, "remove-properties")
t.end()
Expand Down

0 comments on commit 8289bd5

Please sign in to comment.