Skip to content

Commit

Permalink
Change code to use less.render
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeapage committed Sep 3, 2014
1 parent d0b6c50 commit 544bd3a
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 98 deletions.
85 changes: 38 additions & 47 deletions bin/lessc
Original file line number Diff line number Diff line change
Expand Up @@ -342,60 +342,51 @@ var parseLessFile = function (e, data) {
options.paths = [path.dirname(input)].concat(options.paths);
options.filename = input;

var parser = new(less.Parser)(options);
parser.parse(data, function (err, tree) {
if (err) {
less.writeError(err, options);
currentErrorcode = 1;
return;
} else if (options.depends) {
if (options.depends) {
var parser = new(less.Parser)(options);
parser.parse(data, function (err, tree) {
if (err) {
less.writeError(err, options);
currentErrorcode = 1;
return;
}
for(var file in parser.imports.files) {
if (parser.imports.files.hasOwnProperty(file)) {
process.stdout.write(file + " ");
}
}
console.log("");
} else {
try {
if (options.lint) { writeSourceMap = function() {}; }
var css = tree.toCSS({
silent: options.silent,
verbose: options.verbose,
ieCompat: options.ieCompat,
compress: options.compress,
cleancss: options.cleancss,
cleancssOptions: cleancssOptions,
sourceMap: Boolean(options.sourceMap),
sourceMapFilename: options.sourceMap,
sourceMapURL: options.sourceMapURL,
sourceMapOutputFilename: options.sourceMapOutputFilename,
sourceMapBasepath: options.sourceMapBasepath,
sourceMapRootpath: options.sourceMapRootpath || "",
outputSourceFiles: options.outputSourceFiles,
writeSourceMap: writeSourceMap,
maxLineLen: options.maxLineLen,
strictMath: options.strictMath,
strictUnits: options.strictUnits,
urlArgs: options.urlArgs
});
if(!options.lint) {
if (output) {
ensureDirectory(output);
fs.writeFileSync(output, css, 'utf8');
if (options.verbose) {
console.log('lessc: wrote ' + output);
}
} else {
process.stdout.write(css);
}
}
} catch (e) {
less.writeError(e, options);
currentErrorcode = 2;
return;
});
return;
}

if (options.lint) {
options.writeSourceMap = function() {};
} else {
options.writeSourceMap = writeSourceMap;
}
options.sourceMapFilename = options.sourceMap;
options.sourceMap = Boolean(options.sourceMap);
options.sourceMapRootpath = options.sourceMapRootpath || "";

less.render(data, options)
.then(function(css) {
if(!options.lint) {
if (output) {
ensureDirectory(output);
fs.writeFileSync(output, css, 'utf8');
if (options.verbose) {
console.log('lessc: wrote ' + output);
}
} else {
process.stdout.write(css);
}
}
}
});
},
function(err) {
less.writeError(err, options);
currentErrorcode = 1;
});
};

if (input != '-') {
Expand Down
55 changes: 27 additions & 28 deletions lib/less/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,7 @@ function loadStyles(modifyVars) {
/*jshint loopfunc:true */
// use closure to store current style
less.render(lessText, instanceOptions)
.then(
bind(function(style, css) {
.then(bind(function(style, css) {
style.type = 'text/css';
if (style.styleSheet) {
style.styleSheet.cssText = css;
Expand All @@ -368,23 +367,26 @@ function loadStyles(modifyVars) {

function loadStyleSheet(sheet, callback, reload, remaining, modifyVars) {

var env = new less.contexts.parseEnv(options);
env.mime = sheet.type;
var instanceOptions = clone(options);
instanceOptions.mime = sheet.type;

if (modifyVars) {
instanceOptions.modifyVars = modifyVars;
}
if (modifyVars || options.globalVars) {
env.useFileCache = true;
instanceOptions.useFileCache = true;
}

less.environment.loadFile(env, sheet.href, null, function loadInitialFileCallback(e, data, path, webInfo) {
less.environment.loadFile(instanceOptions, sheet.href, null, function loadInitialFileCallback(e, data, path, webInfo) {

var newFileInfo = {
currentDirectory: less.environment.getPath(env, path),
currentDirectory: less.environment.getPath(instanceOptions, path),
filename: path,
rootFilename: path,
relativeUrls: env.relativeUrls};
relativeUrls: instanceOptions.relativeUrls};

newFileInfo.entryPath = newFileInfo.currentDirectory;
newFileInfo.rootpath = env.rootpath || newFileInfo.currentDirectory;
newFileInfo.rootpath = instanceOptions.rootpath || newFileInfo.currentDirectory;

if (webInfo) {
webInfo.remaining = remaining;
Expand All @@ -407,19 +409,18 @@ function loadStyleSheet(sheet, callback, reload, remaining, modifyVars) {
removeError(path);

if (data) {
env.currentFileInfo = newFileInfo;
new(less.Parser)(env).parse(data, function (e, root) {
if (e) { return callback(e, null, null, sheet); }
try {
callback(e, root, data, sheet, webInfo, path);
} catch (e) {
instanceOptions.currentFileInfo = newFileInfo;
less.render(data, instanceOptions)
.then(function(css) {
callback(null, css, data, sheet, webInfo, path);
},
function(e) {
callback(e, null, null, sheet);
}
}, {modifyVars: modifyVars, globalVars: options.globalVars});
});
} else {
callback(e, null, null, sheet, webInfo, path);
}
}, env, modifyVars);
});
}

function loadStyleSheets(callback, reload, modifyVars) {
Expand All @@ -432,13 +433,12 @@ function initRunningMode(){
if (less.env === 'development') {
less.watchTimer = setInterval(function () {
if (less.watchMode) {
loadStyleSheets(function (e, root, _, sheet, env) {
loadStyleSheets(function (e, css, _, sheet, env) {
if (e) {
error(e, sheet.href);
} else if (root) {
var styles = root.toCSS(options);
styles = postProcessCSS(styles);
createCSS(styles, sheet, env.lastModified);
} else if (css) {
css = postProcessCSS(css);
createCSS(css, sheet, env.lastModified);
}
});
}
Expand Down Expand Up @@ -496,17 +496,16 @@ less.refresh = function (reload, modifyVars) {
var startTime, endTime;
startTime = endTime = new Date();

loadStyleSheets(function (e, root, _, sheet, webInfo) {
loadStyleSheets(function (e, css, _, sheet, webInfo) {
if (e) {
return error(e, sheet.href);
}
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, webInfo.lastModified);
log("rendered " + sheet.href + " successfully.", logLevel.debug);
css = postProcessCSS(css);
createCSS(css, sheet, webInfo.lastModified);
}
log("css for " + sheet.href + " generated in " + (new Date() - endTime) + 'ms', logLevel.info);
if (webInfo.remaining === 0) {
Expand Down
34 changes: 11 additions & 23 deletions test/less-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ module.exports = function() {
}

function toCSS(options, path, callback) {
var css;
options = options || {};
fs.readFile(path, 'utf8', function (e, str) {
if (e) { return callback(e); }
Expand All @@ -212,31 +211,20 @@ module.exports = function() {
options.filename = require('path').resolve(process.cwd(), path);
options.optimization = options.optimization || 0;

var parser = new(less.Parser)(options);
var additionalData = {};
if (options.globalVars) {
additionalData.globalVars = options.getVars(path);
options.globalVars = options.getVars(path);
} else if (options.modifyVars) {
additionalData.modifyVars = options.getVars(path);
options.modifyVars = options.getVars(path);
}
if (options.banner) {
additionalData.banner = options.banner;
}
parser.parse(str, function (err, tree) {
if (err) {
callback(err);
} else {
try {
css = tree.toCSS(options);
var css2 = tree.toCSS(options); // integration test that 2nd call gets same output
if (css2 !== css) { throw new Error("css not equal to 2nd call"); }
callback(null, css);
} catch (e) {
callback(e);
}
}
}, additionalData);
});

less.render(str, options)
.then(function(css) {
// TODO integration test that calling toCSS twice results in the same css?
callback(null, css);
}, function(e) {
callback(e);
});
});
}

function testNoOptions() {
Expand Down

0 comments on commit 544bd3a

Please sign in to comment.