Skip to content
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

Support specifying custom variables when calling lessc and less.js. #1655

Merged
merged 1 commit into from
Nov 15, 2013
Merged
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
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] }
Copy link
Member

Choose a reason for hiding this comment

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

whats the meaning of this change? so it goes from all non whitespace to characters?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it's to allow for whitespace characters. Otherwise, it's not possible to do expressions or any other sort of content containing whitespace. The whitespace will only end up in the value if explicitly quoted on the shell.

I looked through the other command line arguments and didn't see any that seemed they would be impacted by this change, and sys.argv already handles whitespace between arguments. I may have missed something important though.

Copy link
Member

Choose a reason for hiding this comment

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

thanks, makes sense.

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();
});