首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何防止commonsChunkPlugin向html中注入块

如何防止commonsChunkPlugin向html中注入块
EN

Stack Overflow用户
提问于 2018-11-21 19:40:53
回答 1查看 586关注 0票数 2

我正在使用预装- webpack插件,同时为webpack使用公共插件。我的webpack版本是3。

现在的问题是,使用预加载插件,我已经在vue应用程序中预取异步路由。但是,由于公共插件也被使用,在页面加载之后,它也会注入这样的标记-

代码语言:javascript
复制
<script type="text/javascript" charset="utf-8" async="" src="/static/js/3.3c5729583b9ce21e8fd8.js"></script>

因此,这一页变成了这样-

代码语言:javascript
复制
<link rel="prefetch" href="/static/js/0.806cdda5777e81bb1a8b.js">
<link rel="prefetch" href="/static/js/1.95cf2f4474949209d50b.js">
<link rel="prefetch" href="/static/js/2.5231ce0aada93adb0dd7.js">
<link rel="prefetch" href="/static/js/3.6eef2cb918c01f721e28.js">
<link rel="prefetch" href="/static/js/4.228c75e21459a43cb71c.js">
<script type="text/javascript" charset="utf-8" async="" src="/static/js/0.806cdda5777e81bb1a8b.js"></script>
<script type="text/javascript" charset="utf-8" async="" src="/static/js/4.228c75e21459a43cb71c.js"></script>

我不想插入最后两个脚本标记,因为我已经在预取它们了。

这是我的副手webpack配置-

代码语言:javascript
复制
new HtmlWebpackPlugin({
      filename: config.build.index,
      template: 'index.html',
      inject: true,
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: true
        // more options:
        // https://github.com/kangax/html-minifier#options-quick-reference
      },
      // necessary to consistently work with multiple chunks via CommonsChunkPlugin
      chunksSortMode: 'dependency'
    }),
    // preloading assets
    new PreloadWebpackPlugin({
      rel: 'prefetch'
    }),
    // keep module.id stable when vendor modules does not change
    new webpack.HashedModuleIdsPlugin(),
    // enable scope hoisting
    new webpack.optimize.ModuleConcatenationPlugin(),
    // split vendor js into its own file
    new webpack.optimize.CommonsChunkPlugin({
      name: 'vendor',
      minChunks (module) {
        // any required modules inside node_modules are extracted to vendor
        return (
          module.resource &&
          /\.js$/.test(module.resource) &&
          module.resource.indexOf(
            path.join(__dirname, '../node_modules')
          ) === 0
        )
      }
    }),
    // extract webpack runtime and module manifest to its own file in order to
    // prevent vendor hash from being updated whenever app bundle is updated
    new webpack.optimize.CommonsChunkPlugin({
      name: 'manifest',
      minChunks: Infinity
    }),
    // This instance extracts shared chunks from code splitted chunks and bundles them
    // in a separate chunk, similar to the vendor chunk
    // see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk
    new webpack.optimize.CommonsChunkPlugin({
      name: 'app',
      async: 'vendor-async',
      children: true,
      minChunks: 3
    }),

我在之前的一个项目中做过这个工作。但不知何故,我不知道为什么在这个项目中,这些异步标记不断被注入。我已经匹配了相同的webpack版本,尝试了各种配置变体,但是没有什么效果。

在这方面的任何帮助都将不胜感激!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-21 20:18:50

将这些脚本注入到HTML中的是HTMLWebpackPlugin,您有两个选项:

  1. 使用excludeChunks选项指定要忽略哪些块。
  2. 使用chunks选项指定要包含哪些块。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53419432

复制
相关文章

相似问题

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