Skip to content

Commit

Permalink
fix(build): declare moduleSideEffects for vite:modulepreload-polyfill (
Browse files Browse the repository at this point in the history
  • Loading branch information
linl33 committed May 8, 2023
1 parent c63ba3f commit d63129b
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 1 deletion.
@@ -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);
}
})();
"
`;
@@ -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<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()
})
})
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/modulePreloadPolyfill.ts
Expand Up @@ -25,7 +25,7 @@ export function modulePreloadPolyfillPlugin(config: ResolvedConfig): Plugin {
if (!polyfillString) {
polyfillString = `${isModernFlag}&&(${polyfill.toString()}());`
}
return polyfillString
return { code: polyfillString, moduleSideEffects: true }
}
},
}
Expand Down

0 comments on commit d63129b

Please sign in to comment.