【问题标题】:How to use webpack DLL Plugin?如何使用 webpack DLL 插件?
【发布时间】:2018-07-22 13:57:40
【问题描述】:

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

【问题讨论】:

标签: webpack webpack-dev-server webpack-2 webpack-3


【解决方案1】:

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

我们在 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

【讨论】:

  • 在你的 app.js 中,为什么你 require('./vendor')?你不应该require('./vendor.bundle.js')吗?
  • @PunCha 您需要在源代码 (.js) 中引用源文件。 bundle 文件是输出,将由工具生成
  • vendor_libvendor.webpack.config.js中的含义是什么?它们没有出现在app.webpack.config.jsapp.jsindex.html
  • @tcpiper vendor_lib 这里是库的名称。 app.js 使用 require 来包含 vendor.js
猜你喜欢
  • 2017-06-12
  • 2019-02-16
  • 1970-01-01
  • 2023-03-20
  • 2020-09-10
  • 2020-11-19
  • 2020-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多