Skip to content

Commit 8f4ef41

Browse files
committed
Merge pull request #1655 from chipx86/custom-vars
Support specifying custom variables when calling lessc and less.js.
2 parents ff611f7 + daec7df commit 8f4ef41

File tree

8 files changed

+73
-11
lines changed

8 files changed

+73
-11
lines changed

Gruntfile.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,14 @@ module.exports = function(grunt) {
195195
specs: 'test/browser/runner-modify-vars-spec.js',
196196
outfile: 'tmp/browser/test-runner-modify-vars.html'
197197
}
198+
},
199+
globalVars: {
200+
src: ['test/browser/less/global-vars/*.less'],
201+
options: {
202+
helpers: 'test/browser/runner-global-vars-options.js',
203+
specs: 'test/browser/runner-global-vars-spec.js',
204+
outfile: 'tmp/browser/test-runner-global-vars.html'
205+
}
198206
}
199207
},
200208

bin/lessc

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ var options = {
2525
relativeUrls: false,
2626
ieCompat: true,
2727
strictMath: false,
28-
strictUnits: false
28+
strictUnits: false,
29+
globalVariables: '',
30+
modifyVariables: ''
2931
};
3032
var continueProcessing = true,
3133
currentErrorcode;
@@ -53,6 +55,11 @@ var checkBooleanArg = function(arg) {
5355
return Boolean(onOff[2]);
5456
};
5557

58+
var parseVariableOption = function(option) {
59+
var parts = option.split('=', 2);
60+
return '@' + parts[0] + ': ' + parts[1] + ';\n';
61+
};
62+
5663
var warningMessages = "";
5764
var sourceMapFileInline = false;
5865

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

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

7077
switch (arg) {
@@ -189,6 +196,16 @@ args = args.filter(function (arg) {
189196
options.strictUnits = checkBooleanArg(match[2]);
190197
}
191198
break;
199+
case "global-var":
200+
if (checkArgFunc(arg, match[2])) {
201+
options.globalVariables += parseVariableOption(match[2]);
202+
}
203+
break;
204+
case "modify-var":
205+
if (checkArgFunc(arg, match[2])) {
206+
options.modifyVariables += parseVariableOption(match[2]);
207+
}
208+
break;
192209
}
193210
});
194211

@@ -267,6 +284,8 @@ var parseLessFile = function (e, data) {
267284
return;
268285
}
269286

287+
data = options.globalVariables + data + options.modifyVariables;
288+
270289
options.paths = [path.dirname(input)].concat(options.paths);
271290
options.filename = input;
272291

lib/less/browser.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ if (dumpLineNumbers) {
5252
var typePattern = /^text\/(x-)?less$/;
5353
var cache = null;
5454
var fileCache = {};
55+
var varsPre = "";
5556

5657
function log(str, level) {
5758
if (less.env == 'development' && typeof(console) !== 'undefined' && less.logLevel >= level) {
@@ -294,9 +295,15 @@ function loadStyles(newVars) {
294295
var env = new less.tree.parseEnv(less),
295296
lessText = style.innerHTML || '';
296297
env.filename = document.location.href.replace(/#.*$/, '');
297-
if (newVars) {
298+
299+
if (newVars || varsPre) {
298300
env.useFileCache = true;
299-
lessText += "\n" + newVars;
301+
302+
lessText = varsPre + lessText;
303+
304+
if (newVars) {
305+
lessText += "\n" + newVars;
306+
}
300307
}
301308

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

501508
doXHR(href, env.mime, function (data, lastModified) {
509+
data = varsPre + data;
510+
502511
// per file cache
503512
fileCache[href] = data;
504513

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

521-
if (newVars) {
530+
if (newVars || varsPre) {
522531
env.useFileCache = true;
523532
}
524533

@@ -585,6 +594,18 @@ function initRunningMode(){
585594
}
586595
}
587596

597+
function serializeVars(vars) {
598+
var s = "";
599+
600+
for (var name in vars) {
601+
s += ((name.slice(0,1) === '@')? '' : '@') + name +': '+
602+
((vars[name].slice(-1) === ';')? vars[name] : vars[name] +';');
603+
}
604+
605+
return s;
606+
}
607+
608+
588609
//
589610
// Watch mode
590611
//
@@ -627,12 +648,7 @@ for (var i = 0; i < links.length; i++) {
627648
// CSS without reloading less-files
628649
//
629650
less.modifyVars = function(record) {
630-
var newVars = "";
631-
for (var name in record) {
632-
newVars += ((name.slice(0,1) === '@')? '' : '@') + name +': '+
633-
((record[name].slice(-1) === ';')? record[name] : record[name] +';');
634-
}
635-
less.refresh(false, newVars);
651+
less.refresh(false, serializeVars(record));
636652
};
637653

638654
less.refresh = function (reload, newVars) {
@@ -659,6 +675,10 @@ less.refresh = function (reload, newVars) {
659675
loadStyles(newVars);
660676
};
661677

678+
if (less.globalVars) {
679+
varsPre = serializeVars(less.globalVars) + "\n";
680+
}
681+
662682
less.refreshStyles = loadStyles;
663683

664684
less.Parser.fileLoader = loadFile;

lib/less/lessc_helper.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ var lessc_helper = {
5252
console.log(" be removed in the future.");
5353
console.log(" -su=on|off Allow mixed units, e.g. 1px+1em or 1px*1px which have units");
5454
console.log(" --strict-units=on|off that cannot be represented.");
55+
console.log(" --global-var='VAR=VALUE' Defines a variable that can be referenced by the file.");
56+
console.log(" --modify-var='VAR=VALUE' Modifies a variable already declared in the file.");
5557
console.log("");
5658
console.log("-------------------------- Deprecated ----------------");
5759
console.log(" -O0, -O1, -O2 Set the parser's optimization level. The lower");
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.test {
2+
color: #ff0000;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.test {
2+
color: @global-var;
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var less = {};
2+
less.globalVars = {
3+
"@global-var": "red"
4+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
describe("less.js global vars", function() {
2+
testLessEqualsInDocument();
3+
});

0 commit comments

Comments
 (0)