簡體   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