简体   繁体   English

使用webpack时如何临时打开/关闭React开发模式?

[英]How to temporarily turn on/off React development mode when using webpack?

I'm using webpack as my bundler and I'd like to test the real performance sometimes without having to actually bundle the whole application. 我将webpack用作捆绑程序,有时想测试实际性能,而不必实际捆绑整个应用程序。 Is there a way how to temporarily turn off React development mode? 有没有办法暂时关闭React开发模式? As far as I know, the production version of React is actually a different file with all the extra debug info stripped but I don't know how to enforce which version should be loaded. 据我所知,React的生产版本实际上是一个不同的文件,其中删除了所有额外的调试信息,但我不知道如何执行应加载哪个版本。

Tell Webpack to use Node's production environment. 告诉Webpack使用Node的生产环境。 One way of doing this is to use DefinePlugin in your Webpack config to set the process.env to production: 一种方法是在Webpack配置中使用DefinePluginprocess.env设置为生产:

plugins: [
  new webpack.DefinePlugin({
    'process.env': {
      NODE_ENV: JSON.stringify("production")
    }
  })
]

Also make sure to use a production-ready devtool option, eg devtool: 'cheap-module-source-map' and not devtool: 'eval' (also in your Webpack config). 还要确保使用可用于生产环境的devtool选项,例如devtool: 'cheap-module-source-map'不是 devtool: 'eval' (也在您的Webpack配置中)。

The production version of React is just the .min.js version. React的生产版本只是.min.js版本。 From the download page : 下载页面

We provide two versions of React: an uncompressed version for development and a minified version for production. 我们提供了两个版本的React:用于开发的未压缩版本和用于生产的精简版本。 The development version includes extra warnings about common mistakes, whereas the production version includes extra performance optimizations and strips all error messages. 开发版本包括有关常见错误的额外警告,而生产版本包括额外的性能优化并去除所有错误消息。

So if you include this in your asset pipeline, instead of the uncompressed version, you will be able to test your app in 'production' mode. 因此,如果将其包含在资产管道中,而不是未压缩的版本中,则可以在“生产”模式下测试您的应用。

Assuming you install React 15.0.1 with npm, import react from 'react' or react = require('react') will run ./mode_modules/react/lib/React.js which is React's raw source. 假设您使用npm安装React 15.0.1, import react from 'react'react = require('react')将运行./mode_modules/react/lib/React.js ,这是React的原始源代码。

The React docs suggest you use ./mode_modules/react/dist/react.js for development and react.min.js for production. React文档建议您将./mode_modules/react/dist/react.js用于开发,将react.min.js用于生产。

Should you minify /lib/React.js or /dist/react.js for production, React will display a warning message that you've minified non-production code: 如果您缩小用于生产的/lib/React.js/dist/react.js将显示一条警告消息,说明您已经缩小了非生产代码:

Warning: It looks like you're using a minified copy of the development build of React. When deploying React apps to production, make sure to use the production build which skips development warnings and is faster. See fb.me/react-minification for more details.

react-dom, redux, react-redux behave similarly. react-dom,redux,react-redux的行为类似。 Redux displays a warning message. Redux显示警告消息。 I believe react-dom does too. 我相信React-dom也是如此。

So you are clearly encouraged to use the production version from /dist . 因此,显然鼓励您使用/dist的生产版本。

However if you minify the /dist versions, webpack's UglifyJsPlugin will complain. 但是,如果缩小/dist版本,则webpack的UglifyJsPlugin会抱怨。

WARNING in ../~/react/dist/react.js Critical dependencies: 4:478-485 This seems to be a pre-built javascript file. Though this is possible, it's not recommended. Try to require the original source to get better results. @ ../~/react/dist/react.js 4:478-4851

You cannot avoid this message because UglifyJsPlugin can only exclude webpack chunks, not individual files. 您无法避免此消息,因为UglifyJsPlugin只能排除Webpack块,而不能排除单个文件。

I use the both the development and production /dist versions myself. 我自己使用开发和生产/dist版本。

  • Webpack has less work to do and finishes a bit sooner. Webpack的工作量较少,并且可以更快地完成。 (YRMV) (YRMV)
  • React docs say /dist/react.min.js is optimised for production. React文档说/dist/react.min.js已针对生产进行了优化。 I've read no proof, just handwaving, that 'process.env': { NODE_ENV: JSON.stringify(IS_PRODUCTION ? 'production' : 'development') } plus uglify does as good a job as '/dist/react.min.js`. 我还没有手工证明过'process.env': { NODE_ENV: JSON.stringify(IS_PRODUCTION ? 'production' : 'development') }证据'process.env': { NODE_ENV: JSON.stringify(IS_PRODUCTION ? 'production' : 'development') }加上uglify与'/dist/react.min一样好.js`。
  • I get 1 warning message from uglify rather than 3 from the react/redux ecosystem. 我从uglify收到1条警告消息,而不是从react / redux生态系统收到3条警告消息。

You can have webpack use the /dist versions with: 您可以让webpack将/dist版本用于:

resolve: {
    alias: {
      'react$': path.join(__dirname, 'node_modules', 'react','dist',
        (IS_PRODUCTION ? 'react.min.js' : 'react.js')),
      'react-dom$': path.join(__dirname, 'node_modules', 'react-dom','dist',
        (IS_PRODUCTION ? 'react-dom.min.js' : 'react-dom.js')),
      'redux$': path.join(__dirname, 'node_modules', 'redux','dist',
        (IS_PRODUCTION ? 'redux.min.js' : 'redux.js')),
      'react-redux$': path.join(__dirname, 'node_modules', 'react-redux','dist',
        (IS_PRODUCTION ? 'react-redux.min.js' : 'react-redux.js'))
    }
  }

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

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