admin管理员组文章数量:1435231
I am setting up Redux DevTools () in my project and want to exclude the DevTools when building my project for production. The documentation says that this can be acplished by using this code:
if (process.env.NODE_ENV === 'production') {
module.exports = require('./configureStore.prod');
} else {
module.exports = require('./configureStore.dev');
}
I already have a module with constants so I have put the checking for the NODE_ENV in there.
Constants.PRODUCTION = process.env.NODE_ENV === 'production'
In my Webpack config file I have the following code that works like it should:
const production = process.env.NODE_ENV === 'production'
var config = {
// configuration goes here
}
if (production) {
config.plugins = [
new webpack.optimize.UglifyJsPlugin({
press: {
warnings: false,
},
}),
].concat(config.plugins)
}
When running set NODE_ENV=production&&webpack
the build get's minified and using just webpack
dosen't minify the build. However, in the source code itself the NODE_ENV is undefined:
console.log(process.env.NODE_ENV) // Output: Undefined
If I set my Constants.PRODUCTION
to true
then DevTools is not included in the build. Somehow I am supposed to use DefinePlugin or ProvidePlugin (the Redux DevTools documentation mention them both but on different places), but I can't figure out how. I am using Windows 10, DevTools 3.0.0 and npm scripts to run the build process. Can anyone help me with how I'm supposed to set up DefinePlugin or ProvidePlugin in my webpack config file?
I am setting up Redux DevTools (https://www.npmjs./package/redux-devtools) in my project and want to exclude the DevTools when building my project for production. The documentation says that this can be acplished by using this code:
if (process.env.NODE_ENV === 'production') {
module.exports = require('./configureStore.prod');
} else {
module.exports = require('./configureStore.dev');
}
I already have a module with constants so I have put the checking for the NODE_ENV in there.
Constants.PRODUCTION = process.env.NODE_ENV === 'production'
In my Webpack config file I have the following code that works like it should:
const production = process.env.NODE_ENV === 'production'
var config = {
// configuration goes here
}
if (production) {
config.plugins = [
new webpack.optimize.UglifyJsPlugin({
press: {
warnings: false,
},
}),
].concat(config.plugins)
}
When running set NODE_ENV=production&&webpack
the build get's minified and using just webpack
dosen't minify the build. However, in the source code itself the NODE_ENV is undefined:
console.log(process.env.NODE_ENV) // Output: Undefined
If I set my Constants.PRODUCTION
to true
then DevTools is not included in the build. Somehow I am supposed to use DefinePlugin or ProvidePlugin (the Redux DevTools documentation mention them both but on different places), but I can't figure out how. I am using Windows 10, DevTools 3.0.0 and npm scripts to run the build process. Can anyone help me with how I'm supposed to set up DefinePlugin or ProvidePlugin in my webpack config file?
2 Answers
Reset to default 3I solved it by myself; in the webpack config file I added this:
plugins: [
// Some other plugins
new webpack.DefinePlugin({
PRODUCTION: production,
})
]
I changed the code in my Constants module to
Constants.PRODUCTION = PRODUCTION
and that works. Now when running my npm scripts I got one build without the modules since that is removed pletely during uglifying and I can start webpack dev server as before and then I have Redux DevTools loaded:
"scripts": {
"start": "set NODE_ENV=development&&webpack-dev-server",
"build": "set NODE_ENV=production&&webpack --progress --colors"
}
The first code snippet in the question now looks like this:
if (Constants.PRODUCTION) {
module.exports = require('./configureStore.prod');
} else {
module.exports = require('./configureStore.dev');
}
Based on webpack documentation. Setting webpack -p
will do the following
- Minification using UglifyJSPlugin
- Runs the LoaderOptionsPlugin
- Sets the Node environment variable
So instead of manually setting NODE_ENV all you need to do is to set -p flag. Something like this
"scripts": {
"start": "webpack-dev-server",
"build": "webpack -p --progress --colors"
}
本文标签: javascriptUse NODEENV in source code to control build process with WebpackStack Overflow
版权声明:本文标题:javascript - Use NODE_ENV in source code to control build process with Webpack - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745618399a2666538.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论