Skip to content

Commit 1422395

Browse files
authored
fix: treat all optimizeDeps.entries values as globs (vitejs#20045)
1 parent 6c3dd8e commit 1422395

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

docs/config/dep-optimization-options.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Unless noted, the options in this section are only applied to the dependency opt
1010

1111
By default, Vite will crawl all your `.html` files to detect dependencies that need to be pre-bundled (ignoring `node_modules`, `build.outDir`, `__tests__` and `coverage`). If `build.rollupOptions.input` is specified, Vite will crawl those entry points instead.
1212

13-
If neither of these fit your needs, you can specify custom entries using this option - the value should be a [`tinyglobby` pattern](https://github.com/SuperchupuDev/tinyglobby) or array of patterns that are relative from Vite project root. This will overwrite default entries inference. Only `node_modules` and `build.outDir` folders will be ignored by default when `optimizeDeps.entries` is explicitly defined. If other folders need to be ignored, you can use an ignore pattern as part of the entries list, marked with an initial `!`. If you don't want to ignore `node_modules` and `build.outDir`, you can specify using literal string paths (without `tinyglobby` patterns) instead.
13+
If neither of these fit your needs, you can specify custom entries using this option - the value should be a [`tinyglobby` pattern](https://github.com/SuperchupuDev/tinyglobby) or array of patterns that are relative from Vite project root. This will overwrite default entries inference. Only `node_modules` and `build.outDir` folders will be ignored by default when `optimizeDeps.entries` is explicitly defined. If other folders need to be ignored, you can use an ignore pattern as part of the entries list, marked with an initial `!`. `node_modules` will not be ignored for patterns that explicitly include the string `node_modules`.
1414

1515
## optimizeDeps.exclude
1616

packages/vite/src/node/optimizer/scan.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type {
1212
import esbuild, { formatMessages, transform } from 'esbuild'
1313
import type { PartialResolvedId } from 'rollup'
1414
import colors from 'picocolors'
15-
import { glob, isDynamicPattern } from 'tinyglobby'
15+
import { glob } from 'tinyglobby'
1616
import {
1717
CSS_LANGS_RE,
1818
JS_TYPES_RE,
@@ -318,25 +318,42 @@ function orderedDependencies(deps: Record<string, string>) {
318318
return Object.fromEntries(depsList)
319319
}
320320

321-
function globEntries(pattern: string | string[], environment: ScanEnvironment) {
322-
const resolvedPatterns = arraify(pattern)
323-
if (resolvedPatterns.every((str) => !isDynamicPattern(str))) {
324-
return resolvedPatterns.map((p) =>
325-
normalizePath(path.resolve(environment.config.root, p)),
326-
)
321+
async function globEntries(
322+
patterns: string | string[],
323+
environment: ScanEnvironment,
324+
) {
325+
const nodeModulesPatterns: string[] = []
326+
const regularPatterns: string[] = []
327+
328+
for (const pattern of arraify(patterns)) {
329+
if (pattern.includes('node_modules')) {
330+
nodeModulesPatterns.push(pattern)
331+
} else {
332+
regularPatterns.push(pattern)
333+
}
327334
}
328-
return glob(pattern, {
335+
336+
const sharedOptions = {
329337
absolute: true,
330338
cwd: environment.config.root,
331339
ignore: [
332-
'**/node_modules/**',
333340
`**/${environment.config.build.outDir}/**`,
334341
// if there aren't explicit entries, also ignore other common folders
335342
...(environment.config.optimizeDeps.entries
336343
? []
337344
: [`**/__tests__/**`, `**/coverage/**`]),
338345
],
339-
})
346+
}
347+
348+
const results = await Promise.all([
349+
glob(nodeModulesPatterns, sharedOptions),
350+
glob(regularPatterns, {
351+
...sharedOptions,
352+
ignore: [...sharedOptions.ignore, '**/node_modules/**'],
353+
}),
354+
])
355+
356+
return results.flat()
340357
}
341358

342359
export const scriptRE =

0 commit comments

Comments
 (0)