2

我想在不同的块中有一些依赖项,所以 client.js 会更小。网络包 4.16.5

为什么我将相同的代码包含在几个块中?少了什么东西?

在此处输入图像描述

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

module.exports = {
  mode: 'production',
  target: 'web',
  // devtool: 'source-map',
  entry: {
   echarts: ['echarts'],
    vendor_1: ['react', 'immutable', 'lodash', 'redux', 'redux-saga'],
    vendor_2: ['@material-ui/core', '@tinymce/tinymce-react'],
    client: ['babel-polyfill', path.join(__dirname, 'app/index.js')],
  },
  output: {
    path: path.join(__dirname, '/dist/'),
    filename: '[hash].[name].js',
    publicPath: './',
  },
  resolve: {
    modules: [
      path.join(__dirname, '/app'),
      'node_modules',
    ],
  },
  plugins: [
    new CleanWebpackPlugin(['dist']),
    new HtmlWebpackPlugin({
      template: 'app/index.tpl.html',
      inject: 'body',
      filename: 'index.html',
    }),
    new BundleAnalyzerPlugin(),
  ],
  module: {
    rules: [
      {
        test: /\.js?$/,
        exclude: /node_modules/,
        use: [{
          loader: 'babel-loader',
        }],
      },
      {
        test: /\.scss$/,
        use: [
          'style-loader',
          'css-loader?modules&localIdentName=[name]---[local]---[hash:base64:5]',
          'sass-loader',
        ],
      },
      {
        test: /\.woff(2)?(\?[a-z0-9#=&.]+)?$/,
        use: [{
          loader: 'url?limit=10000&mimetype=application/font-woff',
        }],
      },
      {
        test: /\.(ttf|eot|svg)(\?[a-z0-9#=&.]+)?$/,
        use: [{
          loader: 'file',
        }],
      },
    ],
  },
};
4

1 回答 1

3

我添加了优化配置,它现在按预期工作。

  optimization: {
    splitChunks: {
      chunks: 'initial',
    },
  },

我已经使用了一些值,这是我的代码库的最佳结果,无需任何额外的代码编辑。您可以在此处找到所有可能的选项:

https://webpack.js.org/configuration/optimization/

感谢帕维尔的提示;)

于 2018-08-28T06:16:58.563 回答