Skip to content

Commit

Permalink
Chore: accelerate prod build with worker-farm (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
wenboyu2 authored Jan 17, 2018
1 parent a7f846c commit fe3daf0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
20 changes: 20 additions & 0 deletions build/build_locale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-disable */
const execSync = require('child_process').execSync;

/**
* Build a single locale
*
* @param {string} locale - locale to build
* @param {*} callback - callback from worker-farm master process
*/
module.exports = (locale, callback) => {
try {
console.log(`Building ${locale}...`);
// build assets for a single locale
execSync(`time LANGUAGE=${locale} yarn run build-prod`);
callback();
} catch (error) {
console.error(`Error: Failed to build ${locale}`);
callback(true);
}
};
33 changes: 29 additions & 4 deletions build/prod.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
const execSync = require('child_process').execSync;
/* eslint-disable */
const workerFarm = require('worker-farm');
const locales = require('./locales');
const numCPUs = require('os').cpus().length;
const execSync = require('child_process').execSync;
const path = require('path');

const filename = path.basename(__filename);
const localesCount = locales.length;
let counter = 0;

const workers = workerFarm(
{
maxConcurrentWorkers: numCPUs - 1,
maxRetries: 0
},
require.resolve('./build_locale.js')
);

locales.forEach(locale => {
workers(locale, error => {
if (++counter === localesCount || error) {
// terminate after all locales have been processed
workerFarm.end(workers);
}

locales.forEach((language) => {
console.log(`Building ${language}...`);
execSync(`LANGUAGE=${language} yarn run build-prod`);
if (error) {
// kill the node process that spawns the workers as well as all processes been spawned
execSync(`ps ax | grep "${filename}" | cut -b1-06 | xargs -t kill`);
}
});
});
4 changes: 1 addition & 3 deletions build/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function getConfig(isReactExternalized) {
rules: [
{
test: /\.js$/,
loader: 'babel-loader?cacheDirectory',
loader: 'babel-loader',
exclude: /(node_modules)/
},
{
Expand Down Expand Up @@ -120,8 +120,6 @@ function getConfig(isReactExternalized) {
if (isRelease) {
config.plugins.push(
new UglifyJsPlugin({
cache: true,
parallel: true,
uglifyOptions: {
ecma: 5,
compress: {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@
"uglifyjs-webpack-plugin": "^1.1.6",
"webpack": "^3.10.0",
"webpack-bundle-analyzer": "^2.9.2",
"webpack-dev-server": "^2.10.1"
"webpack-dev-server": "^2.10.1",
"worker-farm": "^1.5.2"
},
"peerDependencies": {
"box-react-ui": "^21.4.0",
Expand Down

0 comments on commit fe3daf0

Please sign in to comment.