2016-12-27 62 views
1

是否可以使用html-webpack-plugin在頭部和部分內容中編寫一些塊?Webpack - html-webpack-plugin - 一個文件名 - 多注入

這是我的HTML的WebPack-插件配置:

var HtmlWebpackPluginConfigIndex = { 
    template: path.join(__dirname, 'src/core/server/views', 'index.dust'), 
    filename: 'core/server/views/index.dust', 
    hash: true, 
    chunksSortMode: 'none' 
    // chunks: ['core/public/js/vendor'] 
}; 

當我使用複製與此配置,但與改變大塊:

var HtmlWebpackPluginConfigIndex = { 
    template: path.join(__dirname, 'src/core/server/views', 'index.dust'), 
    filename: 'core/server/views/index.dust', 
    hash: true, 
    inject: 'head', 
    chunksSortMode: 'none' 
    chunks: ['core/public/js/vendor'] 
}; 


var HtmlWebpackPluginConfigIndex2 = { 
    template: path.join(__dirname, 'src/core/server/views', 'index.dust'), 
    filename: 'core/server/views/index.dust', 
    hash: true, 
    inject: 'body', 
    chunksSortMode: 'none' 
    chunks: ['core/public/js/app'] 
}; 

.... 

new HtmlWebpackPlugin(HtmlWebpackPluginConfigIndex); 
new HtmlWebpackPlugin(HtmlWebpackPluginConfigIndex2); 

的WebPack僅去年申請大塊的HTML -webpack的插件。

回答

0

你可以嘗試建立該項目,然後搗鼓代碼是這樣的:
html-webpack-plugin index.js:

HtmlWebpackPlugin.prototype.generateAssetTags = function (assets) { 
    ... 
    // Add scripts to body or head 
    // <<< You would add this bit >>> 
    if (!! this.options.headScripts) { 
     var hScripts = this.options.headScripts; 
     head = head.concat(scripts.filter((s) => { 
      var src = s.attributes.src; 
      return hScripts.indexOf(src) > -1; 
     })); 
     body = body.concat(scripts.filter((s) => { 
      var src = s.attributes.src; 
      return hScripts.indexOf(src) < 0; 
     })); 
    // <<< to here (and the following "else" in front of the if) >>> 
    else if (this.options.inject === 'head') { 
    ... 
} 

,然後添加一個headScripts選項,你的插件定義:

plugins: [ 
    new HTMLWebpackPlugin({ 
     template: path.join(__dirname, 'src','core','server','views', 'index.dust'), 
     filename: 'core/server/views/index.dust', 
     headScripts: ['vendor.bundle.js'] // or whatever your output filename is 
    }) 
    ] 
相關問題