Skip to content

Commit

Permalink
Merge pull request #1655 from chipx86/custom-vars
Browse files Browse the repository at this point in the history
Support specifying custom variables when calling lessc and less.js.
  • Loading branch information
lukeapage committed Nov 15, 2013
2 parents ff611f7 + daec7df commit 8f4ef41
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 11 deletions.
8 changes: 8 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ module.exports = function(grunt) {
specs: 'test/browser/runner-modify-vars-spec.js',
outfile: 'tmp/browser/test-runner-modify-vars.html'
}
},
globalVars: {
src: ['test/browser/less/global-vars/*.less'],
options: {
helpers: 'test/browser/runner-global-vars-options.js',
specs: 'test/browser/runner-global-vars-spec.js',
outfile: 'tmp/browser/test-runner-global-vars.html'
}
}
},

Expand Down
23 changes: 21 additions & 2 deletions bin/lessc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ var options = {
relativeUrls: false,
ieCompat: true,
strictMath: false,
strictUnits: false
strictUnits: false,
globalVariables: '',
modifyVariables: ''
};
var continueProcessing = true,
currentErrorcode;
Expand Down Expand Up @@ -53,6 +55,11 @@ var checkBooleanArg = function(arg) {
return Boolean(onOff[2]);
};

var parseVariableOption = function(option) {
var parts = option.split('=', 2);
return '@' + parts[0] + ': ' + parts[1] + ';\n';
};

var warningMessages = "";
var sourceMapFileInline = false;

Expand All @@ -64,7 +71,7 @@ args = args.filter(function (arg) {
return false;
}

if (match = arg.match(/^--?([a-z][0-9a-z-]*)(?:=([^\s]*))?$/i)) { arg = match[1] }
if (match = arg.match(/^--?([a-z][0-9a-z-]*)(?:=(.*))?$/i)) { arg = match[1] }
else { return arg }

switch (arg) {
Expand Down Expand Up @@ -189,6 +196,16 @@ args = args.filter(function (arg) {
options.strictUnits = checkBooleanArg(match[2]);
}
break;
case "global-var":
if (checkArgFunc(arg, match[2])) {
options.globalVariables += parseVariableOption(match[2]);
}
break;
case "modify-var":
if (checkArgFunc(arg, match[2])) {
options.modifyVariables += parseVariableOption(match[2]);
}
break;
}
});

Expand Down Expand Up @@ -267,6 +284,8 @@ var parseLessFile = function (e, data) {
return;
}

data = options.globalVariables + data + options.modifyVariables;

options.paths = [path.dirname(input)].concat(options.paths);
options.filename = input;

Expand Down
38 changes: 29 additions & 9 deletions lib/less/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ if (dumpLineNumbers) {
var typePattern = /^text\/(x-)?less$/;
var cache = null;
var fileCache = {};
var varsPre = "";

function log(str, level) {
if (less.env == 'development' && typeof(console) !== 'undefined' && less.logLevel >= level) {
Expand Down Expand Up @@ -294,9 +295,15 @@ function loadStyles(newVars) {
var env = new less.tree.parseEnv(less),
lessText = style.innerHTML || '';
env.filename = document.location.href.replace(/#.*$/, '');
if (newVars) {

if (newVars || varsPre) {
env.useFileCache = true;
lessText += "\n" + newVars;

lessText = varsPre + lessText;

if (newVars) {
lessText += "\n" + newVars;
}
}

/*jshint loopfunc:true */
Expand Down Expand Up @@ -499,6 +506,8 @@ function loadFile(originalHref, currentFileInfo, callback, env, newVars) {
}

doXHR(href, env.mime, function (data, lastModified) {
data = varsPre + data;

// per file cache
fileCache[href] = data;

Expand All @@ -518,7 +527,7 @@ function loadStyleSheet(sheet, callback, reload, remaining, newVars) {
var env = new less.tree.parseEnv(less);
env.mime = sheet.type;

if (newVars) {
if (newVars || varsPre) {
env.useFileCache = true;
}

Expand Down Expand Up @@ -585,6 +594,18 @@ function initRunningMode(){
}
}

function serializeVars(vars) {
var s = "";

for (var name in vars) {
s += ((name.slice(0,1) === '@')? '' : '@') + name +': '+
((vars[name].slice(-1) === ';')? vars[name] : vars[name] +';');
}

return s;
}


//
// Watch mode
//
Expand Down Expand Up @@ -627,12 +648,7 @@ for (var i = 0; i < links.length; i++) {
// CSS without reloading less-files
//
less.modifyVars = function(record) {
var newVars = "";
for (var name in record) {
newVars += ((name.slice(0,1) === '@')? '' : '@') + name +': '+
((record[name].slice(-1) === ';')? record[name] : record[name] +';');
}
less.refresh(false, newVars);
less.refresh(false, serializeVars(record));
};

less.refresh = function (reload, newVars) {
Expand All @@ -659,6 +675,10 @@ less.refresh = function (reload, newVars) {
loadStyles(newVars);
};

if (less.globalVars) {
varsPre = serializeVars(less.globalVars) + "\n";
}

less.refreshStyles = loadStyles;

less.Parser.fileLoader = loadFile;
Expand Down
2 changes: 2 additions & 0 deletions lib/less/lessc_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ var lessc_helper = {
console.log(" be removed in the future.");
console.log(" -su=on|off Allow mixed units, e.g. 1px+1em or 1px*1px which have units");
console.log(" --strict-units=on|off that cannot be represented.");
console.log(" --global-var='VAR=VALUE' Defines a variable that can be referenced by the file.");
console.log(" --modify-var='VAR=VALUE' Modifies a variable already declared in the file.");
console.log("");
console.log("-------------------------- Deprecated ----------------");
console.log(" -O0, -O1, -O2 Set the parser's optimization level. The lower");
Expand Down
3 changes: 3 additions & 0 deletions test/browser/css/global-vars/simple.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.test {
color: #ff0000;
}
3 changes: 3 additions & 0 deletions test/browser/less/global-vars/simple.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.test {
color: @global-var;
}
4 changes: 4 additions & 0 deletions test/browser/runner-global-vars-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var less = {};
less.globalVars = {
"@global-var": "red"
};
3 changes: 3 additions & 0 deletions test/browser/runner-global-vars-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
describe("less.js global vars", function() {
testLessEqualsInDocument();
});

6 comments on commit 8f4ef41

@jonschlinkert
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 awesome!

@jonschlinkert
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bah! I didn't notice this was for the browser til now. would be great to see this in the node.js code.

@lukeapage
Copy link
Member Author

@lukeapage lukeapage commented on 8f4ef41 Nov 16, 2013 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonschlinkert
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good grief, my attention span is worse than ever. Thanks.

Any chance we can get this published to npm?

@lukeapage
Copy link
Member Author

@lukeapage lukeapage commented on 8f4ef41 Nov 16, 2013 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonschlinkert
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.