webpack/test/configCases/web/prefetch-preload-module-jsonp/index.mjs

140 lines
4.8 KiB
JavaScript
Raw Normal View History

2024-03-25 21:48:46 +08:00
// This config need to be set on initial evaluation to be effective
__webpack_nonce__ = "nonce";
__webpack_public_path__ = "https://example.com/public/path/";
it("should prefetch and preload child chunks on chunk load", () => {
let link, script;
2024-11-20 03:18:51 +08:00
expect(document.head._children).toHaveLength(3);
2024-03-25 21:48:46 +08:00
// Test prefetch from entry chunk
link = document.head._children[0];
expect(link._type).toBe("link");
expect(link.rel).toBe("prefetch");
expect(link.as).toBe("script");
expect(link.href).toBe("https://example.com/public/path/chunk1.js");
link = document.head._children[1];
expect(link._type).toBe("link");
expect(link.rel).toBe("prefetch");
expect(link.as).toBe("style");
expect(link.href).toBe("https://example.com/public/path/chunk2-css.css");
2024-11-20 03:18:51 +08:00
link = document.head._children[2];
expect(link._type).toBe("link");
expect(link.rel).toBe("prefetch");
expect(link.as).toBe("script");
expect(link.href).toBe("https://example.com/public/path/chunk2-css.js");
2024-03-25 21:48:46 +08:00
const promise = import(
/* webpackChunkName: "chunk1", webpackPrefetch: true */ "./chunk1.js"
);
2024-11-20 03:18:51 +08:00
expect(document.head._children).toHaveLength(7);
2024-03-25 21:48:46 +08:00
// Test normal script loading
2024-11-20 03:18:51 +08:00
script = document.head._children[3];
2024-03-25 21:48:46 +08:00
expect(script._type).toBe("script");
expect(script.src).toBe("https://example.com/public/path/chunk1.js");
expect(script.getAttribute("nonce")).toBe("nonce");
expect(script.crossOrigin).toBe("anonymous");
expect(script.onload).toBeTypeOf("function");
// Test preload of chunk1-b
2024-11-20 03:18:51 +08:00
link = document.head._children[4];
2024-03-25 21:48:46 +08:00
expect(link._type).toBe("link");
expect(link.rel).toBe("modulepreload");
expect(link.href).toBe("https://example.com/public/path/chunk1-b.js");
expect(link.charset).toBe("utf-8");
expect(link.getAttribute("nonce")).toBe("nonce");
expect(link.crossOrigin).toBe("anonymous");
// Test preload of chunk1-a-css
2024-11-20 03:18:51 +08:00
link = document.head._children[5];
2024-03-25 21:48:46 +08:00
expect(link._type).toBe("link");
expect(link.rel).toBe("preload");
expect(link.as).toBe("style");
expect(link.href).toBe("https://example.com/public/path/chunk1-a-css.css");
2024-11-20 03:18:51 +08:00
link = document.head._children[6];
expect(link._type).toBe("link");
expect(link.rel).toBe("modulepreload");
expect(link.href).toBe("https://example.com/public/path/chunk1-a-css.js");
expect(link.charset).toBe("utf-8");
expect(link.getAttribute("nonce")).toBe("nonce");
expect(link.crossOrigin).toBe("anonymous");
2024-03-25 21:48:46 +08:00
// Run the script
import(/* webpackIgnore: true */ "./chunk1.js");
script.onload();
return promise.then(() => {
2024-11-20 03:18:51 +08:00
expect(document.head._children).toHaveLength(8);
2024-03-25 21:48:46 +08:00
// Test prefetching for chunk1-c and chunk1-a in this order
2024-11-20 03:18:51 +08:00
link = document.head._children[6];
2024-03-25 21:48:46 +08:00
expect(link._type).toBe("link");
expect(link.rel).toBe("prefetch");
expect(link.as).toBe("script");
expect(link.href).toBe("https://example.com/public/path/chunk1-c.js");
expect(link.crossOrigin).toBe("anonymous");
2024-11-20 03:18:51 +08:00
link = document.head._children[7];
2024-03-25 21:48:46 +08:00
expect(link._type).toBe("link");
expect(link.rel).toBe("prefetch");
expect(link.as).toBe("script");
expect(link.href).toBe("https://example.com/public/path/chunk1-a.js");
expect(link.crossOrigin).toBe("anonymous");
const promise2 = import(
/* webpackChunkName: "chunk1", webpackPrefetch: true */ "./chunk1.js"
);
// Loading chunk1 again should not trigger prefetch/preload
2024-11-20 03:18:51 +08:00
expect(document.head._children).toHaveLength(8);
2024-03-25 21:48:46 +08:00
const promise3 = import(/* webpackChunkName: "chunk2" */ "./chunk2.js");
2024-11-20 03:18:51 +08:00
expect(document.head._children).toHaveLength(9);
2024-03-25 21:48:46 +08:00
// Test normal script loading
2024-11-20 03:18:51 +08:00
script = document.head._children[8];
2024-03-25 21:48:46 +08:00
expect(script._type).toBe("script");
expect(script.src).toBe("https://example.com/public/path/chunk2.js");
expect(script.getAttribute("nonce")).toBe("nonce");
expect(script.crossOrigin).toBe("anonymous");
expect(script.onload).toBeTypeOf("function");
// Run the script
import(/* webpackIgnore: true */ "./chunk2.js");
script.onload();
return promise3.then(() => {
// Loading chunk2 again should not trigger prefetch/preload as it's already prefetch/preloaded
2024-11-20 03:18:51 +08:00
expect(document.head._children).toHaveLength(8);
2024-03-25 21:48:46 +08:00
const promise4 = import(/* webpackChunkName: "chunk1-css" */ "./chunk1.css");
2024-11-20 03:18:51 +08:00
expect(document.head._children).toHaveLength(10);
2024-03-25 21:48:46 +08:00
2024-11-20 03:18:51 +08:00
link = document.head._children[8];
2024-03-25 21:48:46 +08:00
expect(link._type).toBe("link");
expect(link.rel).toBe("stylesheet");
expect(link.href).toBe("https://example.com/public/path/chunk1-css.css");
expect(link.crossOrigin).toBe("anonymous");
const promise5 = import(/* webpackChunkName: "chunk2-css", webpackPrefetch: true */ "./chunk2.css");
2024-11-20 03:18:51 +08:00
expect(document.head._children).toHaveLength(12);
2024-03-25 21:48:46 +08:00
2024-11-20 03:18:51 +08:00
link = document.head._children[10];
2024-03-25 21:48:46 +08:00
expect(link._type).toBe("link");
expect(link.rel).toBe("stylesheet");
expect(link.href).toBe("https://example.com/public/path/chunk2-css.css");
expect(link.crossOrigin).toBe("anonymous");
});
});
});