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 preload-helper has no side effects #18057

Merged
merged 2 commits into from
Sep 10, 2024

Conversation

yuzheng14
Copy link
Contributor

Description

When there's is at least one dynamic import and the modulePreload is false, it will introduce a detectScriptRel function. But when the content imported dynamically is unused, the detectScriptRel will not be tree-shaken.

Minimum reproductible set: https://stackblitz.com/edit/vitejs-vite-8iwu7r?file=src%2Fmain.ts

There is a b.ts which exports a b:

export const b = 2;

And a a.ts which exports a a and a b that dynamically imports b from b.ts:

export const a = 1;

export async function b() {
  const b = (await import('./b.ts')).b;
  return b;
}

Then only use a in entry main.ts.

import { a } from './a.ts';

console.log(a);

Finally bundle it. It will present a IIFE of detectScriptRel in the product:

(function detectScriptRel() {
  const relList = typeof document !== "undefined" && document.createElement("link").relList;
  return relList && relList.supports && relList.supports("modulepreload") ? "modulepreload" : "preload";
})();
const a = 1;
console.log(a);

And the config of vite is:

import { defineConfig } from 'vite';

export default defineConfig({
  build: {
    minify: false,
    modulePreload: false,
  },
});

Copy link

stackblitz bot commented Sep 9, 2024

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@sapphi-red sapphi-red added the p2-nice-to-have Not breaking anything but nice to have (priority) label Sep 9, 2024
Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

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

Thanks!

@patak-dev patak-dev merged commit 587ad7b into vitejs:main Sep 10, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants