From d63129b5f028646596bd5c57d2832eaf441c77b7 Mon Sep 17 00:00:00 2001 From: Li Lin Date: Mon, 8 May 2023 16:35:08 +0800 Subject: [PATCH] fix(build): declare moduleSideEffects for vite:modulepreload-polyfill (#13099) --- .../modulePreloadPolyfill.spec.ts.snap | 51 ++++++++++++++++++ .../modulePreloadPolyfill.spec.ts | 53 +++++++++++++++++++ .../src/node/plugins/modulePreloadPolyfill.ts | 2 +- 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 packages/vite/src/node/__tests__/plugins/modulePreloadPolyfill/__snapshots__/modulePreloadPolyfill.spec.ts.snap create mode 100644 packages/vite/src/node/__tests__/plugins/modulePreloadPolyfill/modulePreloadPolyfill.spec.ts diff --git a/packages/vite/src/node/__tests__/plugins/modulePreloadPolyfill/__snapshots__/modulePreloadPolyfill.spec.ts.snap b/packages/vite/src/node/__tests__/plugins/modulePreloadPolyfill/__snapshots__/modulePreloadPolyfill.spec.ts.snap new file mode 100644 index 00000000000000..7034be03a7c4db --- /dev/null +++ b/packages/vite/src/node/__tests__/plugins/modulePreloadPolyfill/__snapshots__/modulePreloadPolyfill.spec.ts.snap @@ -0,0 +1,51 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`load > doesn't load modulepreload polyfill when format is cjs 1`] = ` +"\\"use strict\\"; +" +`; + +exports[`load > loads modulepreload polyfill 1`] = ` +"(function polyfill() { + const relList = document.createElement(\\"link\\").relList; + if (relList && relList.supports && relList.supports(\\"modulepreload\\")) { + return; + } + for (const link of document.querySelectorAll('link[rel=\\"modulepreload\\"]')) { + processPreload(link); + } + new MutationObserver((mutations) => { + for (const mutation of mutations) { + if (mutation.type !== \\"childList\\") { + continue; + } + for (const node of mutation.addedNodes) { + if (node.tagName === \\"LINK\\" && node.rel === \\"modulepreload\\") + processPreload(node); + } + } + }).observe(document, { childList: true, subtree: true }); + function getFetchOpts(link) { + const fetchOpts = {}; + if (link.integrity) + fetchOpts.integrity = link.integrity; + if (link.referrerPolicy) + fetchOpts.referrerPolicy = link.referrerPolicy; + if (link.crossOrigin === \\"use-credentials\\") + fetchOpts.credentials = \\"include\\"; + else if (link.crossOrigin === \\"anonymous\\") + fetchOpts.credentials = \\"omit\\"; + else + fetchOpts.credentials = \\"same-origin\\"; + return fetchOpts; + } + function processPreload(link) { + if (link.ep) + return; + link.ep = true; + const fetchOpts = getFetchOpts(link); + fetch(link.href, fetchOpts); + } +})(); +" +`; diff --git a/packages/vite/src/node/__tests__/plugins/modulePreloadPolyfill/modulePreloadPolyfill.spec.ts b/packages/vite/src/node/__tests__/plugins/modulePreloadPolyfill/modulePreloadPolyfill.spec.ts new file mode 100644 index 00000000000000..3b24fbd5203baa --- /dev/null +++ b/packages/vite/src/node/__tests__/plugins/modulePreloadPolyfill/modulePreloadPolyfill.spec.ts @@ -0,0 +1,53 @@ +import { describe, it } from 'vitest' +import type { ModuleFormat, RollupOutput } from 'rollup' +import { build } from '../../../build' +import { modulePreloadPolyfillId } from '../../../plugins/modulePreloadPolyfill' + +const buildProject = ({ format = 'es' as ModuleFormat } = {}) => + build({ + logLevel: 'silent', + build: { + write: false, + rollupOptions: { + input: 'main.js', + output: { + format, + }, + treeshake: { + moduleSideEffects: false, + }, + }, + minify: false, + }, + plugins: [ + { + name: 'test', + resolveId(id) { + if (id === 'main.js') { + return `\0${id}` + } + }, + load(id) { + if (id === '\0main.js') { + return `import '${modulePreloadPolyfillId}'` + } + }, + }, + ], + }) as Promise + +describe('load', () => { + it('loads modulepreload polyfill', async ({ expect }) => { + const { output } = await buildProject() + expect(output).toHaveLength(1) + expect(output[0].code).toMatchSnapshot() + }) + + it("doesn't load modulepreload polyfill when format is cjs", async ({ + expect, + }) => { + const { output } = await buildProject({ format: 'cjs' }) + expect(output).toHaveLength(1) + expect(output[0].code).toMatchSnapshot() + }) +}) diff --git a/packages/vite/src/node/plugins/modulePreloadPolyfill.ts b/packages/vite/src/node/plugins/modulePreloadPolyfill.ts index 0f1ef11cf26f5e..ede79c8b882f77 100644 --- a/packages/vite/src/node/plugins/modulePreloadPolyfill.ts +++ b/packages/vite/src/node/plugins/modulePreloadPolyfill.ts @@ -25,7 +25,7 @@ export function modulePreloadPolyfillPlugin(config: ResolvedConfig): Plugin { if (!polyfillString) { polyfillString = `${isModernFlag}&&(${polyfill.toString()}());` } - return polyfillString + return { code: polyfillString, moduleSideEffects: true } } }, }