繁体   English   中英

Grunt、Webpack 和 DllPlugin

[英]Grunt, Webpack, and the DllPlugin

我无法想象如何在DllPlugin/DllReferencePlugin利用DllPlugin/DllReferencePlugin ,同时还使用 Grunt 进行构建。 对于那些不知道的人, DllPlugin创建了一个单独的包,可以与其他包共享。 它还创建一个清单文件(重要)以帮助进行链接。 然后,另一个包在构建时使用DllReferencePlugin来获取先前制作的DllPlugin Bundle 为此,它需要先前创建的清单文件。

在 Grunt 中,这需要在 grunt 运行之前创建清单文件,不是吗? 这是一个简化的代码示例:

webpack.dll.js

// My Dll Bundles, which creates
// - ./bundles/my_dll.js
// - ./bundles/my_dll-manifest.json
module.exports = {

    entry: {
        my_dll : './dll.js'
    },
    // where to send final bundle
    output: {
        path: './bundles',
        filename: "[name].js"
    },
    // CREATES THE MANIFEST
    plugins: [
        new webpack.DllPlugin({
            path: "./bundles/[name]-manifest.json",
            name: "[name]_lib"
        })
    ]
};

webpack.app.js

// My Referencing Bundle, which includes
// - ./bundles/app.js
module.exports = {

    entry: {
        my_app : './app.js'
    },
    // where to send final bundle
    output: {
        path: './bundles',
        filename: "[name].js"
    },
    // SETS UP THE REFERENCE TO THE DLL
    plugins: [
        new webpack.DllReferencePlugin({
          context: '.',
          // IMPORTANT LINE, AND WHERE EVERYTHING SEEMS TO FAIL
          manifest: require('./bundles/my_dll-manifest.json')
        })
    ]
};

如果您查看第二部分webpack.app.js ,我已经评论过一切似乎都在咕噜声中失败的地方。 为了让 DllReferencePlugin 工作,它需要来自 DllPlugin 的清单文件,但在 Grunt 工作流中,grunt 将在 grunt 自身初始化时加载这两个配置,导致manifest: require('./bundles/my_dll-manifest.json')行失败,因为之前构建webpack.dll.js grunt 步骤尚未完成,这意味着清单尚不存在。

var path = require("path");
var util = require('util')
var webpack = require("webpack");

var MyDllReferencePlugin = function(options){
    webpack.DllReferencePlugin.call(this, options);
}

MyDllReferencePlugin.prototype.apply = function(compiler) {
    if (typeof this.options.manifest == 'string') {
        this.options.manifest = require(this.options.manifest);
    }

    webpack.DllReferencePlugin.prototype.apply.call(this, compiler);
};


// My Referencing Bundle, which includes
// - ./bundles/app.js
module.exports = {

    entry: {
        my_app : './app.js'
    },
    // where to send final bundle
    output: {
        path: './bundles',
        filename: "[name].js"
    },
    // SETS UP THE REFERENCE TO THE DLL
    plugins: [
        new MyDllReferencePlugin({
          context: '.',
          // IMPORTANT LINE, AND WHERE EVERYTHING SEEMS TO FAIL
          manifest: path.resolve('./bundles/my_dll-manifest.json')
        })
    ]
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM