XeNote/posts/Code/JS/Webpack Env Variables.md

33 lines
629 B
Markdown
Raw Normal View History

2020-11-28 16:36:28 +00:00
```js
// webpack.config.js
const webpack = require('webpack');
const prod = process.argv.indexOf('-p') !== -1;
module.exports = {
...
plugins: [
new webpack.DefinePlugin({
process: {
env: {
NODE_ENV: prod? `"production"`: '"development"'
}
}
}),
...
]
};
```
<br/>
In case of the above script is not sufficient, I am sharing the package.json flags.
```json
/* Optional: package.json commands (in case of need)*/
"scripts": {
"start": "webpack-dev-server --env.dev --open --hot --mode development ",
"build": "webpack --mode production --env.dev",
},
```