我刚刚开始使用webpack 3和dllplugin。我设法找到了一些博客文章。这。但是,它们中没有一个具有适当的代码示例/ GitHub样本。有人知道这个/工作示例中对示例代码的引用吗?
发布于 2018-05-15 00:55:52
这是一个很好的简单例子:
我们在vendor.js中定义了我们的函数(这是我们将要引用为DLL的库)。
vendor.js
function square(n) {
return n*n;
}
module.exports = square;然后定义WebPack配置,使用DllPlugin将其导出为DLL。
vendor.webpack.config.js
var webpack = require('webpack');
module.exports = {
entry: {
vendor: ['./vendor'],
},
output: {
filename: 'vendor.bundle.js',
path: 'build/',
library: 'vendor_lib',
},
plugins: [new webpack.DllPlugin({
name: 'vendor_lib',
path: 'build/vendor-manifest.json',
})]
};在我们的应用程序中,我们简单地使用require(./dllname)引用创建的DLL
app.js
var square = require('./vendor');
console.log(square(7));在WebPack构建配置中,我们使用DllReferencePlugin来引用创建的DLL。
app.webpack.config.js
var webpack = require('webpack');
module.exports = {
entry: {
app: ['./app'],
},
output: {
filename: 'app.bundle.js',
path: 'build/',
},
plugins: [new webpack.DllReferencePlugin({
context: '.',
manifest: require('./build/vendor-manifest.json'),
})]
};最后,我们需要编译DLL,然后使用WebPack编译应用程序。
用:编译:
webpack --config vendor.webpack.config.js
webpack --config app.webpack.config.js若要在HTML中包含该文件,请使用简单的JS包含脚本标记。
与以下index.html一起使用
<script src="build/vendor.bundle.js"></script>
<script src="build/app.bundle.js"></script>参考:https://gist.github.com/robertknight/058a194f45e77ff95fcd也可以在WebPack存储库中找到更多的DLL示例:https://github.com/webpack/webpack/tree/master/examples
https://stackoverflow.com/questions/48745775
复制相似问题