Webpack供应商库不会拆分为单独的捆绑包

时间:2018-06-11 12:32:15

标签: webpack code-splitting webpack-plugin dllplugin

我正在尝试将我的供应商和应用包与autodll-webpack-pluginv0.4.2)分开。此软件包是webpack的DllPlugin上的顶级插件,add-asset-html-webpack-plugin用于自动排序index.html中的导入。

这个插件应该做的是分离供应商库和应用程序代码。我可以使用webpack中的CommonsChunkPlugin执行此操作,但这样就可以在每次重新编译时重新生成包。这比生成供应商包一次效率低,只在更改库时重新编译它。

问题

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

app.bundle.js

版本

我正在使用:

  • webpack v4.11.0
  • babel-loader v7.1.4
  • babel-core v6.26.3
  • autodll-webpack-plugin v0.4.2

项目结构

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

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 此代码与Dev& prod版本共享)

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-plugin的{​​{3}},应使用上下文密钥进行分隔。所以我认为这就是问题所在。

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

1 个答案:

答案 0 :(得分:1)

所以最后我没有让DLL设置工作。在阅读了一些之后,我意识到autodll-webpack-plugin的创建者建议改为使用hard-source-webpack-plugin,因为webpack可能会在将来用作默认选项。

在阅读了一些之后我也意识到不建议在生产中使用DLL插件,因为你必须重新编译它(dev build添加东西)。因此,您应该将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,
        },
      }),
    ],
  },
});

我希望这对任何人都有帮助。