diff --git a/lib/prefetch/ChunkPrefetchPreloadPlugin.js b/lib/prefetch/ChunkPrefetchPreloadPlugin.js index 811b94eab..a1e26561e 100644 --- a/lib/prefetch/ChunkPrefetchPreloadPlugin.js +++ b/lib/prefetch/ChunkPrefetchPreloadPlugin.js @@ -107,7 +107,10 @@ class ChunkPrefetchPreloadPlugin { if ( chunksFetchPriorityMap[c.id] && - prefetchOrder > chunksFetchPriorityMap[c.id].prefetchOrder + prefetchOrder && + (chunksFetchPriorityMap[c.id].prefetchOrder === undefined || + prefetchOrder > + chunksFetchPriorityMap[c.id].prefetchOrder) ) { chunksFetchPriorityMap[c.id] = { fetchPriority, @@ -116,7 +119,7 @@ class ChunkPrefetchPreloadPlugin { } else if (!chunksFetchPriorityMap[c.id]) { chunksFetchPriorityMap[c.id] = { fetchPriority, - prefetchOrder: prefetchOrder || 0 + prefetchOrder: prefetchOrder }; } }); diff --git a/test/configCases/web/fetch-priority-2/d.js b/test/configCases/web/fetch-priority-2/d.js new file mode 100644 index 000000000..ff3b76baf --- /dev/null +++ b/test/configCases/web/fetch-priority-2/d.js @@ -0,0 +1,3 @@ +export default function test() { + import("./d1"); +} diff --git a/test/configCases/web/fetch-priority-2/d1.js b/test/configCases/web/fetch-priority-2/d1.js new file mode 100644 index 000000000..7334701be --- /dev/null +++ b/test/configCases/web/fetch-priority-2/d1.js @@ -0,0 +1,3 @@ +export default function test() { + import("./d2"); +} diff --git a/test/configCases/web/fetch-priority-2/d2.js b/test/configCases/web/fetch-priority-2/d2.js new file mode 100644 index 000000000..e64231db3 --- /dev/null +++ b/test/configCases/web/fetch-priority-2/d2.js @@ -0,0 +1,3 @@ +export default function test() { + import(/* webpackFetchPriority: "high" */ "./d3"); +} diff --git a/test/configCases/web/fetch-priority-2/d3.js b/test/configCases/web/fetch-priority-2/d3.js new file mode 100644 index 000000000..a665afd6b --- /dev/null +++ b/test/configCases/web/fetch-priority-2/d3.js @@ -0,0 +1 @@ +export default "d3"; diff --git a/test/configCases/web/fetch-priority-2/index.js b/test/configCases/web/fetch-priority-2/index.js index 2e05e2e42..9f5289ec4 100644 --- a/test/configCases/web/fetch-priority-2/index.js +++ b/test/configCases/web/fetch-priority-2/index.js @@ -1,18 +1,28 @@ it("should set fetchPriority", () => { - import("./a"); - expect(document.head._children).toHaveLength(3); - const script1 = document.head._children[1]; - expect(script1._attributes.fetchpriority).toBe("low"); - - import("./b"); + import(/* webpackFetchPriority: "high" */ "./a"); expect(document.head._children).toHaveLength(4); - const script2 = document.head._children[3]; + const script1 = document.head._children[2]; + expect(script1._attributes.fetchpriority).toBe("high"); + + import(/* webpackFetchPriority: "low" */ "./b"); + expect(document.head._children).toHaveLength(5); + const script2 = document.head._children[4]; expect(script2._attributes.fetchpriority).toBe("low"); - import( "./c"); - expect(document.head._children).toHaveLength(5); - const script3 = document.head._children[4]; + import(/* webpackFetchPriority: "low" */ "./c"); + expect(document.head._children).toHaveLength(6); + const script3 = document.head._children[5]; expect(script3._attributes.fetchpriority).toBe("auto"); import(/* webpackPrefetch: 20, webpackFetchPriority: "auto" */ "./c"); + + import("./d") + expect(document.head._children).toHaveLength(7); + const script4 = document.head._children[6]; + expect(script4._attributes.fetchpriority).toBeUndefined(); + + import(/* webpackPrefetch: -20 */ "./d3"); + expect(document.head._children).toHaveLength(8); + const script5 = document.head._children[7]; + expect(script5._attributes.fetchpriority).toBe("high"); }); diff --git a/test/configCases/web/fetch-priority-2/webpack.config.js b/test/configCases/web/fetch-priority-2/webpack.config.js index c4fa9ff9d..df7784102 100644 --- a/test/configCases/web/fetch-priority-2/webpack.config.js +++ b/test/configCases/web/fetch-priority-2/webpack.config.js @@ -12,10 +12,15 @@ module.exports = { } }, module: { - parser: { - javascript: { - dynamicImportFetchPriority: "low" + rules: [ + { + test: /d\.js$/, + parser: { + javascript: { + dynamicImportFetchPriority: "low" + } + } } - } + ] } };