Minify modules in a batch

This commit is contained in:
Peter Jaszkowiak
2017-05-18 21:20:04 -06:00
parent e8caee3c4c
commit 9f5ce24993
2 changed files with 109 additions and 78 deletions

View File

@@ -88,44 +88,56 @@ module.exports = function (Meta) {
},
};
function copyFile(source, target, cb) {
var called = false;
var rd = fs.createReadStream(source);
rd.on('error', done);
var wr = fs.createWriteStream(target);
wr.on('error', done);
wr.on('close', function () {
done();
});
rd.pipe(wr);
function done(err) {
if (!called) {
cb(err);
called = true;
}
}
}
function minifyModules(modules, fork, callback) {
// for it to never fork
// otherwise it spawns way too many processes
// maybe eventually we can pool modules
// and pass the pools to the minifer
// to reduce the total number of threads
fork = false;
async.eachLimit(modules, 1000, function (mod, next) {
mkdirp(path.dirname(mod.destPath), next);
}, function (err) {
if (err) {
return callback(err);
}
async.eachLimit(modules, 500, function (mod, next) {
var srcPath = mod.srcPath;
var destPath = mod.destPath;
async.parallel({
dirped: function (cb) {
mkdirp(path.dirname(destPath), cb);
},
minified: function (cb) {
fs.readFile(srcPath, function (err, buffer) {
if (err) {
return cb(err);
}
if (srcPath.endsWith('.min.js') || path.dirname(srcPath).endsWith('min')) {
return cb(null, { code: buffer.toString() });
}
minifier.js.minify(buffer.toString(), fork, cb);
});
},
}, function (err, results) {
if (err) {
return next(err);
var filtered = modules.reduce(function (prev, mod) {
if (mod.srcPath.endsWith('.min.js') || path.dirname(mod.srcPath).endsWith('min')) {
prev.skip.push(mod);
} else {
prev.minify.push(mod);
}
var minified = results.minified;
fs.writeFile(destPath, minified.code, next);
});
}, callback);
return prev;
}, { minify: [], skip: [] });
async.parallel([
function (cb) {
minifier.js.minifyBatch(filtered.minify, fork, cb);
},
function (cb) {
async.eachLimit(filtered.skip, 500, function (mod, next) {
copyFile(mod.srcPath, mod.destPath, next);
}, cb);
},
], callback);
});
}
function linkModules(callback) {