简体   繁体   English

在源代码中使用NODE_ENV来控制Webpack的构建过程

[英]Use NODE_ENV in source code to control build process with Webpack

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. 我正在我的项目中设置Redux DevTools( https://www.npmjs.com/package/redux-devtools ),并希望在构建生产项目时排除DevTools。 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');
}

I already have a module with constants so I have put the checking for the NODE_ENV in there. 我已经有一个带常量的模块,所以我已经在那里检查了NODE_ENV。

Constants.PRODUCTION = process.env.NODE_ENV === 'production'

In my Webpack config file I have the following code that works like it should: 在我的Webpack配置文件中,我有以下代码:

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)
}

When running set NODE_ENV=production&&webpack the build get's minified and using just webpack dosen't minify the build. 当运行set NODE_ENV=production&&webpack ,构建得到了缩小,只使用webpack缩小构建。 However, in the source code itself the NODE_ENV is undefined: 但是,在源代码本身中,NODE_ENV未定义:

console.log(process.env.NODE_ENV) // Output: Undefined

If I set my Constants.PRODUCTION to true then DevTools is not included in the build. 如果我将Constants.PRODUCTION设置为true那么DevTools不会包含在构建中。 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. 不知怎的,我应该使用DefinePlugin或ProvidePlugin(Redux DevTools文档提到它们但在不同的地方),但我无法弄清楚如何。 I am using Windows 10, DevTools 3.0.0 and npm scripts to run the build process. 我使用Windows 10,DevTools 3.0.0和npm脚本来运行构建过程。 Can anyone help me with how I'm supposed to set up DefinePlugin or ProvidePlugin in my webpack config file? 任何人都可以帮我解决我应该如何在我的webpack配置文件中设置DefinePlugin或ProvidePlugin?

I solved it by myself; 我自己解决了; in the webpack config file I added this: 在webpack配置文件中我添加了这个:

plugins: [
  // Some other plugins
  new webpack.DefinePlugin({
      PRODUCTION: production,
  })
]

I changed the code in my Constants module to 我将Constants模块中的代码更改为

Constants.PRODUCTION = PRODUCTION

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: 现在,在运行我的npm脚本时,我得到了一个没有模块的构建,因为在uglifying期间完全删除了它,我可以像以前一样启动webpack dev服务器,然后我加载了Redux DevTools:

"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 . 基于webpack文档 Setting webpack -p will do the following 设置webpack -p将执行以下操作

  • Minification using UglifyJSPlugin 使用UglifyJSPlugin进行缩小
  • Runs the LoaderOptionsPlugin 运行LoaderOptionsPlugin
  • Sets the Node environment variable 设置Node环境变量

So instead of manually setting NODE_ENV all you need to do is to set -p flag. 因此,您需要做的就是设置-p标志,而不是手动设置NODE_ENV。 Something like this 像这样的东西

"scripts": {
  "start": "webpack-dev-server",
  "build": "webpack -p --progress --colors"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM