Skip to content

Pre-bundling should be more resilient to errors #3715

@Conduitry

Description

@Conduitry

Clear and concise description of the problem

When Vite fails during the pre-bundling of a particular dependency for the browser, it seems to get into a state where it refuses to pre-bundle any further dependencies. Even if the reference to the offending dependency is removed, in dev mode Vite remains in this state until the server is restarted.

This is an issue because some dependencies simply can't be pre-bundled for the browser (for example, if they make use of Node APIs). Mentioning these in code that's meant to be isomorphic for the browser and the server - even if it's inside if (false) { import ('something'); } - causes Vite to attempt to pre-bundle them, resulting in an error, and also resulting in other dependencies not being pre-bundled, resulting in (for example) CJS being served to the browser when it's expecting ESM.

Suggested solution

I do not know Vite's internals well enough to suggest what should be changed.

Alternative

Other options that seem less good to me (because they don't solve the underlying brittleness issue) would be to make Vite smarter about import()s that it sees could never be reached, or to make Vite only actually bundle dependencies for the browser when they are requested. Both of these might be good ideas by themselves, but even if someone does accidentally import something on the browser that cannot be bundled for the browser, this shouldn't prevent other dependencies from being pre-bundled until Vite is restarted.

Additional context

See sveltejs/kit#1570 which contains a reproduction using SvelteKit.

For now, my workaround is to hide the import()s from Vite that I don't want it to try to pre-bundle by wrapping them in another function call, which then calls import().

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestp2-nice-to-haveNot breaking anything but nice to have (priority)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions