Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
126
rated 0 times [  129] [ 3]  / answers: 1 / hits: 54480  / 7 Years ago, mon, may 22, 2017, 12:00:00

I have a simple webpack.config.js



module.exports = {
entry: ./app.js,
output: {
filename: bundle.js
},
}


And I want to pass the values for entryand output through command line arguments. Is that possible and how would I do that?


More From » webpack

 Answers
9

webpack.config.js can also exports a function of env which can return a conf object. You can therefore have a webpack config (above webpack version 5) like:


module.exports = env => {
return {
entry: env.production ? "./app.js": "app-dev.js",
output: {
filename: "bundle.js"
},
}
};

env can be any name that you want. Call webpack from the command-line (or package.json) like this:


webpack --env=production

or


webpack --env=development

in the webpack.config.js, we get the env value is like this,


{ WEBPACK_BUNDLE: true, WEBPACK_BUILD: true, production: true }

[#57702] Friday, May 19, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daren

Total Points: 577
Total Questions: 114
Total Answers: 120

Location: Malaysia
Member since Fri, Dec 3, 2021
3 Years ago
;