简体   繁体   English

webpack 生产构建 bundle.js 文件大小为 10mb

[英]webpack production build bundle.js file size is 10mb

I'm working on this react app and when I build the projects the bundle.js file is 10mb so after the deployment it takes time to load the content.我正在开发这个反应应用程序,当我构建项目时, bundle.js文件为 10mb,因此在部署后需要时间来加载内容。

Here's the code: https://github.com/sef-global/scholarx-frontend这是代码: https://github.com/sef-global/scholarx-frontend

Here's my webpack config file:这是我的 webpack 配置文件:

// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const webpack = require('webpack');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: './src/index.tsx',
  mode: 'development',
  module: {
    rules: [
      {
        test: /\.(js|jsx|ts|tsx)$/,
        exclude: /(node_modules|bower_components)/,
        loader: 'babel-loader',
        options: { presets: ['@babel/env'] },
      },
      {
        test: /\.css$/,
        use: [
          { loader: 'style-loader' },
          { loader: 'css-loader', options: { modules: true } },
        ],
      },
      {
        test: /\.less$/,
        use: [
          {
            loader: 'style-loader',
          },
          {
            loader: 'css-loader',
          },
          {
            loader: 'less-loader',
            options: {
              lessOptions: { javascriptEnabled: true },
            },
          },
        ],
      },
      {
        test: /\.(png|jpe?g|gif)$/i,
        use: [{ loader: 'file-loader' }],
      },
    ],
  },
  resolve: { extensions: ['*', '.js', '.jsx', '.ts', '.tsx'] },
  output: {
    path: path.resolve(__dirname, 'dist/'),
    publicPath: '/dist/',
    filename: 'bundle.js',
  },
  devServer: {
    contentBase: path.join(__dirname, 'public/'),
    port: 3000,
    historyApiFallback: true,
    open: true,
    hotOnly: true,
  },
  plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new HtmlWebpackPlugin({
      template: 'public/index.html',
      favicon: 'public/favicon.png',
    }),
  ],
};

I assume for production build you using your "build" command from packages.json which states:我假设您使用packages.json中的“构建”命令进行生产构建,其中指出:

"build": "webpack",

That will trigger webpack "building" of course, but in your webpack config the mode is set to development - so it will build in development mode.当然,这将触发webpack “构建”,但在您的 webpack 配置中, mode设置为development - 所以它将在开发模式下构建。

What you want to do is this:你想要做的是:

"build": "webpack --mode production",

--mode argument will override what you have in webpack.config. --mode参数将覆盖您在 webpack.config 中的内容。

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

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