remove dupe code in minify_js

v1.18.x
Barış Soner Uşaklı 7 years ago
parent 647f1bb3ed
commit 50f4fd5345

@ -164,91 +164,84 @@ function concat(data, callback) {
actions.concat = concat; actions.concat = concat;
function minifyJS_batch(data, callback) { function minifyJS_batch(data, callback) {
async.each(data.files, function (ref, next) { async.each(data.files, function (fileObj, next) {
var srcPath = ref.srcPath; fs.readFile(fileObj.srcPath, 'utf8', function (err, source) {
var destPath = ref.destPath;
var filename = ref.filename;
fs.readFile(srcPath, 'utf8', function (err, file) {
if (err) { if (err) {
return next(err); return next(err);
} }
var scripts = {}; var filesToMinify = [
scripts[filename] = file; {
srcPath: fileObj.srcPath,
try { filename: fileObj.filename,
var minified = uglify.minify(scripts, { source: source,
sourceMap: { },
filename: filename, ];
url: filename + '.map', minifyAndSave({
includeSources: true, files: filesToMinify,
}, destPath: fileObj.destPath,
compress: false, filename: fileObj.filename,
}); }, next);
async.parallel([
async.apply(fs.writeFile, destPath, minified.code),
async.apply(fs.writeFile, destPath + '.map', minified.map),
], next);
} catch (e) {
next(e);
}
}); });
}, callback); }, callback);
} }
actions.minifyJS_batch = minifyJS_batch; actions.minifyJS_batch = minifyJS_batch;
function minifyJS(data, callback) { function minifyJS(data, callback) {
async.mapLimit(data.files, 1000, function (ref, next) { async.mapLimit(data.files, 1000, function (fileObj, next) {
var srcPath = ref.srcPath; fs.readFile(fileObj.srcPath, 'utf8', function (err, source) {
var filename = ref.filename;
fs.readFile(srcPath, 'utf8', function (err, file) {
if (err) { if (err) {
return next(err); return next(err);
} }
next(null, { next(null, {
srcPath: srcPath, srcPath: fileObj.srcPath,
filename: filename, filename: fileObj.filename,
source: file, source: source,
}); });
}); });
}, function (err, files) { }, function (err, filesToMinify) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
var scripts = {}; minifyAndSave({
files.forEach(function (ref) { files: filesToMinify,
if (!ref) { destPath: data.destPath,
return; filename: data.filename,
} }, callback);
});
scripts[ref.filename] = ref.source; }
}); actions.minifyJS = minifyJS;
var minified = uglify.minify(scripts, {
sourceMap: {
filename: data.filename,
url: data.filename + '.map',
includeSources: true,
},
compress: false,
});
if (minified.error) { function minifyAndSave(data, callback) {
return callback(minified.error); var scripts = {};
data.files.forEach(function (ref) {
if (!ref) {
return;
} }
async.parallel([ scripts[ref.filename] = ref.source;
async.apply(fs.writeFile, data.destPath, minified.code), });
async.apply(fs.writeFile, data.destPath + '.map', minified.map),
], callback); var minified = uglify.minify(scripts, {
sourceMap: {
filename: data.filename,
url: data.filename + '.map',
includeSources: true,
},
compress: false,
}); });
if (minified.error) {
return callback(minified.error);
}
async.parallel([
async.apply(fs.writeFile, data.destPath, minified.code),
async.apply(fs.writeFile, data.destPath + '.map', minified.map),
], callback);
} }
actions.minifyJS = minifyJS;
Minifier.js = {}; Minifier.js = {};
Minifier.js.bundle = function (data, minify, fork, callback) { Minifier.js.bundle = function (data, minify, fork, callback) {

Loading…
Cancel
Save