1

我正在尝试使用autodll-webpack-plugin ( v0.4.2) 将我的供应商和应用程序包分开。这个包是webpack的 DllPlugin 和add-asset-html-webpack-plugin 的顶级插件,用于自动排序index.html.

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


问题

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

app.bundle.js


版本

我在用:

  • 网页包v4.11.0
  • 通天塔装载机v7.1.4
  • 通天塔核心v6.26.3
  • autodll-webpack-插件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此代码与开发和产品构建共享

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用于分离。所以我认为这就是问题所在。

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

4

1 回答 1

1

所以最后我没有让DLL设置工作。在阅读了更多内容后,我意识到autodll-webpack-plugin的创建者建议改用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,
        },
      }),
    ],
  },
});

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

于 2018-06-15T12:05:01.967 回答