Comparing version 0.4.17 to 0.4.18
@@ -148,3 +148,4 @@ var _ = require('underscore'); | ||
if (self.config) { | ||
return callback(null, self.config); | ||
callback(null, self.config); | ||
return; | ||
} | ||
@@ -154,6 +155,27 @@ | ||
if (err) { | ||
return callback(err); | ||
callback(err); | ||
return; | ||
} | ||
self.config = json; | ||
callback(null, self.config); | ||
if (!json.plugins) { | ||
json.plugins = {}; | ||
} | ||
self.app.getConfig(function(err, appCfg) { | ||
if (err) { | ||
next(err); | ||
return; | ||
} | ||
if (appCfg.plugins) { | ||
_.defaults(json.plugins, appCfg.plugins); | ||
} | ||
self.config = json; | ||
callback(null, self.config); | ||
}); | ||
}); | ||
@@ -481,3 +503,4 @@ }, | ||
if (err) { | ||
return callback(err); | ||
callback(err); | ||
return; | ||
} | ||
@@ -484,0 +507,0 @@ if (report) { |
@@ -9,7 +9,8 @@ var fs = require('fs'); | ||
var uglify = function (from_file, to_file, gen_config, done) { | ||
var uglify = function (from_file, to_file, options, done) { | ||
fs.readFile(from_file, 'utf8', function (err, content) { | ||
if (err) { | ||
return done(err); | ||
done(err); | ||
return; | ||
} | ||
@@ -22,8 +23,9 @@ | ||
ast = jsp.parse(content); | ||
ast = pro.ast_mangle(ast); | ||
ast = pro.ast_squeeze(ast); | ||
compressed = pro.gen_code(ast, gen_config); | ||
ast = pro.ast_mangle(ast, options.ast_mangle); | ||
ast = pro.ast_squeeze(ast, options.ast_squeeze); | ||
compressed = pro.gen_code(ast, options.gen_code); | ||
} catch (ex) { | ||
ex.message = ex.message + ' [' + from_file + ']'; | ||
return done(ex); | ||
done(ex); | ||
return; | ||
} | ||
@@ -34,3 +36,3 @@ | ||
}); | ||
} | ||
}; | ||
@@ -41,18 +43,23 @@ /** | ||
* minify xx.js to xx-min.js | ||
* @param {[type]} page [description] | ||
* @param {Function} next [description] | ||
* @return {[type]} [description] | ||
*/ | ||
module.exports = function (config) { | ||
module.exports = function () { | ||
// plugin default options | ||
var options = { | ||
ast_mangle: { | ||
config = _.extend({ | ||
ascii_only: true | ||
}, config); | ||
var options = { | ||
ascii_only: true | ||
}, | ||
ast_squeeze: { | ||
ascii_only: true, | ||
make_seqs: true, //which will cause consecutive statements in a block | ||
// to be merged using the “sequence” (comma) operator | ||
dead_code: true //which will remove unreachable code. | ||
}, | ||
gen_code: { | ||
beautify: false, | ||
quote_keys: true, | ||
ascii_only: true | ||
} | ||
}; | ||
return function (page, next) { | ||
var start_time = new Date(); | ||
var reports = { | ||
@@ -66,9 +73,16 @@ name: 'uglifyjs', | ||
var dir = page.destDir; | ||
var config = {}; | ||
if (page.config.uglifyjs) { | ||
_.extend(config, page.config.uglifyjs); | ||
} | ||
if (config.options) { | ||
_.extend(options, config.options); | ||
if (page.config.plugins && page.config.plugins.uglifyjs) { | ||
var pluginConfig = page.config.plugins.uglifyjs; | ||
_.chain(options) | ||
.keys() | ||
.each(function(key){ | ||
if (key in pluginConfig) { | ||
_.defaults(pluginConfig[key], options[key]); | ||
} else { | ||
pluginConfig[key] = options[key]; | ||
} | ||
}) | ||
} | ||
@@ -78,5 +92,6 @@ | ||
if (err) { | ||
return next(err); | ||
next(err); | ||
return; | ||
} | ||
//去掉压缩版的 | ||
@@ -87,3 +102,2 @@ list = _.filter(list, function(p){ | ||
async.forEach(list, function(file, callback) { | ||
@@ -93,13 +107,8 @@ reports.files.push(file); | ||
var to_file = from_file.replace(/\.js$/i, '-min.js'); | ||
// 检查压缩版是否已存在, 有则忽略之 | ||
fs.stat(to_file, function (err, stat) { | ||
if (err) { | ||
uglify(from_file, to_file, options, callback); | ||
return; | ||
} | ||
callback(null); | ||
}); | ||
uglify(from_file, to_file, pluginConfig, callback); | ||
}, function (err) { | ||
if (err) { | ||
return next(err); | ||
next(err); | ||
return; | ||
} | ||
@@ -109,4 +118,4 @@ next(null, reports); | ||
}); | ||
} | ||
}; | ||
}; | ||
@@ -5,3 +5,3 @@ { | ||
"description": "build front project", | ||
"version": "0.4.17", | ||
"version": "0.4.18", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
1570024
39284