move render to be accessible in all environments and start using it in the browser

This commit is contained in:
Luke Page
2014-09-03 15:07:33 +01:00
parent 8fc964dc34
commit a0658a4b15
4 changed files with 74 additions and 51 deletions

View File

@@ -312,37 +312,56 @@ function removeError(path) {
}
}
function clone(obj) {
var cloned = {};
for(var prop in obj) {
if (obj.hasOwnProperty(prop)) {
cloned[prop] = obj[prop];
}
}
return cloned;
}
// only really needed for phantom
function bind(func, thisArg) {
var curryArgs = Array.prototype.slice.call(arguments, 2);
return function() {
var args = curryArgs.concat(Array.prototype.slice.call(arguments, 0));
return func.apply(thisArg, args);
};
}
function loadStyles(modifyVars) {
var styles = document.getElementsByTagName('style'),
style;
for (var i = 0; i < styles.length; i++) {
style = styles[i];
if (style.type.match(typePattern)) {
var env = new less.contexts.parseEnv(options),
lessText = style.innerHTML || '';
env.filename = document.location.href.replace(/#.*$/, '');
var instanceOptions = clone(options);
instanceOptions.modifyVars = modifyVars;
var lessText = style.innerHTML || '';
instanceOptions.filename = document.location.href.replace(/#.*$/, '');
if (modifyVars || options.globalVars) {
env.useFileCache = true;
if (modifyVars || instanceOptions.globalVars) {
instanceOptions.useFileCache = true;
}
/*jshint loopfunc:true */
// use closure to store current value of i
var callback = (function(style) {
return function (e, cssAST) {
if (e) {
return error(e, "inline");
}
var css = cssAST.toCSS(options);
// use closure to store current style
less.render(lessText, instanceOptions)
.then(
bind(function(style, css) {
style.type = 'text/css';
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.innerHTML = css;
}
};
})(style);
new(less.Parser)(env).parse(lessText, callback, {globalVars: options.globalVars, modifyVars: modifyVars});
}, null, style),
function(e) {
error(e, "inline");
});
}
}
}
@@ -417,7 +436,7 @@ function initRunningMode(){
if (e) {
error(e, sheet.href);
} else if (root) {
var styles = root.toCSS(less);
var styles = root.toCSS(options);
styles = postProcessCSS(styles);
createCSS(styles, sheet, env.lastModified);
}
@@ -477,20 +496,20 @@ less.refresh = function (reload, modifyVars) {
var startTime, endTime;
startTime = endTime = new Date();
loadStyleSheets(function (e, root, _, sheet, env) {
loadStyleSheets(function (e, root, _, sheet, webInfo) {
if (e) {
return error(e, sheet.href);
}
if (env.local) {
if (webInfo.local) {
log("loading " + sheet.href + " from cache.", logLevel.info);
} else {
log("parsed " + sheet.href + " successfully.", logLevel.debug);
var styles = root.toCSS(options);
styles = postProcessCSS(styles);
createCSS(styles, sheet, env.lastModified);
createCSS(styles, sheet, webInfo.lastModified);
}
log("css for " + sheet.href + " generated in " + (new Date() - endTime) + 'ms', logLevel.info);
if (env.remaining === 0) {
if (webInfo.remaining === 0) {
log("less has finished. css generated in " + (new Date() - startTime) + 'ms', logLevel.info);
}
endTime = new Date();

View File

@@ -1,37 +1,6 @@
var PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise;
var environment = require("./environment/node");
var less = require("./non-node-index.js")(environment);
less.render = function (input, options, callback) {
options = options || {};
if (typeof(options) === 'function') {
callback = options;
options = {};
}
var parser = new(less.Parser)(options);
if (callback) {
parser.parse(input, function (e, root) {
if (e) { callback(e); return; }
var css;
try {
css = root && root.toCSS && root.toCSS(options);
}
catch (err) { callback(err); return; }
callback(null, css);
}, options);
} else {
return new PromiseConstructor(function (resolve, reject) {
parser.parse(input, function (e, root) {
if (e) { return reject(e); }
try { resolve(root.toCSS(options)); }
catch (err) { reject( err); }
}, options);
});
}
};
less.formatError = function(ctx, options) {
options = options || {};

View File

@@ -9,6 +9,7 @@ module.exports = function(environment) {
contexts: require("./contexts.js"),
environment: environment
};
less.render = require("./render.js")(less.Parser);
return less;
};

34
lib/less/render.js Normal file
View File

@@ -0,0 +1,34 @@
var PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise;
module.exports = function(Parser) {
return function (input, options, callback) {
options = options || {};
if (typeof(options) === 'function') {
callback = options;
options = {};
}
var parser = new(Parser)(options);
if (callback) {
parser.parse(input, function (e, root) {
if (e) { callback(e); return; }
var css;
try {
css = root && root.toCSS && root.toCSS(options);
}
catch (err) { callback(err); return; }
callback(null, css);
}, options);
} else {
return new PromiseConstructor(function (resolve, reject) {
parser.parse(input, function (e, root) {
if (e) { return reject(e); }
try { resolve(root.toCSS(options)); }
catch (err) { reject( err); }
}, options);
});
}
};
};