Vite optimizeDeps.include Glob Pattern Support and Precedence with exclude
0 reputation · 01 Jun 2020, 12:01 UTC
0 reputation · 01 Jun 2020, 12:01 UTC
When configuring Vite's dependency pre‑bundling, I want to use a glob pattern such as lodash-* in optimizeDeps.include to automatically pre‑bundle all lodash submodules without listing each one individually. However, the documentation states that optimizeDeps.include accepts only exact package name strings and does not interpret glob patterns, and it does not specify whether inclusions are processed before exclusions when both optimizeDeps.include and optimizeDeps.exclude are present. This leaves the effective set of pre‑bundled dependencies unclear.
lodash-* as a literal package name or ignore it during dependency scanning?optimizeDeps.include and optimizeDeps.exclude are defined, which operation is applied first to determine the final list?34275 reputation · 01 Jun 2020, 19:32 UTC
Vite’s optimizeDeps.include array does not interpret glob patterns. A value such as lodash-* is treated as a literal package name, which will not match any installed package and is effectively ignored. When both optimizeDeps.include and optimizeDeps.exclude are present, the exclude list takes precedence: any package listed in exclude is removed from the pre‑bundled set regardless of its presence in include.
optimizeDeps.include accepts only exact strings (package names or paths). No glob or wildcard matching is performed.optimizeDeps.exclude is evaluated after include and overrides it; excluded packages are never pre‑bundled.import statements. Manual inclusion is only needed for dependencies that are dynamically imported or not detected by the scanner.Vite keeps the optimizeDeps configuration simple to avoid costly pattern matching during dev server startup and to make the behavior predictable. Supporting globs would require scanning the entire node_modules tree for each pattern, which could slow startup and introduce subtle mismatches.
lodash.debounce, lodash.throttle), list them individually:// vite.config.js
import { defineConfig } from 'vite'
export default defineConfig({
optimizeDeps: {
include: [
'lodash.debounce',
'lodash.throttle',
'lodash.cloneDeep'
// add any other lodash-* packages you use
],
exclude: [] // keep empty unless you need to omit something
}
})
include entirely and rely on static analysis. Only add to include when you see a missing pre‑bundle in the dev server.After starting the dev server, check which packages were pre‑bundled:
ls node_modules/.vite/deps
Each bundled dependency appears as a file like lodash-debounce.js. In the browser DevTools → Network tab, filter by node_modules/.vite/deps; requests to raw node_modules/lodash/... indicate a package was not pre‑bundled.
Are you importing lodash submodules dynamically (e.g., import(`lodash/${name}`))? If so, manual inclusion via optimizeDeps.include is necessary; otherwise you can rely on Vite’s automatic detection and omit the include array entirely.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.