Skip to content

Commit 6f74a3a

Browse files
authored
chore(deps)!: migrate fast-glob to tinyglobby (#18243)
1 parent b5d1a05 commit 6f74a3a

File tree

17 files changed

+121
-67
lines changed

17 files changed

+121
-67
lines changed

docs/config/dep-optimization-options.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
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.
1010

11-
If neither of these fit your needs, you can specify custom entries using this option - the value should be a [fast-glob pattern](https://github.com/mrmlnc/fast-glob#basic-syntax) 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 fast-glob patterns) instead.
11+
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.
1212

1313
## optimizeDeps.exclude
1414

docs/config/server-options.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ The error that appears in the Browser when the fallback happens can be ignored.
184184

185185
Warm up files to transform and cache the results in advance. This improves the initial page load during server starts and prevents transform waterfalls.
186186

187-
`clientFiles` are files that are used in the client only, while `ssrFiles` are files that are used in SSR only. They accept an array of file paths or [`fast-glob`](https://github.com/mrmlnc/fast-glob) patterns relative to the `root`.
187+
`clientFiles` are files that are used in the client only, while `ssrFiles` are files that are used in SSR only. They accept an array of file paths or [`tinyglobby`](https://github.com/SuperchupuDev/tinyglobby) patterns relative to the `root`.
188188

189189
Make sure to only add files that are frequently used to not overload the Vite dev server on startup.
190190

docs/guide/features.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ Note that:
542542

543543
- This is a Vite-only feature and is not a web or ES standard.
544544
- The glob patterns are treated like import specifiers: they must be either relative (start with `./`) or absolute (start with `/`, resolved relative to project root) or an alias path (see [`resolve.alias` option](/config/shared-options.md#resolve-alias)).
545-
- The glob matching is done via [`fast-glob`](https://github.com/mrmlnc/fast-glob) - check out its documentation for [supported glob patterns](https://github.com/mrmlnc/fast-glob#pattern-syntax).
545+
- The glob matching is done via [`tinyglobby`](https://github.com/SuperchupuDev/tinyglobby).
546546
- You should also be aware that all the arguments in the `import.meta.glob` must be **passed as literals**. You can NOT use variables or expressions in them.
547547

548548
## Dynamic Import

packages/vite/LICENSE.md

+29
Original file line numberDiff line numberDiff line change
@@ -2405,6 +2405,35 @@ Repository: git+https://github.com/antfu/strip-literal.git
24052405
24062406
---------------------------------------
24072407

2408+
## tinyglobby
2409+
License: MIT
2410+
By: Superchupu
2411+
Repository: git+https://github.com/SuperchupuDev/tinyglobby.git
2412+
2413+
> MIT License
2414+
>
2415+
> Copyright (c) 2024 Madeline Gurriarán
2416+
>
2417+
> Permission is hereby granted, free of charge, to any person obtaining a copy
2418+
> of this software and associated documentation files (the "Software"), to deal
2419+
> in the Software without restriction, including without limitation the rights
2420+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
2421+
> copies of the Software, and to permit persons to whom the Software is
2422+
> furnished to do so, subject to the following conditions:
2423+
>
2424+
> The above copyright notice and this permission notice shall be included in all
2425+
> copies or substantial portions of the Software.
2426+
>
2427+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2428+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2429+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2430+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2431+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2432+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2433+
> SOFTWARE.
2434+
2435+
---------------------------------------
2436+
24082437
## to-regex-range
24092438
License: MIT
24102439
By: Jon Schlinkert, Rouven Weßling

packages/vite/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@
120120
"escape-html": "^1.0.3",
121121
"estree-walker": "^3.0.3",
122122
"etag": "^1.8.1",
123-
"fast-glob": "^3.3.2",
124123
"http-proxy": "^1.18.1",
125124
"launch-editor-middleware": "^2.9.1",
126125
"lightningcss": "^1.27.0",
@@ -148,6 +147,7 @@
148147
"source-map-support": "^0.5.21",
149148
"strip-ansi": "^7.1.0",
150149
"strip-literal": "^2.1.0",
150+
"tinyglobby": "^0.2.9",
151151
"tsconfck": "^3.1.4",
152152
"tslib": "^2.7.0",
153153
"types": "link:./types",

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import colors from 'picocolors'
77
import type { BuildContext, BuildOptions as EsbuildBuildOptions } from 'esbuild'
88
import esbuild, { build } from 'esbuild'
99
import { init, parse } from 'es-module-lexer'
10-
import glob from 'fast-glob'
10+
import { isDynamicPattern } from 'tinyglobby'
1111
import type { ResolvedConfig } from '../config'
1212
import {
1313
createDebugger,
@@ -151,8 +151,8 @@ export type DepOptimizationOptions = DepOptimizationConfig & {
151151
* will crawl those entry points instead.
152152
*
153153
* If neither of these fit your needs, you can specify custom entries using
154-
* this option - the value should be a fast-glob pattern or array of patterns
155-
* (https://github.com/mrmlnc/fast-glob#basic-syntax) that are relative from
154+
* this option - the value should be a tinyglobby pattern or array of patterns
155+
* (https://github.com/SuperchupuDev/tinyglobby) that are relative from
156156
* vite project root. This will overwrite default entries inference.
157157
*/
158158
entries?: string | string[]
@@ -826,7 +826,7 @@ export async function addManuallyIncludedOptimizeDeps(
826826
const includes = [...optimizeDepsInclude]
827827
for (let i = 0; i < includes.length; i++) {
828828
const id = includes[i]
829-
if (glob.isDynamicPattern(id)) {
829+
if (isDynamicPattern(id)) {
830830
const globIds = expandGlobIds(id, environment.getTopLevelConfig())
831831
includes.splice(i, 1, ...globIds)
832832
i += globIds.length - 1

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

+16-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from 'node:path'
2-
import glob from 'fast-glob'
32
import micromatch from 'micromatch'
3+
import { globSync } from 'tinyglobby'
44
import type { ResolvedConfig } from '../config'
55
import { escapeRegex, getNpmPackageName } from '../utils'
66
import { resolvePackageData } from '../packages'
@@ -81,27 +81,24 @@ export function expandGlobIds(id: string, config: ResolvedConfig): string[] {
8181

8282
// "./dist/glob/*-browser/*.js" => "./dist/glob/**/*-browser/**/*.js"
8383
// NOTE: in some cases, this could expand to consecutive /**/*/**/* etc
84-
// but it's fine since fast-glob handles it the same.
84+
// but it's fine since `tinyglobby` handles it the same.
8585
const exportValuePattern = exportsValue.replace(/\*/g, '**/*')
8686
// "./dist/glob/*-browser/*.js" => /dist\/glob\/(.*)-browser\/(.*)\.js/
8787
const exportsValueGlobRe = new RegExp(
8888
exportsValue.split('*').map(escapeRegex).join('(.*)'),
8989
)
9090

9191
possibleExportPaths.push(
92-
...glob
93-
.sync(exportValuePattern, {
94-
cwd: pkgData.dir,
95-
ignore: ['node_modules'],
96-
})
92+
...globSync(exportValuePattern, {
93+
cwd: pkgData.dir,
94+
expandDirectories: false,
95+
ignore: ['node_modules'],
96+
})
9797
.map((filePath) => {
98-
// ensure "./" prefix for inconsistent fast-glob result
99-
// glob.sync("./some-dir/**/*") -> "./some-dir/some-file"
100-
// glob.sync("./**/*") -> "some-dir/some-file"
101-
if (
102-
exportsValue.startsWith('./') &&
103-
!filePath.startsWith('./')
104-
) {
98+
// `tinyglobby` returns paths as they are formatted by the underlying `fdir`.
99+
// Both `globSync("./some-dir/**/*")` and `globSync("./**/*")` result in
100+
// `"some-dir/somefile"` being returned, so we ensure the correct prefix manually.
101+
if (exportsValue.startsWith('./')) {
105102
filePath = './' + filePath
106103
}
107104

@@ -146,9 +143,11 @@ export function expandGlobIds(id: string, config: ResolvedConfig): string[] {
146143
return matched
147144
} else {
148145
// for packages without exports, we can do a simple glob
149-
const matched = glob
150-
.sync(pattern, { cwd: pkgData.dir, ignore: ['node_modules'] })
151-
.map((match) => path.posix.join(pkgName, slash(match)))
146+
const matched = globSync(pattern, {
147+
cwd: pkgData.dir,
148+
expandDirectories: false,
149+
ignore: ['node_modules'],
150+
}).map((match) => path.posix.join(pkgName, slash(match)))
152151
matched.unshift(pkgName)
153152
return matched
154153
}

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import fs from 'node:fs'
22
import fsp from 'node:fs/promises'
33
import path from 'node:path'
44
import { performance } from 'node:perf_hooks'
5-
import glob from 'fast-glob'
65
import type {
76
BuildContext,
87
Loader,
@@ -13,6 +12,7 @@ import type {
1312
import esbuild, { formatMessages, transform } from 'esbuild'
1413
import type { PartialResolvedId } from 'rollup'
1514
import colors from 'picocolors'
15+
import { glob, isDynamicPattern } from 'tinyglobby'
1616
import {
1717
CSS_LANGS_RE,
1818
JS_TYPES_RE,
@@ -332,12 +332,13 @@ function orderedDependencies(deps: Record<string, string>) {
332332

333333
function globEntries(pattern: string | string[], environment: ScanEnvironment) {
334334
const resolvedPatterns = arraify(pattern)
335-
if (resolvedPatterns.every((str) => !glob.isDynamicPattern(str))) {
335+
if (resolvedPatterns.every((str) => !isDynamicPattern(str))) {
336336
return resolvedPatterns.map((p) =>
337337
normalizePath(path.resolve(environment.config.root, p)),
338338
)
339339
}
340340
return glob(pattern, {
341+
absolute: true,
341342
cwd: environment.config.root,
342343
ignore: [
343344
'**/node_modules/**',
@@ -347,8 +348,6 @@ function globEntries(pattern: string | string[], environment: ScanEnvironment) {
347348
? []
348349
: [`**/__tests__/**`, `**/coverage/**`]),
349350
],
350-
absolute: true,
351-
suppressErrors: true, // suppress EACCES errors
352351
})
353352
}
354353

packages/vite/src/node/plugins/css.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import fsp from 'node:fs/promises'
33
import path from 'node:path'
44
import { createRequire } from 'node:module'
55
import { fileURLToPath, pathToFileURL } from 'node:url'
6-
import glob from 'fast-glob'
76
import postcssrc from 'postcss-load-config'
87
import type {
98
ExistingRawSourceMap,
@@ -27,6 +26,7 @@ import type { TransformOptions } from 'esbuild'
2726
import { formatMessages, transform } from 'esbuild'
2827
import type { RawSourceMap } from '@ampproject/remapping'
2928
import { WorkerWithFallback } from 'artichokie'
29+
import { globSync } from 'tinyglobby'
3030
import { getCodeWithSourcemap, injectSourcesContent } from '../server/sourcemap'
3131
import type { EnvironmentModuleNode } from '../server/moduleGraph'
3232
import {
@@ -1405,11 +1405,10 @@ async function compileCSS(
14051405
} else if (message.type === 'dir-dependency') {
14061406
// https://github.com/postcss/postcss/blob/main/docs/guidelines/plugin.md#3-dependencies
14071407
const { dir, glob: globPattern = '**' } = message
1408-
const pattern =
1409-
glob.escapePath(normalizePath(path.resolve(path.dirname(id), dir))) +
1410-
`/` +
1411-
globPattern
1412-
const files = glob.sync(pattern, {
1408+
const files = globSync(globPattern, {
1409+
absolute: true,
1410+
cwd: path.resolve(path.dirname(id), dir),
1411+
expandDirectories: false,
14131412
ignore: ['**/node_modules/**'],
14141413
})
14151414
for (let i = 0; i < files.length; i++) {

packages/vite/src/node/plugins/importMetaGlob.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import type {
1212
} from 'estree'
1313
import type { CustomPluginOptions, RollupAstNode, RollupError } from 'rollup'
1414
import MagicString from 'magic-string'
15-
import fg from 'fast-glob'
1615
import { stringifyQuery } from 'ufo'
1716
import type { GeneralImportGlobOptions } from 'types/importGlob'
1817
import { parseAstAsync } from 'rollup/parseAst'
18+
import { escapePath, glob } from 'tinyglobby'
1919
import type { Plugin } from '../plugin'
2020
import type { EnvironmentModuleNode } from '../server/moduleGraph'
2121
import type { ResolvedConfig } from '../config'
@@ -354,7 +354,7 @@ function findCorrespondingCloseParenthesisPosition(
354354

355355
const importPrefix = '__vite_glob_'
356356

357-
const { basename, dirname, relative, join } = posix
357+
const { basename, dirname, relative } = posix
358358

359359
export interface TransformGlobImportResult {
360360
s: MagicString
@@ -396,13 +396,12 @@ export async function transformGlobImport(
396396
async ({ globsResolved, isRelative, options, index, start, end }) => {
397397
const cwd = getCommonBase(globsResolved) ?? root
398398
const files = (
399-
await fg(globsResolved, {
400-
cwd,
399+
await glob(globsResolved, {
401400
absolute: true,
401+
cwd,
402402
dot: !!options.exhaustive,
403-
ignore: options.exhaustive
404-
? []
405-
: [join(cwd, '**/node_modules/**')],
403+
expandDirectories: false,
404+
ignore: options.exhaustive ? [] : ['**/node_modules/**'],
406405
})
407406
)
408407
.filter((file) => file !== id)
@@ -515,8 +514,7 @@ type IdResolver = (
515514

516515
function globSafePath(path: string) {
517516
// slash path to ensure \ is converted to / as \ could lead to a double escape scenario
518-
// see https://github.com/mrmlnc/fast-glob#advanced-syntax
519-
return fg.escapePath(normalizePath(path))
517+
return escapePath(normalizePath(path))
520518
}
521519

522520
function lastNthChar(str: string, n: number) {

packages/vite/src/node/server/warmup.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'node:fs/promises'
22
import path from 'node:path'
3-
import glob from 'fast-glob'
43
import colors from 'picocolors'
4+
import { glob } from 'tinyglobby'
55
import { FS_PREFIX } from '../constants'
66
import { normalizePath } from '../utils'
77
import type { ViteDevServer } from '../index'
@@ -71,7 +71,8 @@ function fileToUrl(file: string, root: string) {
7171

7272
function mapFiles(files: string[], root: string) {
7373
return glob(files, {
74-
cwd: root,
7574
absolute: true,
75+
cwd: root,
76+
expandDirectories: false,
7677
})
7778
}

packages/vite/src/node/watch.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { EventEmitter } from 'node:events'
22
import path from 'node:path'
3-
import glob from 'fast-glob'
43
import type { FSWatcher, WatchOptions } from 'dep-types/chokidar'
54
import type { OutputOptions } from 'rollup'
65
import colors from 'picocolors'
6+
import { escapePath } from 'tinyglobby'
77
import { withTrailingSlash } from '../shared/utils'
88
import { arraify, normalizePath } from './utils'
99
import type { Logger } from './logger'
@@ -59,12 +59,12 @@ export function resolveChokidarOptions(
5959
'**/.git/**',
6060
'**/node_modules/**',
6161
'**/test-results/**', // Playwright
62-
glob.escapePath(cacheDir) + '/**',
62+
escapePath(cacheDir) + '/**',
6363
...arraify(ignoredList || []),
6464
]
6565
if (emptyOutDir) {
6666
ignored.push(
67-
...[...resolvedOutDirs].map((outDir) => glob.escapePath(outDir) + '/**'),
67+
...[...resolvedOutDirs].map((outDir) => escapePath(outDir) + '/**'),
6868
)
6969
}
7070

playground/backend-integration/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
"devDependencies": {
1313
"sass": "^1.79.5",
1414
"tailwindcss": "^3.4.13",
15-
"fast-glob": "^3.3.2"
15+
"tinyglobby": "^0.2.9"
1616
}
1717
}

playground/backend-integration/vite.config.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'node:path'
2-
import glob from 'fast-glob'
2+
import { globSync } from 'tinyglobby'
33
import { defineConfig, normalizePath } from 'vite'
44

55
/**
@@ -14,9 +14,11 @@ function BackendIntegrationExample() {
1414
const root = path.join(sourceCodeDir, 'entrypoints')
1515
const outDir = path.relative(root, path.join(projectRoot, 'dist/dev'))
1616

17-
const entrypoints = glob
18-
.sync(`${normalizePath(root)}/**/*`, { onlyFiles: true })
19-
.map((filename) => [path.relative(root, filename), filename])
17+
const entrypoints = globSync(`${normalizePath(root)}/**/*`, {
18+
absolute: true,
19+
expandDirectories: false,
20+
onlyFiles: true,
21+
}).map((filename) => [path.relative(root, filename), filename])
2022

2123
entrypoints.push(['tailwindcss-colors', 'tailwindcss/colors.js'])
2224
entrypoints.push(['bar.css', path.resolve(__dirname, './dir/foo.css')])

playground/css/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
"@vitejs/test-css-js-dep": "file:./css-js-dep",
2222
"@vitejs/test-css-proxy-dep": "file:./css-proxy-dep",
2323
"@vitejs/test-scss-proxy-dep": "file:./scss-proxy-dep",
24-
"fast-glob": "^3.3.2",
2524
"less": "^4.2.0",
2625
"postcss-nested": "^6.2.0",
2726
"sass": "^1.79.5",
2827
"stylus": "^0.63.0",
29-
"sugarss": "^4.0.1"
28+
"sugarss": "^4.0.1",
29+
"tinyglobby": "^0.2.9"
3030
},
3131
"imports": {
3232
"#imports": "./imports-field.css"

playground/css/postcss.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import fs from 'node:fs'
22
import path from 'node:path'
3-
import glob from 'fast-glob'
3+
import { globSync } from 'tinyglobby'
44
import { normalizePath } from 'vite'
55
import postcssNested from 'postcss-nested'
66

@@ -19,7 +19,7 @@ function testDirDep() {
1919
const pattern = normalizePath(
2020
path.resolve(path.dirname(result.opts.from), './glob-dep/**/*.css'),
2121
)
22-
const files = glob.sync(pattern)
22+
const files = globSync(pattern, { expandDirectories: false })
2323
const text = files.map((f) => fs.readFileSync(f, 'utf-8')).join('\n')
2424
atRule.parent.insertAfter(atRule, text)
2525
atRule.remove()

0 commit comments

Comments
 (0)