首页 > 解决方案 > webpackJsonp 改变 chunkHash

问题描述

vendors: {
 minSize: 20000,
 test: /[\\/]node_modules[\\/]/,
 chunks: 'initial',
 name(module) {
    // get the name. E.g. node_modules/packageName/not/this/part.js
    // or node_modules/packageName
    const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
    // npm package names are URL-safe, but some servers don't like @ symbols
    return `npm.${packageName.replace('@', '')}`;
 },
 reuseExistingChunk: true
}

在上面的 cacheGroup 中,我试图拆分每个 node_module,输出文件名为c[chunkhash].js

我希望 2 个不同的页面具有共同的导入,例如axios应该具有相同的“chunkHash”,但令我惊讶的是,事实并非如此。

在查看了这些块之后,我发现这 2 个块具有相同的主体,只是它们的 webpackJsonp 不同。

块1

(window.webpackJsonp = window.webpackJsonp || []).push([[1], {
    4: function (e, t, r) {
        "use strict";
        (function (e) {
            r.d(t, "a", (function () {
                return ye
            }));
            var n, o, i, a, c = r(0), u = r.n(c), s = r(5), l = r.n(s), f = r(6), p = r.n(f), d = r(1), h = r.n(d),
                y = r(7), T = r.n(y), b = "bodyAttributes", m = "htmlAttributes", g = "titleAttributes", v = {
                    BASE: "base",
                    BODY: "body",
                    HEAD: "head",
                    HTML: "html",
...

块 2

(window.webpackJsonp = window.webpackJsonp || []).push([[2], {
    5: function (e, t, r) {
        "use strict";
        (function (e) {
            r.d(t, "a", (function () {
                return ye
            }));
            var n, o, i, a, c = r(0), u = r.n(c), s = r(6), l = r.n(s), f = r(7), p = r.n(f), d = r(1), h = r.n(d),
                y = r(8), T = r.n(y), b = "bodyAttributes", m = "htmlAttributes", g = "titleAttributes", v = {
                    BASE: "base",
                    BODY: "body",
                    HEAD: "head",
                    HTML: "html",
...

两个块具有相同的主体,但 Jsonp 索引不同。在 chunk14中,在 chunk2 中5

这会导致浏览器两次下载相同的内容。

有逛街吗?

标签: javascriptwebpackwebpack-4chunks

解决方案


我发现,这是多编译器多入口webpack 配置之间的主要区别。

当您为 webpack 进行多编译器配置时,您将失去重用块和更好地进行代码拆分的能力。

因此,尝试坚持多入口 webpack 配置和单一运行时


推荐阅读