Skip to content

Commit 476892f

Browse files
committed
Adding support for hex colors
making hex color parsing optional checking input length before checking char[0]
1 parent 655540e commit 476892f

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## What is this
22
Add Sass variables to gulp stream, so that you can use for example environment variables in your Sass build process.
33

4-
Supports strings, numbers and booleans.
4+
Supports strings, numbers, booleans, and hex colors.
55

66
## Installation
77

@@ -39,3 +39,18 @@ $path: '/dev/path/' !default;
3939
$path: '/prod/path';
4040
}
4141
```
42+
43+
## Options
44+
### allowHexColors
45+
(bool) - when true, will print hex colors as colors instead of strings
46+
```javascript
47+
gulp.src('./src/scss/master.scss')
48+
.pipe(sassVariables({
49+
$color-primary: '#800080'
50+
}, { allowHexColors: true }))
51+
```
52+
```scss
53+
body {
54+
color: $color-primary;
55+
}
56+
```

index.js

+32-2
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,47 @@ const PLUGIN_NAME = 'gulp-sass-variables';
55
const PluginError = require('plugin-error');
66
const through = require('through2');
77

8+
let globalOptions = {
9+
allowHexColors: false
10+
};
11+
812
const getVariablesBuffer = function(sassVariables, file) {
913
let str = '';
1014

1115
for(let variable in sassVariables) {
12-
str += variable + ': ' + JSON.stringify(sassVariables[variable]) + ';\n';
16+
str += variable + ': ' + printVariable(sassVariables[variable]) + ';\n';
1317
}
1418

1519
return new Buffer(str, file);
1620
}
1721

18-
module.exports = function(sassVariables) {
22+
const printVariable = function(variable) {
23+
if (globalOptions.allowHexColors && isHexColor(variable)) {
24+
return variable;
25+
} else {
26+
return JSON.stringify(variable);
27+
}
28+
}
29+
30+
const isHexColor = function(variable) {
31+
return (
32+
typeof variable === 'string' &&
33+
(variable.length === 7 || variable.length === 4) &&
34+
variable[0] === '#'
35+
);
36+
}
37+
38+
const parseOptions = function(options) {
39+
if (options.hasOwnProperty('allowHexColors')) {
40+
globalOptions.allowHexColors = options.allowHexColors;
41+
}
42+
}
43+
44+
module.exports = function(sassVariables, options) {
45+
46+
if (typeof options !== 'undefined') {
47+
parseOptions(options);
48+
}
1949

2050
return through.obj(function (file, encoding, cb) {
2151

test/expected/color-test.scss

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
$LONGCOLOR: #800080;
2+
$SHORTCOLOR: #f0f;

test/main.js

+23
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,29 @@ describe('gulp-sass-variables', function() {
118118
stream.write(sassFile);
119119
});
120120

121+
it('should work with hex colors when allowHexColors is true', function (done) {
122+
let stream = sassVariables({
123+
$LONGCOLOR: '#800080',
124+
$SHORTCOLOR: '#f0f'
125+
}, { allowHexColors: true });
126+
127+
let sassFile = createVinyl('color-test.scss');
128+
129+
stream.on('data', function(file) {
130+
should.exist(file);
131+
should.exist(file.path);
132+
should.exist(file.relative);
133+
should.exist(file.contents);
134+
should.equal(path.basename(file.path), 'color-test.scss');
135+
String(file.contents).should.equal(
136+
fs.readFileSync(path.join(__dirname, 'expected/color-test.scss'), 'utf8')
137+
);
138+
done();
139+
});
140+
141+
stream.write(sassFile);
142+
});
143+
121144
it('should work with gulp-sass', function (done) {
122145
let streamDoneCount = 0;
123146

test/scss/color-test.scss

Whitespace-only changes.

0 commit comments

Comments
 (0)