Skip to content
Closed
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
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ less:
@@echo "(function (window, undefined) {" >> ${DIST}
@@cat build/require.js\
build/ecma-5.js\
${SRC}/when.js\
${SRC}/parser.js\
${SRC}/functions.js\
${SRC}/tree/*.js\
Expand All @@ -41,6 +42,7 @@ rhino:
@@touch ${RHINO}
@@cat build/require-rhino.js\
build/ecma-5.js\
${SRC}/when.js\
${SRC}/parser.js\
${SRC}/functions.js\
${SRC}/tree/*.js\
Expand Down
39 changes: 21 additions & 18 deletions benchmark/less-benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ fs.readFile(file, 'utf8', function (e, data) {

start = new(Date);

new(less.Parser)({ optimization: 2 }).parse(data, function (err, tree) {
try {
var root = new(less.Parser)({ optimization: 2 }).parse(data);
end = new(Date);

total = end - start;
Expand All @@ -28,22 +29,24 @@ fs.readFile(file, 'utf8', function (e, data) {
data.length / 1024) + " KB\/s)");

start = new(Date);
css = tree.toCSS();
end = new(Date);

sys.puts("Generation: " + (end - start) + " ms (" +
parseInt(1000 / (end - start) *
data.length / 1024) + " KB\/s)");

total += end - start;

sys.puts("Total: " + total + "ms (" +
parseInt(1000 / total * data.length / 1024) + " KB/s)");

if (err) {
less.writeError(err);
process.exit(3);
}
});
root.toCSS().then(function(css) {
end = new(Date);

sys.puts("Generation: " + (end - start) + " ms (" +
parseInt(1000 / (end - start) *
data.length / 1024) + " KB\/s)");

total += end - start;

sys.puts("Total: " + total + "ms (" +
parseInt(1000 / total * data.length / 1024) + " KB/s)");
}, function(e) {
less.writeError(e);
process.exit(2);
});
} catch (e) {
less.writeError(e);
process.exit(1);
}
});

42 changes: 20 additions & 22 deletions bin/lessc
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,27 @@ var parseLessFile = function (e, data) {
process.exit(1);
}

new(less.Parser)({
paths: [path.dirname(input)].concat(options.paths),
optimization: options.optimization,
filename: input
}).parse(data, function (err, tree) {
if (err) {
less.writeError(err, options);
process.exit(1);
} else {
try {
css = tree.toCSS({ compress: options.compress });
if (output) {
fd = fs.openSync(output, "w");
fs.writeSync(fd, css, 0, "utf8");
} else {
sys.print(css);
}
} catch (e) {
less.writeError(e, options);
process.exit(2);
try {
var root = new(less.Parser)({
paths: [path.dirname(input)].concat(options.paths),
optimization: options.optimization,
filename: input
}).parse(data);
root.toCSS({ compress: options.compress }).then(function(css) {
if (output) {
fd = fs.openSync(output, "w");
fs.writeSync(fd, css, 0, "utf8");
} else {
sys.print(css);
}
}
});
}, function(e) {
less.writeError(e, options);
process.exit(2);
});
} catch (e) {
less.writeError(e, options);
process.exit(1);
}
};

if (input != '-') {
Expand Down
38 changes: 21 additions & 17 deletions lib/less/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ if (less.env === 'development') {
if (less.watchMode) {
loadStyleSheets(function (root, sheet, env) {
if (root) {
createCSS(root.toCSS(), sheet, env.lastModified);
root.toCSS().then(function(css) {
createCSS(css, sheet, env.lastModified);
});
}
});
}
Expand Down Expand Up @@ -78,16 +80,24 @@ less.refresh = function (reload) {
var startTime, endTime;
startTime = endTime = new(Date);

var d = when.defer();

loadStyleSheets(function (root, sheet, env) {
if (env.local) {
log("loading " + sheet.href + " from cache.");
d.resolve();
} else {
log("parsed " + sheet.href + " successfully.");
createCSS(root.toCSS(), sheet, env.lastModified);
root.toCSS().then(function(css) {
createCSS(css, sheet, env.lastModified);
d.resolve();
});
}
log("css for " + sheet.href + " generated in " + (new(Date) - endTime) + 'ms');
(env.remaining === 0) && log("css generated in " + (new(Date) - startTime) + 'ms');
endTime = new(Date);
d.promise.then(function() {
log("css for " + sheet.href + " generated in " + (new(Date) - endTime) + 'ms');
(env.remaining === 0) && log("css generated in " + (new(Date) - startTime) + 'ms');
endTime = new(Date);
});
}, reload);

loadStyles();
Expand All @@ -100,8 +110,8 @@ function loadStyles() {
var styles = document.getElementsByTagName('style');
for (var i = 0; i < styles.length; i++) {
if (styles[i].type.match(typePattern)) {
new(less.Parser)().parse(styles[i].innerHTML || '', function (e, tree) {
var css = tree.toCSS();
var root = new(less.Parser)().parse(styles[i].innerHTML || '');
root.toCSS().then(function(css) {
var style = styles[i];
try {
style.innerHTML = css;
Expand Down Expand Up @@ -146,19 +156,13 @@ function loadStyleSheet(sheet, callback, reload, remaining) {
} else {
// Use remote copy (re-parse)
try {
new(less.Parser)({
var root = new(less.Parser)({
optimization: less.optimization,
paths: [href.replace(/[\w\.-]+$/, '')],
mime: sheet.type
}).parse(data, function (e, root) {
if (e) { return error(e, href) }
try {
callback(root, sheet, { local: false, lastModified: lastModified, remaining: remaining });
removeNode(document.getElementById('less-error-message:' + extractId(href)));
} catch (e) {
error(e, href);
}
});
}).parse(data);
callback(root, sheet, { local: false, lastModified: lastModified, remaining: remaining });
removeNode(document.getElementById('less-error-message:' + extractId(href)));
} catch (e) {
error(e, href);
}
Expand Down
40 changes: 25 additions & 15 deletions lib/less/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,26 @@ var less = {
ee;

if (callback) {
parser.parse(input, function (e, root) {
callback(e, root.toCSS(options));
});
var root = parser.parse(input);
root.toCSS(options).then(function(result) {
callback(null, result);
}, function(e) {
callback(e);
})
} else {
ee = new(require('events').EventEmitter);

process.nextTick(function () {
parser.parse(input, function (e, root) {
if (e) { ee.emit('error', e) }
else { ee.emit('success', root.toCSS(options)) }
});
try {
var root = parser.parse(input);
root.toCSS(options).then(function(css) {
ee.emit('success', css);
}, function(e) {
ee.emit('error', e);
})
} catch (e) {
ee.emit('error', e);
}
});
return ee;
}
Expand All @@ -39,9 +48,8 @@ var less = {
var message = "";
var extract = ctx.extract;
var error = [];
var stylize = options.color ? less.stylize : function (str) { return str };

options = options || {};
var stylize = options.color ? less.stylize : function (str) { return str };

if (options.silent) { return }

Expand Down Expand Up @@ -103,13 +111,15 @@ less.Parser.importer = function (file, paths, callback) {
fs.readFile(pathname, 'utf-8', function(e, data) {
if (e) sys.error(e);

new(less.Parser)({
paths: [path.dirname(pathname)],
filename: pathname
}).parse(data, function (e, root) {
if (e) less.writeError(e);
try {
var root = new(less.Parser)({
paths: [path.dirname(pathname)],
filename: pathname
}).parse(data);
callback(root);
});
} catch(e) {
less.writeError(e);
}
});
} else {
sys.error("file '" + file + "' wasn't found.\n");
Expand Down
Loading