DllPlugin
和 DllReferencePlugin
插件提供了一种拆分 bundle 的方式,可以显著提高构建时间性能。“DLL” 是 “Dynamic-link library”(动态链接库)的缩写,最初由微软引入。
此插件用于一个独立的 webpack 配置中,专门用于创建一个仅包含 DLL 的 bundle。它会创建一个 manifest.json
文件,该文件由 DllReferencePlugin
用于映射依赖。
context
(可选): manifest 文件中请求的上下文 (默认为 webpack 上下文)。format
(boolean = false): 如果为 true
,manifest JSON 文件 (输出) 将被格式化。name
: 暴露的 DLL 函数的名称 (TemplatePaths: [fullhash]
, [chunkhash]
, [contenthash]
, & [name]
)path
: manifest JSON 文件 (输出) 的绝对路径。entryOnly
(boolean = true): 如果为 true
,将只暴露入口点。type
: DLL bundle 的类型。new webpack.DllPlugin(options);
创建一个 manifest.json
文件,并写入给定 path
。它包含从 require 和 import 请求到模块 ID 的映射。它被 DllReferencePlugin
使用。
将此插件与 output.library
选项结合使用,以暴露(即,放入全局作用域)DLL 函数。
此插件用于主 webpack 配置中,它引用仅包含 DLL 的 bundle(s) 以按需加载预构建的依赖。
context
: (绝对路径) manifest (或 content 属性) 中请求的上下文。extensions
: 用于解析 DLL bundle 中模块的扩展名(仅在使用 'scope' 时使用)。manifest
: 一个包含 content
和 name
的对象,或者一个指向编译时要加载的 JSON manifest 文件的绝对路径字符串。content
(可选): 从请求到模块 ID 的映射 (默认为 manifest.content
)。name
(可选): DLL 暴露的标识符 (默认为 manifest.name
) (另请参阅 externals
)。scope
(可选): 用于访问 DLL 内容的前缀。sourceType
(可选): DLL 的暴露方式 (libraryTarget)。new webpack.DllReferencePlugin(options);
引用一个 DLL manifest 文件,将依赖名称映射到模块 ID,然后根据需要使用内部的 __webpack_require__
函数来引用它们。
此插件可用于两种不同的模式:作用域模式 (scoped) 和 映射模式 (mapped)。
DLL 的内容可通过模块前缀访问。例如,当 scope = 'xyz'
时,DLL 中的文件 abc
可以通过 require('xyz/abc')
访问。
DLL 的内容被映射到当前目录。如果一个被引用的文件(解析后)与 DLL 中的文件匹配,那么将使用 DLL 中的文件。
因为这发生在解析 DLL bundle 中的每个文件之后,所以对于 DLL bundle 的使用者,必须提供相同的路径。例如,如果 DLL 包含 lodash
和文件 abc
,那么 require('lodash')
和 require('./abc')
将会从 DLL 中使用,而不是将它们构建到主 bundle 中。
webpack.vendor.config.js
const path = require('path');
new webpack.DllPlugin({
context: __dirname,
name: '[name]_[fullhash]',
path: path.join(__dirname, 'manifest.json'),
});
webpack.app.config.js
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./manifest.json'),
scope: 'xyz',
sourceType: 'commonjs2',
});
两个独立的示例文件夹。演示了作用域 (scope) 和上下文 (context)。