Webpack DllPlugin 让构建速度柔顺丝滑

概念

DLLPlugin 和 DLLReferencePlugin 用某种方法实现了拆分 bundles,同时还大大提高了构建的速度,将包含大量复用模块且不会频繁更新的库进行编译,只须要编译一次,编译完成后存在指定的文件中。在以后的构建过程当中不会再对这些模块进行编译,而是直接使用 DllReferencePlugin 来引用动态连接库的代码。通常会对经常使用的第三方模块使用这种方式,例如 react、react-dom、lodash 等。只要这些模块不升级更新,这些动态连接库就不须要从新编译。javascript

摘要

本文介绍了 Webpack 中 DllPlugin 插件的使用,以及配合使用 AddAssetHtmlPlugin 或者 HtmlWebpackIncludeAssetsPlugin 将构建好的 JS 文件插入到 html页面中html

代码地址

github: 源码
欢迎交流,Star!java

项目目录

常规webpack项目,搭建过程本文章不在描述react

myreact
|- /build
  |- webpack.config.js
  |- webpack.dll.conf.js
|- /dist
  |- dll
  |- js
|- /src
  |- index.js
|- package.json
|- index.html
复制代码

具体项目结构,请看下图webpack

下面开始DLL之旅

一.build目录下建立webpack.dll.conf.js(DllPlugin)

const webpack = require("webpack")
const path = require('path')
const CleanWebpaclPlugin = require('clean-webpack-plugin');
const resolve = (dir) => path.join(__dirname, '..', dir);

module.exports = {
  entry: {
    # 将 react、lodash等模块做为入口编译成动态连接库
    vendor: ['react', 'react-dom', 'react-router-dom', 'lodash']
  },
  output: {
    # 指定生成文件所在目录文件夹,
    # 将它们存放在了 src 文件夹下
    path: resolve('public'),
    # 指定文件名
    library: '_dll_[name]',
    # 存放动态连接库的全局变量名称,例如对应 lodash 来讲就是 lodash_dll_lib
    # 这个名称须要与 DllPlugin 插件中的 name 属性值对应起来
    filename: 'dll/_dll_[name].[hash].js'
  },
  plugins: [
    new CleanWebpaclPlugin(['dll'], {
        root: resolve('public')
    }),
    new webpack.DllPlugin({
      name: '_dll_[name]',
    # 和output.library中一致,值就是输出的manifest.json中的 name值
      path: path.join(__dirname, '../public/dll', '[name].manifest.json')
    })
  ]
}
复制代码

二. 建立webpack.base.conf.js 使用 DllReferencePlugin

const path = require('path');
const webpack = require('webpack');
const HTMLWebpackPlugin = require('html-webpack-plugin');
const CleanWebpaclPlugin = require('clean-webpack-plugin');
const resolve = (dir) => path.join(__dirname, '..', dir);

module.exports = {
    entry: './src/index.js',
    output: {
        path: resolve('dist'),
        filename: 'js/[name].[hash].js',
        library: '_dll_[name]'
    },
    plugins: [
        # 需添加root 不然没法删除,exclude未生效
        new CleanWebpackPlugin(['dist'], {
          root: path.join(__dirname, '..')
        }),
        new HTMLWebpackPlugin({
            title: 'Webpak DllPlugin 的使用',
            template: resolve('index.html'),
            filename: 'index.html'
        }),
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': JSON.stringify('production')
        }),
        # 告诉 Webpack 使用动态连接库
        new webpack.DllReferencePlugin({
            // 描述 lodash 动态连接库的文件内容
            manifest: require(../public/dll/vendor.manifest') }) ] } 复制代码

3、在 index.html 文件中引入动态连接库

因为动态连接库咱们通常只编译一次,除非依赖的三方库更新,以后就不用编译,所以入口的 index.js 文件中不包含这些模块,因此要在 index.html 中单独引入。
git

两种方案github

  1. 手动添加script,手动copy打包好的dll文件夹到dist,麻烦反复,很不爽
  2. 使用add-asset-html-webpack-plugin或者html-webpack-include-assets-plugin插入到html中,简单自动化,美滋滋

因此咱们确定会采用第二种方式,下面着重讲下add-asset-html-webpack-plugin与html-webpack-include-assets-plugin插件的使用,项目中使用add-asset-html-webpack-pluginweb

安装大同小异

npm install add-asset-html-webpack-plugin -D
npm install html-webpack-include-assets-plugin -D
复制代码

使用也有类似的地方

webpack.base.conf.js 文件中进行使用npm

# add-asset-html-webpack-plugin
...;
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin');

module.exports = {
    ...,
    plugins: [
        ...,
        # 给定的 JS 或 CSS 文件添加到 webpack 配置的文件中,并将其放入资源列表 html webpack插件注入到生成的 html 中。
        new AddAssetHtmlPlugin([
            {
                # 要添加到编译中的文件的绝对路径
                filepath: path.resolve(__dirname,'../public/dll/_dll_vendor.js'),
                outputPath: 'dll',
                publicPath: 'dll',
                includeSourcemap: false
            }
        ])
    ]
}

复制代码
# html-webpack-include-assets-plugin
...;
const HtmlWebpackIncludeAssetsPlugin = require('html-webpack-include-assets-plugin');

module.exports = {
    ...,
    plugins: [
        ...,
        # 给定的 JS 或 CSS 文件添加到 webpack 配置的文件中,并将其放入资源列表 html webpack插件注入到生成的 html 中。
        new HtmlWebpackIncludeAssetsPlugin([
            {
                assets: ['dll/_dll_vendor.js'],
                append: false
            }
        ])
    ]
}

复制代码

二者区别

index.html

<!DOCTYPE html>
<html>
    
    <head>
        <meta charset="UTF-8">
        <title>爱你的一只猫哈哈哈1111</title>
    </head>
    
    <body>
        <div id='root'></div>

</html>
<script type="text/javascript" src="dll/_dll_vendor.js"></script>
<script type="text/javascript" src="/js/runtime.830efec54753fd6ed91b.js"></script>
<script type="text/javascript" src="/js/vendors.830efec54753fd6ed91b.js"></script>
<script type="text/javascript" src="/js/app.830efec54753fd6ed91b.js"></script>
复制代码

运行项目

npm run build
复制代码

查看dist文件下的文件json

相关文档

webpack 中文

相关文章
相关标签/搜索