Permalink
Browse files

ENYO-554: Add support for compiling .less files as part of minifier, …

…along with command-line option to disable Less compilation. Requires some async refactoring due Less parser.

Enyo-DCO-1.0-Signed-off-by: Kevin Schaaf <kevin.schaaf@palm.com>
  • Loading branch information...
Kevin Schaaf
Kevin Schaaf committed Oct 9, 2012
1 parent 15eb0cf commit 434795cc4e0847a5fdafebe84770c357654c8d64
Showing with 79 additions and 47 deletions.
  1. +1 −1 minify/minify.bat
  2. +76 −45 tools/minifier/minify.js
  3. +1 −1 tools/minify.bat
  4. +1 −0 tools/minify.sh
View
@@ -2,5 +2,5 @@
REM FIXME: minify has a problem if 'package.js' is not in CWD, hence push, cd, pop
PUSHD "%CD%"
CD "%~dp0"
-CALL ..\tools\minify.bat package.js -no-alias -output ..\..\build\enyo
+CALL ..\tools\minify.bat package.js -no-alias -output ..\..\build\enyo %*
POPD
View
@@ -4,7 +4,8 @@ var
walker = require("walker"),
jsp = require("uglify-js").parser,
pro = require("uglify-js").uglify,
- nopt = require("nopt")
+ nopt = require("nopt"),
+ less = require("less")
;
// Shimming path.relative with 0.8.8's version if it doesn't exist
@@ -15,6 +16,7 @@ if(!path.relative){
function printUsage() {
w("Enyo 2.0 Minifier");
w("Flags:");
+ w("-no-less:", "Don't compile less; instad substitute css for less");
w("-no-alias:", "Don't use path macros");
w("-alias:", "Give paths a macroized alias");
w("-enyo ENYOPATH:", "Path to enyo loader (enyo/enyo.js)");
@@ -39,34 +41,62 @@ buildPathBlock = function(loader) {
return !p ? "" : "\n// minifier: path aliases\n\nenyo.path.addPaths({" + p + "});\n";
};
-concatCss = function(loader) {
+concatCss = function(loader, doneCB) {
w("");
var blob = "";
- var repl = function(inMatch) {
- // find the url path, ignore quotes in url string
- var matches = /url\s*\(\s*(('([^']*)')|("([^"]*)")|([^'"]*))\s*\)/.exec(inMatch);
- var urlPath = matches[3] || matches[5] || matches[6];
- // skip data urls
- if (/^data:/.test(urlPath)) {
- return "url(" + urlPath + ")";
- }
- // get absolute path to referenced asset
- var normalizedUrlPath = path.join(s, "..", urlPath);
- // Make relative asset path to built css
- var relPath = path.relative(path.dirname(opt.output || "build"), normalizedUrlPath);
- if (process.platform == "win32") {
- relPath = pathSplit(relPath).join("/");
- }
- return "url(" + relPath + ")";
- };
- for (var i=0, s; (s=loader.sheets[i]); i++) {
- w(s);
- var code = fs.readFileSync(s, "utf8");
+ var addToBlob = function(sheet, code) {
// fix url paths
- code = code.replace(/url\([^)]*\)/g, repl);
- blob += "\n/* " + s + " */\n\n" + code + "\n";
+ code = code.replace(/url\([^)]*\)/g, function(inMatch) {
+ // find the url path, ignore quotes in url string
+ var matches = /url\s*\(\s*(('([^']*)')|("([^"]*)")|([^'"]*))\s*\)/.exec(inMatch);
+ var urlPath = matches[3] || matches[5] || matches[6];
+ // skip data urls
+ if (/^data:/.test(urlPath)) {
+ return "url(" + urlPath + ")";
+ }
+ // get absolute path to referenced asset
+ var normalizedUrlPath = path.join(sheet, "..", urlPath);
+ // Make relative asset path to built css
+ var relPath = path.relative(path.dirname(opt.output || "build"), normalizedUrlPath);
+ if (process.platform == "win32") {
+ relPath = pathSplit(relPath).join("/");
+ }
+ return "url(" + relPath + ")";
+ });
+ blob += "\n/* " + sheet + " */\n\n" + code + "\n";
}
- return blob;
+ // Pops one sheet off the sheets[] array, reads (and parses if less), and then
+ // recurses again from the async callback until no sheets left, then calls doneCB
+ var readAndParse = function(sheets) {
+ var sheet = sheets.shift();
+ if (sheet) {
+ w(sheet);
+ var isLess = (sheet.slice(-4) == "less");
+ if (isLess && (opt.less !== undefined)) {
+ sheet = sheet.slice(0, sheet.length-4) + "css";
+ isLess = false;
+ w(" (Substituting CSS: " + sheet + ")");
+ }
+ var code = fs.readFileSync(sheet, "utf8");
+ if (isLess) {
+ var parser = new(less.Parser)({filename:sheet, paths:[path.dirname(sheet)]});
+ parser.parse(code, function (err, tree) {
+ if (err) {
+ console.error(err);
+ } else {
+ addToBlob(sheet, tree.toCSS());
+ }
+ readAndParse(sheets);
+ });
+ } else {
+ addToBlob(sheet, code);
+ readAndParse(sheets);
+ }
+ } else {
+ doneCB(blob);
+ }
+ }
+ readAndParse(loader.sheets);
};
concatJs = function(loader) {
@@ -110,28 +140,29 @@ finish = function(loader) {
if (outfolder != "." && !exists(outfolder)) {
fs.mkdirSync(outfolder);
}
- //
- var css = concatCss(loader);
- if (css.length) {
+ // Unfortunately, less parsing is asynchronous, so concatCSS is now as well
+ concatCss(loader, function(css) {
+ if (css.length) {
+ w("");
+ fs.writeFileSync(output + ".css", css, "utf8");
+ }
+ //
+ var js = concatJs(loader);
+ /*
+ if (css.length) {
+ js += "\n// " + "minifier: load css" + "\n\n";
+ js += 'enyo.machine.sheet("' + output + '.css");\n';
+ }
+ */
+ if (js.length) {
+ w("");
+ fs.writeFileSync(output + ".js", js, "utf8");
+ }
+ //
w("");
- fs.writeFileSync(output + ".css", css, "utf8");
- }
- //
- var js = concatJs(loader);
- /*
- if (css.length) {
- js += "\n// " + "minifier: load css" + "\n\n";
- js += 'enyo.machine.sheet("' + output + '.css");\n';
- }
- */
- if (js.length) {
+ w("done.");
w("");
- fs.writeFileSync(output + ".js", js, "utf8");
- }
- //
- w("");
- w("done.");
- w("");
+ });
};
w = console.log;
View
@@ -14,4 +14,4 @@ REM node location
SET NODE=node.exe
REM use node to invoke minify with a known path to enyo and imported parameters
-%NODE% "%MINIFY%" -enyo "%ENYO%" %1 %2 %3 %4 %5 %6
+%NODE% "%MINIFY%" -enyo "%ENYO%" %*
View
@@ -10,6 +10,7 @@ MINIFY="$TOOLS/minifier/minify.js"
# check for node, but quietly
if command -v node >/dev/null 2>&1; then
# use node to invoke minify with a known path to enyo and imported parameters
+ echo "enyo/tools/minify.sh args: " $@
node "$MINIFY" -enyo "$ENYO" $@
else
echo "No node found in path"

0 comments on commit 434795c

Please sign in to comment.