Last active
July 22, 2020 05:13
-
-
Save ibeeger/effb669c38247ff8c04aa36becd03a57 to your computer and use it in GitHub Desktop.
webpack.config.js multi-compiler Sequential execution
This file contains 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
/** | |
webpack.config.js | |
**/ | |
const webpack = require('webpack'); | |
const env = process.env.NODE_ENV; | |
const configs = [{ | |
name:"one", | |
entry:{ | |
}, | |
output:{ | |
}, | |
module: modules, | |
resolve: _resolve, | |
plugins: _plugins | |
},{ | |
name:"two", | |
entry:{ | |
}, | |
output:{ | |
}, | |
module: modules, | |
resolve: _resolve, | |
plugins: _plugins | |
}]; | |
var arg = process.env.npm_config_module; | |
for (var i = 0; i < configs.length; i++) { | |
if (arg == configs[i]["name"]) { | |
configs = [configs[i]] | |
break; | |
} | |
}; | |
console.log("isproduction:" + env + ",target is" + (arg || "all")); | |
if (env != 'production') { | |
module.exports = configs; | |
} else { | |
var i = 0; | |
console.time("build"); | |
function buildModule() { | |
if (i >= configs.length) { | |
console.timeEnd("build"); | |
process.exit(0); | |
return; | |
}; | |
console.log("now target is:" + configs[i]['name']); | |
console.time(configs[i]['name']) | |
var multiCompiler = webpack(configs[i]); | |
multiCompiler.run((err, stats) => { | |
console.timeEnd(configs[i]['name']); | |
i++; | |
buildModule(); | |
}); | |
} | |
buildModule(); | |
}; | |
/** | |
you can edit package.json scripts | |
"dev": "NODE_ENV=local webpack -w --devtool=source-map --colors --progress ", | |
"build": "NODE_ENV=production node --max-old-space-size=4096 ./webpack.config.js" | |
exp: a target | |
npm run dev --module=one | |
or | |
npm run build --module=one | |
exp: | |
npm run dev | |
npm run build | |
**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment