mirror of
https://github.com/less/less.js.git
synced 2026-04-09 03:00:20 -04:00
Support specifying custom variables when calling lessc and less.js.
Both lessc and less.js can now be provided with global variables that all .less files will have immediate access to. This can be used to provide, for example, a base path for an @import, signed URLs offering temporary access to an image on S3, or anything else. lessc has two new parameters, --global-var and --modify-var. Both take a value of the form "varname=value". --global-var declares variables immediately before the content of the .less files, and --modify-var declares them after. --global-var is used when rules, imports, or other variables will depend on the provided variable. --modify-var is used to override a variable declared within the .less file. less.js's equivalent for global variables is less.globalVars. This can be set before loading less.js. There is no new requivalent to --modify-var, as less.modifyVars can be used for that purpose.
This commit is contained in:
@@ -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) {
|
||||
@@ -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 */
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
//
|
||||
@@ -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) {
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user