Skip to content

Commit

Permalink
test: add tests for modulepreload
Browse files Browse the repository at this point in the history
  • Loading branch information
linl33 committed May 5, 2023
1 parent 46ff825 commit 88d64af
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
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);
}
})();
"
`;
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()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "modulepreload-polyfill-test",
"type": "module"
}

0 comments on commit 88d64af

Please sign in to comment.