-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Description
This is to provide a common issue for various discussions.
- Prior PRs: feat: add optimizeDeps.esbuildOptions #2991, feat(esbuild): add user esbuild plugins support #2886
- PRs/issues that could instead be solved by users using an option from this proposal: Collisions with case sensitivity in imports #2861, fix: allow scan to use user provided loaders #1884, Transitive dependencies can't be excluded from pre-bundling #2357
- Related discussions: Provide hook functions related to pre-build for plug-in development #3094, decorators not support in js for prebuild #2349, Feature Request: apply transform hooks in optimize #2511, Flow support #2238, Support transpileDependencies #2922
- + miscellaneous disussions in Vite Land (cc @ggoodman)
Motivation / Use cases
-
Plugin ecosystem Enable developers to create and use plugins that apply before prebundling
-
Dependency patching Many dependencies have compatibility issues with esbuild. These historically have worked with other bundlers due to them being less strict or due to non-standard behaviour. These can often be unmaintained, blocking users from switching to Vite until they migrate away from such deps.
- A workaround: patch-package – but this unfortunately does not work with pnpm
- Adding dependencies to
optimizeDeps.exclude– but since you're using this with dependencies that have compatibility issues, it often does not work since other issues require bundling or further fixes. optimizeDeps.exclude+ running esbuild separately for certain deps - here's a proof of concept for acustomOptimizeDepsplugin, which is my current workaround for this use case.
-
Non-standard setups It would be easier to migrate to Vite if you could work around issues such as fix: allow scan to use user provided loaders #1884 or Collisions with case sensitivity in imports #2861 using custom options/a plugin. These may not be recommended setups, but reduce friction when proposing a switch to a new build tool. Vite would not have to support such use cases natively, but would provide a useful escape hatch for those who need it.
Suggested solution
-
Provide a plugin hook that runs before pre-bundling, but doesn't require the user to opt-out of optimising dependencies. This would be especially valuable for plugin authors who want to hook into transforming dependencies before esbuild has touched them. This should cover many use cases, including patching dependencies. The advantage is that these plugins can be customised to
applyto build, serve, or both, unlike esbuild plugins which would run only on serve (as the transform API doesn't support plugins). They also would stay compatible ifesbuildis replaced with another pre-bundling setup, a concern that @patak-js expressed during a conversation in Vite Land. -
Even with pre-pre-bundling plugins, we should nevertheless allow a custom esbuild config, similar to how we allow a custom rollup config if users need to further customise the build. This is something I outlined in feat: add optimizeDeps.esbuildOptions #2991, but it could be adapted a bit. For example, we could expose it as a custom hook instead, something like
unstable_resolveEsbuildConfig, which would make it clear that it is an escape hatch and not stable.
Alternative
An entirely different approach for the esbuild config options could be to extract the current prebundling plugin fully, and allow users to replace it with their own plugin. However, this would likely be unnecessarily complex.
Additional context
N/A
cc @qnp