Webpack: -p vs NODE_ENV=生产vs过程。env.NODE_ENV。

时间:2021-06-26 23:10:33

I'm having an issue with webpack in production and I'm getting confused with the different ways of running webpack in the production environment (I'm using Windows).

我在生产中遇到了webpack的问题,我对在生产环境中运行webpack的不同方式感到困惑(我在使用Windows)。

Can someone explain the difference between these:

有人能解释一下两者的区别吗?

One: CLI

一:CLI

  • webpack -p
  • webpack - p

Two: CLI

二:CLI

  • SET NODE_ENV=production
  • 设置NODE_ENV =生产
  • webpack
  • webpack

Three: webpack.config.js

三:webpack.config.js

new webpack.ProvidePlugin({
  'process.env': {
    NODE_ENV: JSON.stringify('production')
  }
})

I'm using React and need to build a production version which is both minified and includes the production version of React.

我正在使用反应,并需要构建一个生产版本,它既被简化,又包含了反应的生产版本。

2 个解决方案

#1


3  

SET NODE_ENV=production sets NODE_ENV environment variable to "production" on the server. The machine that compiles and bundles your javascript source files. So it can for example select a different webpack config file for production.

设置NODE_ENV=生产将NODE_ENV环境变量设置为服务器上的“生产”。编译并打包javascript源文件的机器。因此,它可以为生产选择一个不同的webpack配置文件。

But this enviroment variable has no effect when this javascript code actually runs. Because the javascript code will run on a browser in a different machine.

但是,当javascript代码实际运行时,这个环境变量没有影响。因为javascript代码将在另一台机器上的浏览器上运行。

The providePlugin helps to set NODE_ENV variable in the browser. Where your javascript actually runs. Actually what it really does is replacing occurences of process.env.NODE_ENV with "production" rather than setting a variable. But effects are same.

providePlugin帮助在浏览器中设置NODE_ENV变量。javascript实际运行的地方。实际上它所做的是替换过程的发生。NODE_ENV与“生产”,而不是设置一个变量。但是效果是一样的。

EDIT: It's actually DefinePlugin that should be used for this purpose.

编辑:它实际上是定义插件,应该用于这个目的。

Weback -p advertises to doing the second of above but seems to have some issues.

Weback -p广告说要做上述第二件事,但似乎有些问题。

#2


0  

Based on webpack documentation setting webpack -p performs following

基于webpack文档设置webpack -p执行以下操作。

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

So instead of doing:

所以不要做的事情:

"scripts": {
  "dist": "NODE_ENV=production webpack",
  "start": "webpack"
}

You can just specify

你可以指定

"scripts": {
  "dist": "webpack -p",
  "start": "webpack"
}

#1


3  

SET NODE_ENV=production sets NODE_ENV environment variable to "production" on the server. The machine that compiles and bundles your javascript source files. So it can for example select a different webpack config file for production.

设置NODE_ENV=生产将NODE_ENV环境变量设置为服务器上的“生产”。编译并打包javascript源文件的机器。因此,它可以为生产选择一个不同的webpack配置文件。

But this enviroment variable has no effect when this javascript code actually runs. Because the javascript code will run on a browser in a different machine.

但是,当javascript代码实际运行时,这个环境变量没有影响。因为javascript代码将在另一台机器上的浏览器上运行。

The providePlugin helps to set NODE_ENV variable in the browser. Where your javascript actually runs. Actually what it really does is replacing occurences of process.env.NODE_ENV with "production" rather than setting a variable. But effects are same.

providePlugin帮助在浏览器中设置NODE_ENV变量。javascript实际运行的地方。实际上它所做的是替换过程的发生。NODE_ENV与“生产”,而不是设置一个变量。但是效果是一样的。

EDIT: It's actually DefinePlugin that should be used for this purpose.

编辑:它实际上是定义插件,应该用于这个目的。

Weback -p advertises to doing the second of above but seems to have some issues.

Weback -p广告说要做上述第二件事,但似乎有些问题。

#2


0  

Based on webpack documentation setting webpack -p performs following

基于webpack文档设置webpack -p执行以下操作。

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

So instead of doing:

所以不要做的事情:

"scripts": {
  "dist": "NODE_ENV=production webpack",
  "start": "webpack"
}

You can just specify

你可以指定

"scripts": {
  "dist": "webpack -p",
  "start": "webpack"
}