在源代码中使用NODE_ENV来控制Webpack的构建过程 [英] Use NODE_ENV in source code to control build process with Webpack
问题描述
我正在设置Redux DevTools( https://www.npmjs.com/package/redux -devtools )在我的项目中,并希望在构建生产项目时排除DevTools。文档说这可以通过使用以下代码来实现:
I am setting up Redux DevTools (https://www.npmjs.com/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 accomplished by using this code:
if (process.env.NODE_ENV === 'production') {
module.exports = require('./configureStore.prod');
} else {
module.exports = require('./configureStore.dev');
}
我已经有一个带常量的模块所以我已经检查了NODE_ENV在那里。
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'
在我的Webpack配置文件中,我有以下代码可以正常工作:
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({
compress: {
warnings: false,
},
}),
].concat(config.plugins)
}
当运行设置NODE_ENV = production&& webpack
时,构建get被缩小并仅使用 webpack
不要缩小构建。但是,在源代码本身中,NODE_ENV未定义:
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
如果我设置 Constants.PRODUCTION
到 true
然后DevTools不包含在构建中。不知怎的,我应该使用DefinePlugin或ProvidePlugin(Redux DevTools文档提到它们但在不同的地方),但我无法弄清楚如何。我使用Windows 10,DevTools 3.0.0和npm脚本来运行构建过程。任何人都可以帮我解决我应该如何在我的webpack配置文件中设置DefinePlugin或ProvidePlugin?
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?
推荐答案
我解决了它我自己;在webpack配置文件中我添加了这个:
I 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
这是有效的。现在当我运行我的npm脚本时,我得到了一个没有模块的构建,因为在uglifying期间它被完全删除了,我可以像以前一样启动webpack dev服务器,然后我加载了Redux DevTools:
and that works. Now when running my npm scripts I got one build without the modules since that is removed completely 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');
}
这篇关于在源代码中使用NODE_ENV来控制Webpack的构建过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!