Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(build): declare moduleSideEffects for vite:modulepreload-polyfill #13099

Merged
merged 4 commits into from May 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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,58 @@
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
patak-dev marked this conversation as resolved.
Show resolved Hide resolved
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()
})
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't look like this file is used. Could we remove it?

Copy link
Contributor Author

@linl33 linl33 May 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just removed it, I forgot to delete it.

@@ -0,0 +1,4 @@
{
"name": "modulepreload-polyfill-test",
"type": "module"
}
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