如何使用webpack DLL插件?

bie*_*ier 6 webpack webpack-dev-server webpack-2 webpack-3

我刚刚开始使用 webpack 3 和 dllplugin。我设法找到了一些博客文章。这。然而,它们都没有正确的代码示例/ GitHub 示例代码。有谁知道此/工作示例的示例代码的任何参考?

moh*_*eri 5

这是一个很好的简单例子:

我们在vendor.js 中定义我们的函数(这是我们将引用为DLL 的库)。

供应商.js

function square(n) {
  return n*n;
}

module.exports = square;
Run Code Online (Sandbox Code Playgroud)

然后定义 WebPack 配置以使用 DllPlugin 将其导出为 DLL。

供应商.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',
  })]
};
Run Code Online (Sandbox Code Playgroud)

在我们的应用程序中,我们只需使用 require(./dllname) 引用创建的 DLL

应用程序.js

var square = require('./vendor');
console.log(square(7));
Run Code Online (Sandbox Code Playgroud)

在WebPack构建配置中,我们使用DllReferencePlugin来引用创建的DLL。

应用程序.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'),
  })]
};
Run Code Online (Sandbox Code Playgroud)

最后,我们需要编译DLL,然后使用WebPack应用程序。

编译:

webpack --config vendor.webpack.config.js
webpack --config app.webpack.config.js
Run Code Online (Sandbox Code Playgroud)

要将文件包含在 HTML 中,请使用简单的 JS include script 标记。

与以下index.html一起使用

<script src="build/vendor.bundle.js"></script>
<script src="build/app.bundle.js"></script>
Run Code Online (Sandbox Code Playgroud)

参考: https: //gist.github.com/robertknight/058a194f45e77ff95fcd 您还可以在 WebPack 存储库中找到更多 DLL 示例: https://github.com/webpack/webpack/tree/master/examples