首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Webpack - html-webpack-plugin -一个文件名-多重注入

Webpack - html-webpack-plugin -一个文件名-多重注入
EN

Stack Overflow用户
提问于 2016-12-27 23:44:42
回答 1查看 721关注 0票数 1

可以用html-webpack-plugin在头上写一些块,在身体里写一些块吗?

这是我的html-webpack-插件的配置:

代码语言:javascript
复制
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']
};

当我在此配置中使用copy,但对已更改的块使用时:

代码语言:javascript
复制
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-插件。

EN

回答 1

Stack Overflow用户

发布于 2018-02-14 22:34:26

您可以尝试分叉项目,然后篡改代码,如下所示:

html-webpack-plugin index.js:

代码语言:javascript
复制
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选项:

代码语言:javascript
复制
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
    })
  ]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41353585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档