Skip to content

Commit d63129b

Browse files
authored
fix(build): declare moduleSideEffects for vite:modulepreload-polyfill (#13099)
1 parent c63ba3f commit d63129b

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`load > doesn't load modulepreload polyfill when format is cjs 1`] = `
4+
"\\"use strict\\";
5+
"
6+
`;
7+
8+
exports[`load > loads modulepreload polyfill 1`] = `
9+
"(function polyfill() {
10+
const relList = document.createElement(\\"link\\").relList;
11+
if (relList && relList.supports && relList.supports(\\"modulepreload\\")) {
12+
return;
13+
}
14+
for (const link of document.querySelectorAll('link[rel=\\"modulepreload\\"]')) {
15+
processPreload(link);
16+
}
17+
new MutationObserver((mutations) => {
18+
for (const mutation of mutations) {
19+
if (mutation.type !== \\"childList\\") {
20+
continue;
21+
}
22+
for (const node of mutation.addedNodes) {
23+
if (node.tagName === \\"LINK\\" && node.rel === \\"modulepreload\\")
24+
processPreload(node);
25+
}
26+
}
27+
}).observe(document, { childList: true, subtree: true });
28+
function getFetchOpts(link) {
29+
const fetchOpts = {};
30+
if (link.integrity)
31+
fetchOpts.integrity = link.integrity;
32+
if (link.referrerPolicy)
33+
fetchOpts.referrerPolicy = link.referrerPolicy;
34+
if (link.crossOrigin === \\"use-credentials\\")
35+
fetchOpts.credentials = \\"include\\";
36+
else if (link.crossOrigin === \\"anonymous\\")
37+
fetchOpts.credentials = \\"omit\\";
38+
else
39+
fetchOpts.credentials = \\"same-origin\\";
40+
return fetchOpts;
41+
}
42+
function processPreload(link) {
43+
if (link.ep)
44+
return;
45+
link.ep = true;
46+
const fetchOpts = getFetchOpts(link);
47+
fetch(link.href, fetchOpts);
48+
}
49+
})();
50+
"
51+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { describe, it } from 'vitest'
2+
import type { ModuleFormat, RollupOutput } from 'rollup'
3+
import { build } from '../../../build'
4+
import { modulePreloadPolyfillId } from '../../../plugins/modulePreloadPolyfill'
5+
6+
const buildProject = ({ format = 'es' as ModuleFormat } = {}) =>
7+
build({
8+
logLevel: 'silent',
9+
build: {
10+
write: false,
11+
rollupOptions: {
12+
input: 'main.js',
13+
output: {
14+
format,
15+
},
16+
treeshake: {
17+
moduleSideEffects: false,
18+
},
19+
},
20+
minify: false,
21+
},
22+
plugins: [
23+
{
24+
name: 'test',
25+
resolveId(id) {
26+
if (id === 'main.js') {
27+
return `\0${id}`
28+
}
29+
},
30+
load(id) {
31+
if (id === '\0main.js') {
32+
return `import '${modulePreloadPolyfillId}'`
33+
}
34+
},
35+
},
36+
],
37+
}) as Promise<RollupOutput>
38+
39+
describe('load', () => {
40+
it('loads modulepreload polyfill', async ({ expect }) => {
41+
const { output } = await buildProject()
42+
expect(output).toHaveLength(1)
43+
expect(output[0].code).toMatchSnapshot()
44+
})
45+
46+
it("doesn't load modulepreload polyfill when format is cjs", async ({
47+
expect,
48+
}) => {
49+
const { output } = await buildProject({ format: 'cjs' })
50+
expect(output).toHaveLength(1)
51+
expect(output[0].code).toMatchSnapshot()
52+
})
53+
})

packages/vite/src/node/plugins/modulePreloadPolyfill.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function modulePreloadPolyfillPlugin(config: ResolvedConfig): Plugin {
2525
if (!polyfillString) {
2626
polyfillString = `${isModernFlag}&&(${polyfill.toString()}());`
2727
}
28-
return polyfillString
28+
return { code: polyfillString, moduleSideEffects: true }
2929
}
3030
},
3131
}

0 commit comments

Comments
 (0)