Minify all module scripts in production

v1.18.x
Peter Jaszkowiak 8 years ago
parent 1b43faba1a
commit 9b1068e868

@ -71,7 +71,7 @@ exports.buildTargets = function (targets, callback) {
winston.info('[build] Building javascript');
var startTime = Date.now();
async.series([
meta.js.linkModules,
meta.js.buildModules,
meta.js.linkStatics,
async.apply(meta.js.minify, 'nodebb.min.js'),
async.apply(meta.js.minify, 'acp.min.js')

@ -7,6 +7,7 @@ var async = require('async');
var fs = require('fs');
var mkdirp = require('mkdirp');
var rimraf = require('rimraf');
var uglifyjs = require('uglify-js');
var file = require('../file');
var plugins = require('../plugins');
@ -79,7 +80,7 @@ module.exports = function (Meta) {
'public/src/modules/string.js'
],
// modules listed below are routed through express (/src/modules) so they can be defined anonymously
// modules listed below are built (/src/modules) so they can be defined anonymously
modules: {
"Chart.js": './node_modules/chart.js/dist/Chart.min.js',
"mousetrap.js": './node_modules/mousetrap/mousetrap.min.js',
@ -90,13 +91,48 @@ module.exports = function (Meta) {
}
};
Meta.js.linkModules = function (callback) {
rimraf(path.join(__dirname, '../../build/public/src/modules'), function (err) {
function minifyModules(modules, callback) {
async.eachLimit(modules, 500, function (mod, next) {
var filePath = mod.filePath;
var destPath = mod.destPath;
var minified;
async.parallel([
function (cb) {
mkdirp(path.dirname(destPath), cb);
},
function (cb) {
fs.readFile(filePath, function (err, buffer) {
if (err) {
return callback(err);
return cb(err);
}
try {
minified = uglifyjs.minify(buffer.toString(), {
fromString: true,
compress: false,
});
} catch (e) {
return cb(e);
}
cb();
});
}
], function (err) {
if (err) {
return next(err);
}
async.eachLimit(Object.keys(Meta.js.scripts.modules), 1000, function (relPath, next) {
var filePath = path.join(__dirname, '../../', Meta.js.scripts.modules[relPath]);
fs.writeFile(destPath, minified.code, next);
});
}, callback);
};
function linkModules(callback) {
var modules = Meta.js.scripts.modules;
async.eachLimit(Object.keys(modules), 1000, function (relPath, next) {
var filePath = path.join(__dirname, '../../', modules[relPath]);
var destPath = path.join(__dirname, '../../build/public/src/modules', relPath);
mkdirp(path.dirname(destPath), function (err) {
@ -107,6 +143,72 @@ module.exports = function (Meta) {
file.link(filePath, destPath, next);
});
}, callback);
};
var moduleDirs = ['modules', 'admin', 'client'];
function getModuleList(callback) {
var modules = Object.keys(Meta.js.scripts.modules).map(function (relPath) {
return {
filePath: path.join(__dirname, '../../', Meta.js.scripts.modules[relPath]),
destPath: path.join(__dirname, '../../build/public/src/modules', relPath),
};
});
var dirs = moduleDirs.map(function (dir) {
return path.join(__dirname, '../../public/src', dir);
});
async.each(dirs, function (dir, next) {
utils.walk(dir, function (err, files) {
if (err) {
return next(err);
}
modules = modules.concat(files.map(function (filePath) {
return {
filePath: filePath,
destPath: path.join(__dirname, '../../build/public/src', path.relative(path.dirname(dir), filePath)),
};
}));
next();
});
}, function (err) {
if (err) {
return callback(err);
}
callback(null, modules);
});
}
function clearModules(callback) {
var builtPaths = moduleDirs.map(function (p) {
return '../../build/public/src/' + p;
});
async.each(builtPaths, function (builtPath, next) {
rimraf(path.join(__dirname, builtPath), next);
}, callback);
}
Meta.js.buildModules = function (callback) {
clearModules(function (err) {
if (err) {
return callback(err);
}
if (global.env === 'development') {
return linkModules(callback);
}
getModuleList(function (err, modules) {
if (err) {
return callback(err);
}
minifyModules(modules, callback);
});
});
};

Loading…
Cancel
Save