Skip to content

Commit

Permalink
Adds modify-vars option.
Browse files Browse the repository at this point in the history
  • Loading branch information
Luisma committed Feb 3, 2014
1 parent 133475e commit 2aac451
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ module.exports = function(grunt) {
'tmp/customFunctions.css': ['test/fixtures/customFunctions.less']
}
},
modifyVars: {
options: {
modifyVars: {
// Note the double quotes in 'imgPath'
imgPath: '"/somewhere/else"',
customPadding: '20px'
}
},
files: {
'tmp/modifyVars.css': ['test/fixtures/modifyVars.less']
}
},
},

// Unit tests.
Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ Default: false

Puts the less files into the map instead of referencing them.

## modifyVars
Type: `Object`

Default: none

Overrides global variables. Equivalent to ```--modify-vars='VAR=VALUE'``` option in less.

### Usage Examples

```js
Expand All @@ -198,7 +205,11 @@ less: {
production: {
options: {
paths: ["assets/css"],
cleancss: true
cleancss: true,
modifyVars: {
imgPath: '"http://mycdn.com/path/to/images"',
bgColor: 'red'
}
},
files: {
"path/to/result.css": "path/to/source.less"
Expand Down
7 changes: 7 additions & 0 deletions docs/less-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,10 @@ Type: `Boolean`
Default: false

Puts the less files into the map instead of referencing them.

## modifyVars
Type: `Object`

Default: none

Overrides global variables. Equivalent to ```--modify-vars='VAR=VALUE'``` option in less.
15 changes: 15 additions & 0 deletions tasks/less.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ module.exports = function(grunt) {

var parser = new less.Parser(grunt.util._.pick(options, lessOptions.parse));

// Equivalent to --modify-vars option.
// Properties under options.modifyVars are appended as less variables
// to override global variables.
var modifyVarsOutput = parseVariableOptions(options['modifyVars']);
srcCode += modifyVarsOutput;

parser.parse(srcCode, function(parse_err, tree) {
if (parse_err) {
lessError(parse_err, srcFile);
Expand Down Expand Up @@ -131,6 +137,15 @@ module.exports = function(grunt) {
});
};

var parseVariableOptions = function(options) {
var pairs = grunt.util._.pairs(options);
var output = '';
grunt.util._.forEach(pairs, function(pair) {
output += '@' + pair[0] + ':' + pair[1] + ';';
});
return output;
};

var formatLessError = function(e) {
var pos = '[' + 'L' + e.line + ':' + ('C' + e.column) + ']';
return e.filename + ': ' + pos + ' ' + e.message;
Expand Down
4 changes: 4 additions & 0 deletions test/expected/modifyVars.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.nice-kitty {
padding: 20px;
background: url('/somewhere/else/nice-kitty.jpg');
}
7 changes: 7 additions & 0 deletions test/fixtures/modifyVars.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@imgPath: '/path/to/images';
@customPadding: 12px;

.nice-kitty {
padding: @customPadding;
background: url('@{imgPath}/nice-kitty.jpg')
}
9 changes: 9 additions & 0 deletions test/less_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ exports.less = {

test.done();
},
modifyVars: function(test) {
test.expect(1);

var actual = grunt.file.read('tmp/modifyVars.css');
var expected = grunt.file.read('test/expected/modifyVars.css');
test.equal(expected, actual, 'should override global variables');

test.done();
},
sourceMap: function(test) {
test.expect(1);

Expand Down

0 comments on commit 2aac451

Please sign in to comment.