Webpack 供应商库未拆分为单独的包

Webpack vendor libs not splitted into separate bundle

我正在尝试将我的供应商和应用程序包与 autodll-webpack-plugin (v0.4.2). this package is a top level plugin over the DllPlugin of webpack and the add-asset-html-webpack-plugin 分开,以便在 index.html 中自动订购进口商品。

这个插件应该做的是分离供应商库和应用程序代码。我可以使用 webpack 中的 CommonsChunkPlugin 来做到这一点,但这样在每次重新编译时都会重新生成捆绑包。这比一次生成供应商包并且仅在更改库时才重新编译它效率低。


问题

我把这个插件安装到 "work"(它正在输出一个 vendor.bundle.js)。这里唯一的问题是,当我用 webpack-bundle-analyzer (v2.13.1) 检查 app.bundle.js 时。我可以看到 vendor.bundle.js 中的所有 node_modules 也被加载到这个包中,因此插件无法正常工作。


版本

我正在使用:


项目结构

我的项目具有以下文档结构:

App
-- build //Here are the output bundles located
-- node_modules
-- public
  -- index.html
-- src // App code
-- webpack
  -- webpack.common.js
  -- webpack.dev.js
  -- webpack.prod.js
-- package.json 

我的webpack.common.js此代码与开发和生产版本共享

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const AutoDllPlugin = require('autodll-webpack-plugin');

module.exports = {
  entry: {
    app: [
      'babel-polyfill',
      './src/index.js',
    ],
  },
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, '../build'),
    publicPath: '', // Is referenced by the autodll-webpack-plugin
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
        options: {
          plugins: ['react-hot-loader/babel'],
          cacheDirectory: true,
          presets: ['env', 'react'],
        },
      }
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './public/index.html'
    }),
    new AutoDllPlugin({
      inject: true, // will inject the DLL bundle to index.html
      debug: false,
      filename: '[name]_[hash].js',
      context: path.join(__dirname, '..'),
      path: '',
      entry: {
        vendor: [
          'react',
          'react-dom'
        ]
      },
    }),
  ],
};

根据 autodll-webpack-plugindocs 上下文键应该用于分隔。所以我认为这就是问题所在。

文档说你应该引用webpack.config.js所在的文件夹,但我有3个,我需要引用哪一个?我的文件夹名为 webpack 而不是您在文档中看到的 config.. 在这里也正确吗?

所以最后我没有让 DLL 设置正常工作。在阅读更多内容后,我意识到 autodll-webpack-plugin advises to use the hard-source-webpack-plugin 的创建者相反,因为 webpack 将来可能会将其用作默认选项。

在阅读更多内容后,我还意识到不建议在生产中使用 DLL 插件,因为无论如何你都必须重新编译它(开发构建添加了东西)。因此,您应该将 hard-source-webpack-plugin 用于开发构建,将 SplitChunksPlugin 用于生产。

我很容易让这两个工作:

Webpack.dev.js

const merge = require('webpack-merge');
const webpack = require('webpack');
const path = require('path');
const HardSourceWebpackPlugin = require('hard-source-webpack-plugin');

const common = require('./webpack.common.js');

module.exports = merge(common, {
  mode: 'development',
  devtool: 'eval-source-map',
  devServer: {
    hot: true,
    contentBase: path.resolve(__dirname, 'build'),
    historyApiFallback: true, // Allow refreshing of the page
  },
  plugins: [
    // Enable hot reloading
    new webpack.HotModuleReplacementPlugin(),

    // Enable caching
    new HardSourceWebpackPlugin({
      cacheDirectory: '.cache/hard-source/[confighash]',
      configHash: function(webpackConfig) {
        return require('node-object-hash')({ sort: false }).hash(webpackConfig);
      },
      environmentHash: {
        root: process.cwd(),
        directories: [],
        files: ['package-lock.json'],
      },
      info: {
        mode: 'none',
        level: 'warn',
      },
    }),
  ],
});

webpack.prod.js

const merge = require('webpack-merge');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const CleanWebpackPlugin = require('clean-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

const common = require('./webpack.common.js');

module.exports = merge(common, {
  mode: 'production',
  plugins: [
    // Clean the build folders
    new CleanWebpackPlugin(['build'], {
      root: process.cwd(), // Otherwise the 'webpack' folder is used
    }),

    //Make vendor bundle size visible
    new BundleAnalyzerPlugin({
      analyzerMode: 'static',
      openAnalyzer: false,
      reportFilename: 'stats/prod-report.html',
    }),
  ],
  optimization: {
    splitChunks: {
      chunks: 'all',
      cacheGroups: {
        vendor: {
          name: 'vendor',
          chunks: 'all',
          test: /[\/]node_modules[\/]/,
        },
      },
    },
    minimizer: [
      // Optimize minimization
      new UglifyJsPlugin({
        cache: true,
        parallel: true,
        uglifyOptions: {
          ecma: 6,
          mangle: true,
          toplevel: true,
        },
      }),
    ],
  },
});

希望对大家有所帮助。