简体   繁体   English

webpack-bundle-analyzer显示webpack -p不会删除开发依赖项react-dom.development.js

[英]webpack-bundle-analyzer shows webpack -p does not remove development dependency react-dom.development.js

This is my webpack setup 这是我的webpack设置

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;


const SOURCE_DIR = './src';
const HtmlWebpackPluginConfig = new HtmlWebpackPlugin({
  template: SOURCE_DIR + '/index.html',
  filename: 'index.html',
  inject: 'body'
});

module.exports = env => {
  console.log(`Environment Configs: ${JSON.stringify(env) || 'Default'}`);
  console.log(`
  Available Configs:
  --env.watch = true / false //for allow webpack to watch build
  `)
  let environment = env || {};
  const {
    watch,
    analyze,
  } = environment;

  const configedAnalyzer = new BundleAnalyzerPlugin({
    // Can be `server`, `static` or `disabled`.
    // In `server` mode analyzer will start HTTP server to show bundle report.
    // In `static` mode single HTML file with bundle report will be generated.
    // In `disabled` mode you can use this plugin to just generate Webpack Stats JSON file by setting `generateStatsFile` to `true`.
    analyzerMode: 'static',//was server
    // Host that will be used in `server` mode to start HTTP server.
    analyzerHost: '127.0.0.1',
    // Port that will be used in `server` mode to start HTTP server.
    analyzerPort: 9124,
    // Path to bundle report file that will be generated in `static` mode.
    // Relative to bundles output directory.
    reportFilename: './../report/bundle_anlaysis.html',
    // Module sizes to show in report by default.
    // Should be one of `stat`, `parsed` or `gzip`.
    // See "Definitions" section for more information.
    defaultSizes: 'stat',
    // Automatically open report in default browser
    openAnalyzer: Boolean(analyze),
    // If `true`, Webpack Stats JSON file will be generated in bundles output directory
    generateStatsFile: Boolean(analyze),
    // Name of Webpack Stats JSON file that will be generated if `generateStatsFile` is `true`.
    // Relative to bundles output directory.
    statsFilename: 'stats.json',
    // Options for `stats.toJson()` method.
    // For example you can exclude sources of your modules from stats file with `source: false` option.
    // See more options here: https://github.com/webpack/webpack/blob/webpack-1/lib/Stats.js#L21
    statsOptions: null,
    // Log level. Can be 'info', 'warn', 'error' or 'silent'.
    logLevel: 'info'
  });

  return {
    entry: SOURCE_DIR + '/index.js',
    output: {
      path: path.resolve(__dirname, "dist"),
      filename: 'bundle.js',
    },
    module: {
      rules: [
        {
          test: /\.js$/,
          exclude: /(node_modules)/,
          use: {
            loader: 'babel-loader',
            options: {
              presets: ['env', 'react']
            }
          }
        }
      ]
    },
    watchOptions: {
      aggregateTimeout: 300,
      poll: 1000
    },
    watch: Boolean(watch),
    plugins: [HtmlWebpackPluginConfig, configedAnalyzer], //
    devServer: {
      contentBase: path.join(__dirname, "dist"),
      compress: false,
      port: 9123,
    }
  };
}

When I do webpack -p file size is a lot smaller but this react-dom.development.js take over almost 50% of the size, in my case 500ish KB out of 1.1ish MB. 当我做webpack -p文件大小要小得多,但是这个react-dom.development.js占用了近50%的大小,在我的情况下是500ish KB的1.1ish MB。

Report here: 报告在这里:

在此输入图像描述

To see a demo of the report and how it got run you can check this repository. 要查看报告的演示及其运行方式,您可以查看存储库。

NOTE: even I add NODE_ENV=production , size is smaller but the development JavaScript file is still there! 注意:即使我添加NODE_ENV=production ,尺寸更小但开发JavaScript文件仍然存在!

Your application's process.env.NODE_ENV variable needs to be set to production within webpack's build script. 您的应用程序的process.env.NODE_ENV变量需要在webpack的构建脚本中设置为production React's Optimizing Performance documentation instructs webpack users to do this using webpack's DefinePlugin . React的Optimizing Performance文档指示webpack用户使用webpack的DefinePlugin执行此操作。

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

While it seems that the -p option should set process.env.NODE_ENV to production , there is a caveat explained in Webpack's Specifying the Environment documentation that this is not set withtin webpack's build script (for what its worth, many developers have reported this as an issue ). 虽然似乎-p选项应该process.env.NODE_ENV设置为production ,但是在Webpack的Specifying the Environment文档中有一个警告说明这不是使用 webpack的构建脚本设置的(为了它的价值,许多开发人员已将此报告为一个问题 )。

暂无
暂无

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

相关问题 angular 8 webpack-bundle-analyzer 寻找错误的 polyfill 文件 - angular 8 webpack-bundle-analyzer looking for wrong polyfill files function createFiberFromTypeAndProps in./node_modules/react-dom/cjs/react-dom.development.js:25058 中的错误 - Error in function createFiberFromTypeAndProps in ./node_modules/react-dom/cjs/react-dom.development.js:25058 react-dom.development.js:55未捕获的不变违规:对象作为React子对象无效(找到:带有键的对象 - react-dom.development.js:55 Uncaught Invariant Violation: Objects are not valid as a React child (found: object with keys 我的 index.js 文件有什么问题导致我出现这个错误 react-dom.development.js? - What's wrong in my index.js file that raise me this error react-dom.development.js? 解决错误消息“react-dom.development.js:4091 Uncaught TypeError: onItemSelect is not a function” - resolving error message "react-dom.development.js:4091 Uncaught TypeError: onItemSelect is not a function" 添加唯一的数据属性以使用 webpack 来响应 dom 元素进行开发 - Add a unique data attribute to react dom elements using webpack for development 如何在开发模式下减少 webpack bundle? - How to reduce webpack bundle in development mode? NPM的反应不能在客户端上使用,因为未定义“开发”。该包是从Webpack生成的 - React from NPM cannot be used on the client because 'development' is not defined. The bundle was generated from Webpack Nextjs:react-dom.development.js?ac89:14906 未捕获的错误:无效的钩子调用。 钩子只能在 function 组件的主体内部调用 - Nextjs: react-dom.development.js?ac89:14906 Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component 错误 #98123 WEBPACK 生成开发 JavaScript 包在 `gatsby develop` 上失败 - ERROR #98123 WEBPACK Generating development JavaScript bundle failed on `gatsby develop`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM