Last active
September 17, 2017 13:58
-
-
Save schowdhuri/5025128d828759397e399c1d656d7996 to your computer and use it in GitHub Desktop.
Parallel Webpack Build
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
{ | |
"presets": [ "env", "stage-1" ] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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