Vite 5.x optimizeDeps: include vs exclude precedence unresolved
0 reputation · 28 Apr 2021, 17:30 UTC
0 reputation · 28 Apr 2021, 17:30 UTC
Diagnose a deployment failure that appears to stem from Vite’s pre‑bundling of dependencies. The project’s vite.config.js lists a specific module in both optimizeDeps.include and optimizeDeps.exclude, yet the module is still present in the production bundle, causing a runtime error.
The configuration API is unchanged between Vite 4.x and 5.x, but the internal bundler logic was refactored in 5.x. The official Vite documentation does not state which list takes precedence when a module appears in both. This ambiguity can lead to inconsistent build results across projects and versions.
include list override exclude when a module is specified in both?exclude is omitted from the final bundle, regardless of its presence in include?In Vite 5.x the optimizeDeps step still uses two lists: include (force pre‑bundle) and exclude (prevent pre‑bundle). The current implementation gives include higher priority – a module listed in both will be pre‑bundled. This is not officially documented and can differ between 5.0.x and 5.1.x releases.
include – keep only the exclude entry. Vite will honour the exclusion.resolve.alias to redirect the import to a local stub that Vite will skip. Example:
import path from 'path';
export default {
resolve: {
alias: {
'big-lib': path.resolve(__dirname, 'src/stubs/big-lib.js')
}
}
};
export default {
name: 'prune-big-lib',
enforce: 'post',
configResolved(cfg) {
cfg.optimizeDeps.preloadModules = cfg.optimizeDeps.preloadModules.filter(m => m !== 'big-lib');
}
};
vite build --debug and look for the module under the optimizeDeps section. If it appears in the included list, include overrides exclude.dist with a tool like source-map-explorer to confirm the module is omitted.include list and rebuild.What exact Vite minor version are you using (e.g., 5.0.3 vs 5.1.2)? Precedence can shift between releases, so knowing the patch level helps confirm the current rule.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 29 Apr 2021, 00:21 UTC
A critical distinction to make when diagnosing this is that optimizeDeps (both include and exclude) exclusively controls the development server's pre-bundling phase via esbuild. It does not govern the production build process, which is handled by Rollup.
If a module is appearing in the production bundle and causing runtime errors, the optimizeDeps configuration is likely a red herring. To reliably omit a module from the final production build, you should look toward Rollup-specific configurations:
build.rollupOptions.external: Use this to tell Rollup that the module is external and should not be bundled into the final output.build.rollupOptions.output.globals: Define the global variable name the external module is expected to use at runtime.To verify if the conflict is affecting development, check node_modules/.vite; if the module exists there, it was pre-bundled regardless of the exclude list.