From 88d64af5567e6d90b1b3941db93b11ce491617e2 Mon Sep 17 00:00:00 2001 From: linl33 Date: Fri, 5 May 2023 12:54:48 +0800 Subject: [PATCH] test: add tests for modulepreload --- .../modulePreloadPolyfill.spec.ts.snap | 51 ++++++++++++++++ .../modulePreloadPolyfill.spec.ts | 58 +++++++++++++++++++ .../package/package.json | 4 ++ 3 files changed, 113 insertions(+) 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 create mode 100644 packages/vite/src/node/__tests__/plugins/modulePreloadPolyfill/package/package.json 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..01ba77a6d50fd9 --- /dev/null +++ b/packages/vite/src/node/__tests__/plugins/modulePreloadPolyfill/modulePreloadPolyfill.spec.ts @@ -0,0 +1,58 @@ +import { resolve } from 'node:path' +import { fileURLToPath } from 'node:url' +import { describe, it } from 'vitest' +import type { ModuleFormat, RollupOutput } from 'rollup' +import { build } from '../../../build' +import { modulePreloadPolyfillId } from '../../../plugins/modulePreloadPolyfill' + +const __dirname = resolve(fileURLToPath(import.meta.url), '..') + +const buildProject = ({ format = 'es' as ModuleFormat } = {}) => + build({ + root: resolve(__dirname, 'packages/build-project'), + 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/__tests__/plugins/modulePreloadPolyfill/package/package.json b/packages/vite/src/node/__tests__/plugins/modulePreloadPolyfill/package/package.json new file mode 100644 index 00000000000000..3bd38889692875 --- /dev/null +++ b/packages/vite/src/node/__tests__/plugins/modulePreloadPolyfill/package/package.json @@ -0,0 +1,4 @@ +{ + "name": "modulepreload-polyfill-test", + "type": "module" +}