From 0adebbf2e951278989333d787979ac7a0ad050d6 Mon Sep 17 00:00:00 2001 From: nightwing Date: Wed, 24 Jan 2018 14:06:18 +0400 Subject: [PATCH] improve architect-build/compress --- node_modules/architect-build/build.js | 4 +- .../build_support/mini_require.js | 2 +- node_modules/architect-build/compress.js | 55 +++++++++++++++---- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/node_modules/architect-build/build.js b/node_modules/architect-build/build.js index 37e3007a..7696cce1 100644 --- a/node_modules/architect-build/build.js +++ b/node_modules/architect-build/build.js @@ -124,7 +124,7 @@ function build(config, opts, callback) { // Concatenate all files using uglify2 with source maps var result; if (opts.compress) - result = require("./compress")(sources, opts); + result = require("./compress").withCache(sources, opts); else { result = { code : sources.map(function(src){ return src.source.trim(); }).join("\n\n") + "\n", @@ -301,7 +301,7 @@ function checkImages(css, opts, cache) { } function addCssPrefixes(css) { - return css.replace(/\b(user-select|font-smoothing)\b([^;\n]+);?/g, function(_, prop, value, index, string) { + return css.replace(/\b(user-select|font-smoothing)\b([^;}\n]+);?/g, function(_, prop, value, index, string) { if (prop[0] == "u" && string[index - 1] != "-") { return "-webkit-" + prop + value + "; -moz-" + prop + value + "; -ms-" + prop + value + "; " + _; } diff --git a/node_modules/architect-build/build_support/mini_require.js b/node_modules/architect-build/build_support/mini_require.js index 5e723adc..7be11ca4 100644 --- a/node_modules/architect-build/build_support/mini_require.js +++ b/node_modules/architect-build/build_support/mini_require.js @@ -286,7 +286,7 @@ var config = require.config = function(cfg) { config.paths[p] = cfg.paths[p]; }); - if (cfg.useCache && global.caches && location.protocol == "https:") { + if (cfg.useCache && global.caches && location.protocol === "https:") { config.useCache = true; checkCache(); } diff --git a/node_modules/architect-build/compress.js b/node_modules/architect-build/compress.js index 13b0778d..eb9f80f8 100644 --- a/node_modules/architect-build/compress.js +++ b/node_modules/architect-build/compress.js @@ -1,5 +1,5 @@ var UglifyJS = require("uglify-js"); - +var fs = require("fs"); function compress(sources, opts) { if (!opts) @@ -10,23 +10,33 @@ function compress(sources, opts) { var toplevel = null; var literals = []; - sources.forEach(function(pkg){ - if (/"disable compress"/.test(pkg.source)) { + sources.forEach(function(pkg) { + if (pkg.source == undefined && pkg.file) + pkg.source = fs.readFileSync(pkg.file, "utf8"); + + if (/"disable compress"/.test(pkg.source)) return literals.push(pkg.source); - } // if (pkg.file) console.log("Adding '" + pkg.file + "'."); - - toplevel = UglifyJS.parse(pkg.source, { - filename: (pkg.file || pkg.id).replace(new RegExp("^" + opts.basepath + "/"), ""), //@todo remove prefix - toplevel: toplevel - }); + try { + toplevel = UglifyJS.parse(pkg.source, { + filename: (pkg.file || pkg.id || "").replace(new RegExp("^" + opts.basepath + "/"), ""), //@todo remove prefix + toplevel: toplevel + }); + } catch (e) { + logParseError(e, pkg); + throw e; + } }); if (!toplevel) { return { code: literals.join("\n"), map: null }; } + if (opts.compress == "dryrun") { + return { code: "dryrun\n" }; + } + /** * UglifyJS contains a scope analyzer that you need to call manually before * compressing or mangling. Basically it augments various nodes in the AST @@ -80,6 +90,10 @@ function compress(sources, opts) { outputOptions.source_map = source_map; } var stream = UglifyJS.OutputStream(outputOptions); + UglifyJS.AST_Node.warn_function = function(txt) { + if (txt && txt.startsWith("Output exceeds")) return; + console.error("WARN: %s", txt); + }; compressed_ast.print(stream); function asciify(text) { @@ -102,13 +116,32 @@ function compress(sources, opts) { }; } +function logParseError(e, pkg) { + console.error("Error while parsing", (pkg.file || pkg.id) + ":" + e.line + ":" + e.col); + if (e.line) { + var MAX_LEN = 45; + var line = pkg.source.split("\n")[e.line - 1]; + var col = e.col; + if (col > MAX_LEN) { + line = line.slice(col - MAX_LEN); + col = MAX_LEN; + } + if (line.length > col + MAX_LEN) + line = line.slice(0, col + MAX_LEN); + line = line + "\n" + Array(col + 1).join(" ") + "^"; + console.error(line); + } +} + compress.withCache = function(sources, opts) { - var cache = opts.cache; + var cache = opts.cache || {}; if (cache && !cache.compress) cache.compress = Object.create(null); + if (typeof sources == "string") + sources = [{source: sources, file: ""}]; var code = sources.map(function(pkg) { if (pkg.id && cache.compress[pkg.id]) { - console.log("Compress Cache Hit " + pkg.id); + // console.log("Compress Cache Hit " + pkg.id); return cache.compress[pkg.id]; } if (opts.exclude && opts.exclude.test(pkg.id))