Webpack多个块包含相同的依赖项

时间:2018-08-27 13:08:54

标签: webpack-4 commonschunkplugin

我想在不同的块中有一些依赖关系,所以client.js会更小。 webpack 4.16.5

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

enter image description here

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',
        }],
      },
    ],
  },
};

1 个答案:

答案 0 :(得分:1)

我添加了优化配置,它现在可以按预期运行。

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

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

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

感谢帕维尔的小费;)