Skip to content

Instantly share code, notes, and snippets.

@schowdhuri
Last active September 17, 2017 13:58
Show Gist options
  • Save schowdhuri/5025128d828759397e399c1d656d7996 to your computer and use it in GitHub Desktop.
Save schowdhuri/5025128d828759397e399c1d656d7996 to your computer and use it in GitHub Desktop.
Parallel Webpack Build
{
"presets": [ "env", "stage-1" ]
}
import chalk from "chalk";
import path from "path";
import getopt from "node-getopt";
import { exec } from "child_process";
import webpack from "webpack";
import config from "./webpack.config.prod";
const args = getopt.create([
[ "", "procid=ARG", "Proc ID for parallel builds" ],
[ "", "help", "display this help" ]
])
.bindHelp()
.parseSystem();
let procid = args.options.procid;
const MAX_PROC = 4;
const entryPoints = config.entry;
const bundleNames = Object.keys(entryPoints);
const QUEUE_SIZE = Math.ceil(bundleNames.length / MAX_PROC);
if(!procid || isNaN(parseInt(procid))) {
console.log(chalk.cyan("Starting parallel webpack build ..."));
let children = 0;
for(let i=0; i<bundleNames.length; i+=QUEUE_SIZE) {
const cmd = `node_modules/.bin/babel-node build.js --procid=${i}`;
const child = exec(cmd, (error, stdout, stderr) => {
if (error) {
console.error(`${error}`);
return;
}
console.log(`${stdout}`);
console.log(`${stderr}`);
});
++children;
child.on("close", () => {
--children;
if(children==0) {
console.log(chalk.cyan("All builds completed"));
console.log(chalk.green("Static file bundles generated"));
}
});
}
} else {
procid = parseInt(procid);
config.entry = bundleNames.slice(procid, procid + QUEUE_SIZE).reduce((acc, cur) => {
return {
...acc,
[cur]: config.entry[cur]
};
}, {})
webpack(config).run((error, stats) => {
if(error) {
console.error(chalk.red(error));
return 1;
}
const jsonStats = stats.toJson();
const stringStats = stats.toString(config.stats);
if (jsonStats.hasErrors) {
return jsonStats.errors.map(error => console.log(chalk.red(error)));
}
if (jsonStats.hasWarnings) {
console.log(chalk.yellow("Webpack generated the following warnings: "));
jsonStats.warnings.map(warning => console.log(chalkWachalk.yellowrning(warning)));
}
console.log(stringStats);
});
}
{
"scripts": {
"build": "babel-node build.js",
},
"author": "Subir",
"license": "MIT",
"devDependencies": {
"babel-cli": "^6.7.7",
"babel-core": "6.26.0",
"babel-preset-env": "1.6.0",
"babel-preset-stage-1": "6.24.1",
"chalk": "^1.1.3",
"node-getopt": "^0.2.3",
"webpack": "3.6.0"
}
}
import webpack from "webpack";
import glob from "glob";
const INIT_DIR = path.join(__dirname, "src", "init");
const OUT_DIR = path.join(__dirname, "dist");
const entryPoints = glob.sync(path.join(INIT_DIR, "*.js"))
.reduce((obj, filePath) => {
const pieces = path.parse(filePath);
return {
...obj,
[pieces.name]: path.join(INIT_DIR, pieces.name)
};
}, {});
export default {
entry: entryPoints,
output: {
path : OUT_DIR,
filename: "[name].min.js"
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment