提高webpack構(gòu)建速度之DllPlugin與DllReferencePlugin的使用

模塊化打包工具webpack以其“黑魔法”構(gòu)建的方法深受前端er喜愛(ài),但面對(duì)慢如龜毛的編譯速度,怎么能忍。本文旨在通過(guò)介紹DllPlugin與DllReferencePlugin的使用方法,加速webpack的編譯過(guò)程。同時(shí),生成的依賴庫(kù)也能解決緩存問(wèn)題,為頁(yè)面又加快幾秒(逃

  1. DllPlugin
    先來(lái)看官方介紹

This plugin is used in a separate webpack config exclusively to create a dll-only-bundle. It creates a manifest.json
file, which is used by the DllReferencePlugin
to map dependencies.

總結(jié)就是為了分散 bundle 包,加快編譯過(guò)程而生的。通過(guò)創(chuàng)建一個(gè)名為mainfest.json的依賴文件,指明依賴項(xiàng)目,為后面構(gòu)建的bundle包作導(dǎo)引。為此,我們創(chuàng)建一個(gè)名為webpack.config.dll.js的配置文件:

const fs = require('fs')
const path = require('path')
const webpack = require('webpack')
const CleanWebpackPlugin = require('clean-webpack-plugin')

process.noDeprecation = true
  // 入口
var entry = {
  // 把有需要打包的常用庫(kù)封裝,如babel-polyfill,jquery等
  vendor: ['babel-polyfill', 'vue/dist/vue.runtime.min', 'jquery/dist/jquery.slim.min']
}

var config = {
  entry: entry,
  output: {
    path: path.resolve(__dirname, '../dist/static/js'),
    filename: '[name].js',
    library: '[name]_library'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
        query: {
          presets: ['es2015']
        }
      }
    ]
  },
  plugins: [
    new CleanWebpackPlugin(['dist'], {
      root: path.resolve(__dirname, '..')
    }),
    // 劃重點(diǎn)??!
    new webpack.DllPlugin({
    // 指定路徑
      path: path.join(__dirname, '../dist', '[name]-manifest.json'),
    // 指定依賴庫(kù)的名稱
      name: '[name]_library'
    }),
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery'
    }),
    new webpack.optimize.UglifyJsPlugin({
      sourceMap: true,
      output: false,
      compress: {
        unused: true,
        dead_code: true,
        pure_getters: true,
        warnings: false,
        screw_ie8: true,
        conditionals: true,
        comparisons: true,
        sequences: true,
        evaluate: true,
        join_vars: true,
        if_return: true
      },
      comments: false,
      minimize: true
    })
  ]
}

module.exports = config

執(zhí)行webpack --config webpack.config.dll.js得到打包后的文件

image.png

之后,在webpack生產(chǎn)配置文件中添加


const fs = require('fs')
const path = require('path')
const webpack = require('webpack')
// webpack plugins
const HtmlWebpackPlugin = require('html-webpack-plugin')
const CleanWebpackPlugin = require('clean-webpack-plugin')
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin')

var config = {
  ..
  entry 
  ...
  output
  ...
  rules
  ...
  plugins: [
    // 劃重點(diǎn)
    new webpack.DllReferencePlugin({
      context: path.resolve(__dirname, '..'),
      manifest: require('../dist/vendor-manifest.json')
    }),
    ...
    htmlwebpacks
   ...
  // 在htmlwebpack后插入一個(gè)AddAssetHtmlPlugin插件,用于將vendor插入打包后的頁(yè)面
    new AddAssetHtmlPlugin({ filepath: require.resolve('../dist/static/js/vendor.js'), includeSourcemap: false })
  ]
}

module.exports = config

至此,搞定??!


END

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 最近在學(xué)習(xí) Webpack,網(wǎng)上大多數(shù)入門(mén)教程都是基于 Webpack 1.x 版本的,我學(xué)習(xí) Webpack 的...
    My_Oh_My閱讀 8,345評(píng)論 40 247
  • 無(wú)意中看到zhangwnag大佬分享的webpack教程感覺(jué)受益匪淺,特此分享以備自己日后查看,也希望更多的人看到...
    小小字符閱讀 8,384評(píng)論 7 35
  • webpack 介紹 webpack 是什么 為什么引入新的打包工具 webpack 核心思想 webpack 安...
    yxsGert閱讀 6,682評(píng)論 2 71
  • 藥品臨床試驗(yàn)是科學(xué)方法論在藥品評(píng)價(jià)方面的實(shí)際應(yīng)用,能夠較科學(xué)、客觀、嚴(yán)謹(jǐn)?shù)墨@得藥品療效數(shù)據(jù),從而行之有效的區(qū)分某個(gè)...
    楊靜相伴要你好看閱讀 249評(píng)論 0 0
  • 等紅燈的時(shí)候,車載電臺(tái)播放起周慧的《約定》。歌詞寫(xiě)得很好。 想起08年在深圳,也是這樣的昏暗下午,從銀行出來(lái),街對(duì)...
    茜草蟈蟈閱讀 279評(píng)論 0 0

友情鏈接更多精彩內(nèi)容