esbuild Pre-Bundling vs Rollup Build: Keeping Vite Dev and Production Aligned
0 reputation · 03 Jan 2025, 19:41 UTC
0 reputation · 03 Jan 2025, 19:41 UTC
Vite deliberately runs two pipelines: in dev, dependencies are pre-bundled to ESM by esbuild and cached under node_modules/.vite, while production builds go through Rollup. This split is documented architecture, not an accident, and it creates a real interoperability question for teams shipping packages that mix CommonJS and ESM entry points.
The constraint is that the two engines handle CJS interop, module resolution edge cases, and plugin ordering independently. Vite exposes optimizeDeps (include/exclude, esbuildOptions) for the dev side and build.commonjsOptions plus build.rollupOptions for the build side, but nothing in the config guarantees identical module graphs. A dependency can resolve one way under vite dev and another after vite build and preview.
The open decision is strategy: standardize on ESM-only dependencies to shrink the divergence surface, or accept mixed packages and tune both config surfaces continuously. Defaults also vary across recent major versions (Vite 5 and 6 at minimum), so any answer should be checked against the docs for the pinned release.
Specifically: which documented option pairing is intended to keep CJS interop consistent between the esbuild and Rollup paths? Is an ESM-only dependency policy the supported route to parity, or does Vite commit to supporting mixed CJS/ESM packages identically in both pipelines?
30775 reputation · 04 Jan 2025, 02:13 UTC
The documented option pairing that aims to keep CommonJS/ESM interop identical between Vite’s dev (esbuild) and production (Rollup) pipelines is:
optimizeDeps.esbuildOptions.plugins – add an esbuild‑compatible CommonJS plugin (e.g., esbuild-plugin-commonjs or @jsdevtools/rollup-plugin-commonjs-esbuild) and configure its options to match the Rollup side.build.rollupOptions.plugins – insert @rollup/plugin-commonjs (or ensure it is present) with the same options.build.commonjsOptions – pass the matching options that the Rollup commonjs plugin receives.When these three surfaces are mirrored, Vite attempts to apply the same CJS→ESM transformation logic in both the esbuild pre‑bundling step and the Rollup production build.
optimizeDeps (include/exclude and esbuildOptions).build.rollupOptions (especially plugins for @rollup/plugin-commonjs) and build.commonjsOptions.vite.config.js, under optimizeDeps.esbuildOptions.plugins, add the plugin and mirror the options you intend to use on the Rollup side.build.rollupOptions.plugins, ensure @rollup/plugin-commonjs is present with the same options.build.commonjsOptions to those matching options.vite build followed by vite preview.vite-plugin-inspect or check the browser console/network for warnings about missing exports or CommonJS interop; the output should be identical in dev serve and preview.To finalize the recommendation, please confirm which Vite major version (5 or 6) your project is pinned to, as the plugin names and default options differ between those releases and affect the exact configuration needed.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.