【问题标题】:How to prevent commonsChunkPlugin from injecting the chunks into html如何防止 commonsChunkPlugin 将块注入 html
【发布时间】:2019-04-24 10:58:31
【问题描述】:

我正在使用 preload-webpack-plugin 以及用于 webpack 的 commonschunkplugin。我的 webpack 版本是 3。

现在的问题是使用 preloadplugin,我已经在我的 vue 应用程序中预取异步路由。但是由于也用到了commonschunkplugin,所以在页面加载之后,也会注入这样的标签——

<script type="text/javascript" charset="utf-8" async="" src="/static/js/3.3c5729583b9ce21e8fd8.js"></script>

所以,结果页面变成了这样——

<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 配置 -

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 版本,尝试了各种配置变体,但没有任何效果。

任何形式的帮助都将不胜感激!

【问题讨论】:

    标签: webpack commonschunkplugin


    【解决方案1】:

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

    1. 使用excludeChunks 选项指定要忽略的块。
    2. 使用chunks 选项指定要包含的块。

    【讨论】:

    • 啊,你是对的。这样可行。但是现在如果我更改 HTMLWebpackPlugin 不注入这些块,我的应用程序将无法工作:(我发誓这在我上一个项目的早期工作。现在似乎无法弄清楚有什么不同。
    • 是的。通过 vuejs 中的异步路由加载。
    猜你喜欢
    • 2017-09-05
    • 2019-09-14
    • 2020-12-15
    • 2010-10-26
    • 2015-09-07
    • 2021-03-13
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多