-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
.../__tests__/plugins/modulePreloadPolyfill/__snapshots__/modulePreloadPolyfill.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
})(); | ||
" | ||
`; |
58 changes: 58 additions & 0 deletions
58
packages/vite/src/node/__tests__/plugins/modulePreloadPolyfill/modulePreloadPolyfill.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<RollupOutput> | ||
|
||
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() | ||
}) | ||
}) |
4 changes: 4 additions & 0 deletions
4
packages/vite/src/node/__tests__/plugins/modulePreloadPolyfill/package/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "modulepreload-polyfill-test", | ||
"type": "module" | ||
} |