Skip to content

Adding support for hex colors #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## What is this
Add Sass variables to gulp stream, so that you can use for example environment variables in your Sass build process.

Supports strings, numbers and booleans.
Supports strings, numbers, booleans, and hex colors.

## Installation

Expand Down Expand Up @@ -39,3 +39,18 @@ $path: '/dev/path/' !default;
$path: '/prod/path';
}
```

## Options
### allowHexColors
(bool) - when true, will print hex colors as colors instead of strings
```javascript
gulp.src('./src/scss/master.scss')
.pipe(sassVariables({
$color-primary: '#800080'
}, { allowHexColors: true }))
```
```scss
body {
color: $color-primary;
}
```
34 changes: 32 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,47 @@ const PLUGIN_NAME = 'gulp-sass-variables';
const PluginError = require('plugin-error');
const through = require('through2');

let globalOptions = {
allowHexColors: false
};

const getVariablesBuffer = function(sassVariables, file) {
let str = '';

for(let variable in sassVariables) {
str += variable + ': ' + JSON.stringify(sassVariables[variable]) + ';\n';
str += variable + ': ' + printVariable(sassVariables[variable]) + ';\n';
}

return new Buffer(str, file);
}

module.exports = function(sassVariables) {
const printVariable = function(variable) {
if (globalOptions.allowHexColors && isHexColor(variable)) {
return variable;
} else {
return JSON.stringify(variable);
}
}

const isHexColor = function(variable) {
return (
typeof variable === 'string' &&
(variable.length === 7 || variable.length === 4) &&
variable[0] === '#'
);
}

const parseOptions = function(options) {
if (options.hasOwnProperty('allowHexColors')) {
globalOptions.allowHexColors = options.allowHexColors;
}
}

module.exports = function(sassVariables, options) {

if (typeof options !== 'undefined') {
parseOptions(options);
}

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

Expand Down
2 changes: 2 additions & 0 deletions test/expected/color-test.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$LONGCOLOR: #800080;
$SHORTCOLOR: #f0f;
23 changes: 23 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,29 @@ describe('gulp-sass-variables', function() {
stream.write(sassFile);
});

it('should work with hex colors when allowHexColors is true', function (done) {
let stream = sassVariables({
$LONGCOLOR: '#800080',
$SHORTCOLOR: '#f0f'
}, { allowHexColors: true });

let sassFile = createVinyl('color-test.scss');

stream.on('data', function(file) {
should.exist(file);
should.exist(file.path);
should.exist(file.relative);
should.exist(file.contents);
should.equal(path.basename(file.path), 'color-test.scss');
String(file.contents).should.equal(
fs.readFileSync(path.join(__dirname, 'expected/color-test.scss'), 'utf8')
);
done();
});

stream.write(sassFile);
});

it('should work with gulp-sass', function (done) {
let streamDoneCount = 0;

Expand Down
Empty file added test/scss/color-test.scss
Empty file.