improve architect-build/compress

pull/483/head
nightwing 2018-01-24 14:06:18 +04:00
rodzic 8a93c7aa30
commit 0adebbf2e9
3 zmienionych plików z 47 dodań i 14 usunięć

4
node_modules/architect-build/build.js wygenerowano vendored
Wyświetl plik

@ -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 + "; " + _;
}

Wyświetl plik

@ -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();
}

55
node_modules/architect-build/compress.js wygenerowano vendored
Wyświetl plik

@ -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))