事实上,webpack 提供了这样的插件来实现上述的功能,就是dll-plugin和dllreferenceplugin。 这个插件会生成一个名为 manifest.json 的文件,这个文件是用来让 DLLReferencePlugin 映射到相关的依赖上去的。 /dll/vendors.dll.js') }), new webpack.DllReferencePlugin({ // 注意: DllReferencePlugin 的 context 这个时候我们就要多次调用 AddAssetHtmlPlugin 和 DllReferencePlugin,让配置文件显得冗长而且不易管理。 AutoDllPlugin 是 DllPlugin 和 DllReferencePlugin 的高级插件,隐藏了它们的大部分复杂性。
DllPlugin 和 DllReferencePlugin 用某种方法实现了拆分 bundles,同时还大幅度提升了构建的速度。 借助 DLL 思路,webpack 中引入了 DllPlugin 和 DllReferencePlugin ,允许拆分指定的第三方包、并创建单独的包,生成 manifest.json 二次构建跳过这部分编译 第一步:指定需要拆分的包,形成 DLL 库 – DllPlugin 第二步:告知webpack,命中 DLL 库文件 – DllReferencePlugin Without DllPlugin With 此插件会生成一个名为 manifest.json 的文件,这个文件是用于让 DllReferencePlugin 能够映射到相应的依赖上。生成 manifest.json(实则就是一张映射表)。 文件来把依赖的名称映射到模块的 id 上,之后再在需要的时候通过内置的 __webpack_require__ 函数来 require 对应的模块 { plugins: [ new webpack.DllReferencePlugin
dirname, 'src/utilities/'), Templates: path.resolve(__dirname, 'src/templates/') } 六、使用 DllPlugin/DllReferencePlugin 进行预先构建 Webpack 的 DllPlugin 和 DllReferencePlugin 是在新版本中推出的 Plugin,其思路就是把改变频率比较小的第三方库等依赖单独打包构建,在打包整个项目的时候 在正常的 webpack 配置文件中,使用 webpack DllReferencePlugin 解析上一步生成的 manifest.json new webpack.DllReferencePlugin
dirname, 'src/utilities/'), Templates: path.resolve(__dirname, 'src/templates/') } 六、使用 DllPlugin/DllReferencePlugin 进行预先构建 Webpack 的 DllPlugin 和 DllReferencePlugin 是在新版本中推出的 Plugin,其思路就是把改变频率比较小的第三方库等依赖单独打包构建,在打包整个项目的时候 在正常的 webpack 配置文件中,使用 webpack DllReferencePlugin 解析上一步生成的 manifest.json new webpack.DllReferencePlugin
path.resolve(__dirname,'src/utilities/'), Templates:path.resolve(__dirname,'src/templates/') } 六、使用 DllPlugin/DllReferencePlugin 进行预先构建 Webpack 的 DllPlugin 和 DllReferencePlugin 是在新版本中推出的 Plugin,其思路就是把改变频率比较小的第三方库等依赖单独打包构建,在打包整个项目的时候 在正常的 webpack 配置文件中,使用 webpack DllReferencePlugin 解析上一步生成的 manifest.json newwebpack.DllReferencePlugin
当需要导入的模块存在动态链接库中时,让其直接从链接库中获取 项目依赖的所有动态链接库都需要被加载 接入工具(webpack已内置) DllPlugin插件:用于打包出一个个单独的动态链接库文件; DllReferencePlugin path.join(__dirname, 'dll', '[name].manifest.json') }) ] } webpack.pro.config.js中使用 const DllReferencePlugin = require('webpack/lib/DllReferencePlugin'); ... plugins: [ // 告诉webpack使用了哪些动态链接库 new DllReferencePlugin /dll/react.manifest.json') }), new DllReferencePlugin({ manifest: require /dll/librarys.manifest.json') }), new DllReferencePlugin({ manifest: require
这里推荐与DLLPlugin配套的插件“DLLReferencePlugin”,它起到索引和链接作用。 在工程的webpack配置文件中(注意是webpack.config.js,不是vendor的配置文件),通过DLLReferencePlugin来获取刚才打包好的资源清单,然后在页面中添加vendor.js plugins: [ new webpack.DllReferencePlugin({ manifest: require(path.join(__dirname 小结 本篇从动态链接库思想着手,介绍了DLLPlugin与其配套插件DLLReferencePlugin使用,将第三方库与一些不常改动的模块编译打包,处理为类似于动态链接库的JS文件,以此来节约服务器资源
但是现在有了dll_react,不再需要单独去打包它们,可以直接去引用dll_react即可: 第一步:通过DllReferencePlugin插件告知要使用的DLL库; 第二步:通过AddAssetHtmlPlugin 插件,将打包的DLL库引入到Html模块中; new DllReferencePlugin({ context:path.resolve(__dirname, "../"), manifest
同时生成一个打包文件vendor.dll.js,一般是依赖库, 这个文件会暴露给全局一个类似require功能的函数,这个全局函数的名字是可以配置的, 比如,可以为 vendorRequire(); 步骤三:引入DllReferencePlugin DllReferencePlugin 是在打包过程中使用的,在打包业务代码时,每遇到一个在manifest.json中出现的文件,就可以利用上述 vendor.dll.js 暴露的全局函数进行相应处理, AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin') 在plugins中加入如下代码: // 打包优化 new webpack.DllReferencePlugin
size: 3 }); const HtmlWebpackPlugin = require('html-webpack-plugin'); const path = require('path'); //DllReferencePlugin 用于在主要的配置文件中引入DllPlugin插件打包好的动态链接库文件 const DllReferencePlugin = require('webpack/lib/DllReferencePlugin HtmlWebpackPlugin({ title: 'feikaiixn' }), //告诉webpack使用了哪些动态链接库 new DllReferencePlugin
library:'_dll_[name]',//打包后用指定变量接收 }, plugins:[ //生成manifest.json //这个文件是用来让 DLLReferencePlugin plugins:[ new webpack.DllReferencePlugin({ manifest:path.resolve(__dirname,'dist'
使用 DllReferencePlugin 插件链接这个manifest清单引用 new webpack.DllReferencePlugin({ manifest: require configs.vendorDllInsertFiles }); } commonConfig.plugins.push( new webpack.DllReferencePlugin
webpack中,我们可以结合DllPlugin 和 DllReferencePlugin插件来实现。 DllPlugin是什么? 该manifest.json的作用是用来让 DllReferencePlugin 映射到相关的依赖上去的。 DllReferencePlugin又是什么? 插件的时候,会使用该DllReferencePlugin插件读取vendor-manifest.json文件,看看是否有该第三方库。 上面说了这么多,主要是为了方便大家对于预编译资源模块和DllPlugin 和、DllReferencePlugin插件作用的理解(我第一次使用看了好久才明白~~) 先来看下完成的项目目录结构: ? /dll', file) })) } if(/.*\.manifest.json/.test(file)) { plugins.push(new webpack.DllReferencePlugin
/dll.config.js", 复制代码 3)使用 DllReferencePlugin 将打包生成的dll文件,引用到需要的预编译的依赖上来,并通过 html-webpack-tags-plugin 在打包时自动插入dll文件 vue.config.js 配置如下 import { DllReferencePlugin } from "webpack"; import HtmlTagsPlugin from "html-webpack-tags-plugin"; export default { configureWebpack: { plugins: [ new DllReferencePlugin
第三方动态链接库(专门去做第三方包抽离) 弄成cdn链接 dllplugins const webpack = require("webpack"); plugins: [ new webpack.DllReferencePlugin cache: true, //共享进程池 threadPool: happyThreadPool, //允许 HappyPack 输出日志 verbose: true, }), new webpack.DllReferencePlugin
webpack.dll.config.js,目的是为了创建一个把所有的第三方库依赖打包到一起的bundle的dll文件里面,同时还会生成一个manifest.json的文件,用于:让使用该第三方依赖集合的应用配置的DllReferencePlugin 能映射到相关的依赖上去 具体配置看下图 4.2 DllReferencePlugin DllReferencePlugin:插件核心是把上一节提到的通过webpack.dll.config.js中打包生成的 dll文件,引用到需要实际项目中使用,引用机制就是通过DllReferencePlugin插件来读取vendor-manifest.json文件,看看是否有该第三方库,最后通过add-asset-html-webpack-plugin
production webpack --config webpack.dll.js --progress" } ... } dll 生成后,就要在构建的配置文件里将其引入,这时候就用到 DllReferencePlugin new webpack.DllReferencePlugin({ manifest: require('. dirname, `dll/${vendorFile}`), includeSourcemap: false }), ... ], }; DllReferencePlugin
在 webpack 中实现 DLL 主要依赖两个插件 webpack.DllReferencePlugin 和 webpack.DllPlugin,先记好这两个即可。 plugins: [ // 当使用react的使用,先去这个表里面找 new webpack.DllReferencePlugin({ manifest: path.resolve
在 Webpack 中,可以通过使用 DllPlugin 和 DllReferencePlugin 插件来创建和使用动态链接库。 plugins: [ new webpack.DllReferencePlugin({ context: __dirname, manifest: require('.
path.join(distPath, '[name].manifest.json'), }), ], } 第二步在webpack中配置映射关系,防止打包时再次引用npm包 // 第一,引入 DllReferencePlugin const DllReferencePlugin = require('webpack/lib/DllReferencePlugin'); plugins: [ //告诉 Webpack 使用了哪些动态链接库 new DllReferencePlugin({ // 描述 react 动态链接库的文件内容 manifest: require(path.join (distPath, 'react.manifest.json')), }), ] 第三步在我们的html中引用打包的公用模块,因为当我们在配置DllReferencePlugin