-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
Better handling of dynamic imports in dead code #5676
Comments
This plugin mitigates the issue but breaks vue SFC styling: // Replace import.meta.env.SSR in a pre plugin so that esbuild can eliminate
// dynamic imports in the dead branch.
function DefineSSR(): PluginOption {
return {
name: "define ssr",
async transform(code, _id, ssr) {
if (!ssr && code.includes("import.meta.env.SSR")) {
return code.replace(/import.meta.env.SSR/g, "false");
}
return undefined;
},
enforce: "pre",
};
} |
That's strange. Vite does replace |
Yes. Moving the define plugin before esbuild will solve this issue, but it breaks vue SFC styling. |
Use the following configuration as a workaround to solve the problem, and the dead code is removed after a successful build. import { defineConfig } from 'vite'
import { builtinModules } from 'module'
export default defineConfig({
build: {
rollupOptions: {
external: builtinModules
}
}
}) This may not be the replacement of
In dev, the imported module is handled by vite only when it is executed, where errors do not occur because it is not executed. In build, Vite uses the After some experimentation, I found that I couldn't avoid this behavior, either by bringing the tree-shaking of |
I use in practice this code does not get into browser bundle, but this warning makes me worry Info
|
I have the same problem |
Problem is still relevant |
You could use define instead |
How do you dynamic setting it? Like |
i didn't notice the difference at all, warning remained |
@Tnze either have multiple config files or have your config file change the defined value based on the mode or environment variable or something |
Adding my comment from #18790:
|
Describe the bug
When working with SSR, one would expect patterns like this to work:
I.e., ignore
loadServer
for the browser bundle. It actually works fine with the dev server, but whenloadServer
(transitively) imports node builtin modules,vite build
fails with e.g.:It would be nice if vite could just figure out that
loadServer
will never be needed for the browser bundle, and don't try to analyze/resolve code in it. It would also make dev and build behavior more consistent and might perform better in some cases.I also tried this with esbuild and it does just ignore
loadServer
in this case:Reproduction
https://stackblitz.com/edit/vitejs-vite-ts9jc6?devtoolsheight=33&file=main.js
System Info
System: OS: Linux undefined CPU: (4) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz Memory: 0 Bytes / 0 Bytes Shell: Unknown - /bin/jsh Binaries: Node: 14.16.0 - /usr/local/bin/node Yarn: 1.22.10 - /bin/yarn npm: 7.17.0 - /bin/npm npmPackages: vite: ^2.6.13 => 2.6.14
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: