zoukankan      html  css  js  c++  java
  • webpack实践——DLLPlugin 和 DLLReferencePlugin的使用

    DLLPlugin 和 DLLReferencePlugin的使用

    DLLPlugin 和 DLLReferencePlugin 用某种方法实现了拆分 bundles,同时还大大提升了构建的速度。

    1.首先build文件夹添加----webpack.dll.config.js:

    var path = require("path");
    var webpack = require("webpack");
    
    module.exports = {
      // 要打包的模块的数组
      entry: {
        vendor: ['vue/dist/vue.esm.js','vue-router']
      },
      output: {
        path: path.join(__dirname, '../static/js'), // 打包后文件输出的位置
        filename: '[name].dll.js',// vendor.dll.js中暴露出的全局变量名。
        library: '[name]_library' // 与webpack.DllPlugin中的`name: '[name]_library',`保持一致。
      },
      plugins: [
        new webpack.DllPlugin({
          path: path.join(__dirname, '.', '[name]-manifest.json'),
          name: '[name]_library', 
          context: __dirname
        }),
      ]
    };
    

    2.在package.json的scripts里加上:

    "dll": "webpack --config build/webpack.dll.config.js",
    

    3.运行npm run dll 在static/js下生成vendor-manifest.json;
    4.在build/webpack.base.conf.js里加上:

    // 添加DllReferencePlugin插件
      plugins: [
        new webpack.DllReferencePlugin({
          context: __dirname,
          manifest: require('./vendor-manifest.json')
        })
      ],
    

    5.然后在index.html中引入vendor.dll.js:

    <div id="app"></div>
    <script src="./static/js/vendor.dll.js"></script>
    

    至此,配置之后的:
    可以看到npm run build后的时间大幅度减少,在dist打包体积上也比之前的小。在项目优化中,可以很大程度上加快项目的构建速度和减少项目的打包体积。

  • 相关阅读:
    win7,win8,win8.1修复IE浏览器的建议
    推荐给.net程序员的学习网站
    OLTP与OLAP
    profiler列名的具体意义
    sp_reset_connection
    IDENTITY
    【读书笔记】Android Handler和Looper流程
    Android视频/音频缓存框架AndroidVideoCache
    Android KeyStore格式转换工具
    使用Android Studio开发NDK
  • 原文地址:https://www.cnblogs.com/lusongshu/p/8473318.html
Copyright © 2011-2022 走看看