chore(deps)!: migrate fast-glob to tinyglobby (#18243)

This commit is contained in:
pralkarz 2024-10-17 07:24:33 +02:00 committed by GitHub
parent b5d1a058f9
commit 6f74a3a1b2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 121 additions and 67 deletions

View File

@ -8,7 +8,7 @@
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.
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.
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.
## optimizeDeps.exclude

View File

@ -184,7 +184,7 @@ The error that appears in the Browser when the fallback happens can be ignored.
Warm up files to transform and cache the results in advance. This improves the initial page load during server starts and prevents transform waterfalls.
`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`.
`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`.
Make sure to only add files that are frequently used to not overload the Vite dev server on startup.

View File

@ -542,7 +542,7 @@ Note that:
- This is a Vite-only feature and is not a web or ES standard.
- 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)).
- 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).
- The glob matching is done via [`tinyglobby`](https://github.com/SuperchupuDev/tinyglobby).
- 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.
## Dynamic Import

View File

@ -2405,6 +2405,35 @@ Repository: git+https://github.com/antfu/strip-literal.git
---------------------------------------
## tinyglobby
License: MIT
By: Superchupu
Repository: git+https://github.com/SuperchupuDev/tinyglobby.git
> MIT License
>
> Copyright (c) 2024 Madeline Gurriarán
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
> in the Software without restriction, including without limitation the rights
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in all
> copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
> SOFTWARE.
---------------------------------------
## to-regex-range
License: MIT
By: Jon Schlinkert, Rouven Weßling

View File

@ -120,7 +120,6 @@
"escape-html": "^1.0.3",
"estree-walker": "^3.0.3",
"etag": "^1.8.1",
"fast-glob": "^3.3.2",
"http-proxy": "^1.18.1",
"launch-editor-middleware": "^2.9.1",
"lightningcss": "^1.27.0",
@ -148,6 +147,7 @@
"source-map-support": "^0.5.21",
"strip-ansi": "^7.1.0",
"strip-literal": "^2.1.0",
"tinyglobby": "^0.2.9",
"tsconfck": "^3.1.4",
"tslib": "^2.7.0",
"types": "link:./types",

View File

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

View File

@ -1,6 +1,6 @@
import path from 'node:path'
import glob from 'fast-glob'
import micromatch from 'micromatch'
import { globSync } from 'tinyglobby'
import type { ResolvedConfig } from '../config'
import { escapeRegex, getNpmPackageName } from '../utils'
import { resolvePackageData } from '../packages'
@ -81,7 +81,7 @@ export function expandGlobIds(id: string, config: ResolvedConfig): string[] {
// "./dist/glob/*-browser/*.js" => "./dist/glob/**/*-browser/**/*.js"
// NOTE: in some cases, this could expand to consecutive /**/*/**/* etc
// but it's fine since fast-glob handles it the same.
// but it's fine since `tinyglobby` handles it the same.
const exportValuePattern = exportsValue.replace(/\*/g, '**/*')
// "./dist/glob/*-browser/*.js" => /dist\/glob\/(.*)-browser\/(.*)\.js/
const exportsValueGlobRe = new RegExp(
@ -89,19 +89,16 @@ export function expandGlobIds(id: string, config: ResolvedConfig): string[] {
)
possibleExportPaths.push(
...glob
.sync(exportValuePattern, {
cwd: pkgData.dir,
ignore: ['node_modules'],
})
...globSync(exportValuePattern, {
cwd: pkgData.dir,
expandDirectories: false,
ignore: ['node_modules'],
})
.map((filePath) => {
// ensure "./" prefix for inconsistent fast-glob result
// glob.sync("./some-dir/**/*") -> "./some-dir/some-file"
// glob.sync("./**/*") -> "some-dir/some-file"
if (
exportsValue.startsWith('./') &&
!filePath.startsWith('./')
) {
// `tinyglobby` returns paths as they are formatted by the underlying `fdir`.
// Both `globSync("./some-dir/**/*")` and `globSync("./**/*")` result in
// `"some-dir/somefile"` being returned, so we ensure the correct prefix manually.
if (exportsValue.startsWith('./')) {
filePath = './' + filePath
}
@ -146,9 +143,11 @@ export function expandGlobIds(id: string, config: ResolvedConfig): string[] {
return matched
} else {
// for packages without exports, we can do a simple glob
const matched = glob
.sync(pattern, { cwd: pkgData.dir, ignore: ['node_modules'] })
.map((match) => path.posix.join(pkgName, slash(match)))
const matched = globSync(pattern, {
cwd: pkgData.dir,
expandDirectories: false,
ignore: ['node_modules'],
}).map((match) => path.posix.join(pkgName, slash(match)))
matched.unshift(pkgName)
return matched
}

View File

@ -2,7 +2,6 @@ import fs from 'node:fs'
import fsp from 'node:fs/promises'
import path from 'node:path'
import { performance } from 'node:perf_hooks'
import glob from 'fast-glob'
import type {
BuildContext,
Loader,
@ -13,6 +12,7 @@ import type {
import esbuild, { formatMessages, transform } from 'esbuild'
import type { PartialResolvedId } from 'rollup'
import colors from 'picocolors'
import { glob, isDynamicPattern } from 'tinyglobby'
import {
CSS_LANGS_RE,
JS_TYPES_RE,
@ -332,12 +332,13 @@ function orderedDependencies(deps: Record<string, string>) {
function globEntries(pattern: string | string[], environment: ScanEnvironment) {
const resolvedPatterns = arraify(pattern)
if (resolvedPatterns.every((str) => !glob.isDynamicPattern(str))) {
if (resolvedPatterns.every((str) => !isDynamicPattern(str))) {
return resolvedPatterns.map((p) =>
normalizePath(path.resolve(environment.config.root, p)),
)
}
return glob(pattern, {
absolute: true,
cwd: environment.config.root,
ignore: [
'**/node_modules/**',
@ -347,8 +348,6 @@ function globEntries(pattern: string | string[], environment: ScanEnvironment) {
? []
: [`**/__tests__/**`, `**/coverage/**`]),
],
absolute: true,
suppressErrors: true, // suppress EACCES errors
})
}

View File

@ -3,7 +3,6 @@ import fsp from 'node:fs/promises'
import path from 'node:path'
import { createRequire } from 'node:module'
import { fileURLToPath, pathToFileURL } from 'node:url'
import glob from 'fast-glob'
import postcssrc from 'postcss-load-config'
import type {
ExistingRawSourceMap,
@ -27,6 +26,7 @@ import type { TransformOptions } from 'esbuild'
import { formatMessages, transform } from 'esbuild'
import type { RawSourceMap } from '@ampproject/remapping'
import { WorkerWithFallback } from 'artichokie'
import { globSync } from 'tinyglobby'
import { getCodeWithSourcemap, injectSourcesContent } from '../server/sourcemap'
import type { EnvironmentModuleNode } from '../server/moduleGraph'
import {
@ -1405,11 +1405,10 @@ async function compileCSS(
} else if (message.type === 'dir-dependency') {
// https://github.com/postcss/postcss/blob/main/docs/guidelines/plugin.md#3-dependencies
const { dir, glob: globPattern = '**' } = message
const pattern =
glob.escapePath(normalizePath(path.resolve(path.dirname(id), dir))) +
`/` +
globPattern
const files = glob.sync(pattern, {
const files = globSync(globPattern, {
absolute: true,
cwd: path.resolve(path.dirname(id), dir),
expandDirectories: false,
ignore: ['**/node_modules/**'],
})
for (let i = 0; i < files.length; i++) {

View File

@ -12,10 +12,10 @@ import type {
} from 'estree'
import type { CustomPluginOptions, RollupAstNode, RollupError } from 'rollup'
import MagicString from 'magic-string'
import fg from 'fast-glob'
import { stringifyQuery } from 'ufo'
import type { GeneralImportGlobOptions } from 'types/importGlob'
import { parseAstAsync } from 'rollup/parseAst'
import { escapePath, glob } from 'tinyglobby'
import type { Plugin } from '../plugin'
import type { EnvironmentModuleNode } from '../server/moduleGraph'
import type { ResolvedConfig } from '../config'
@ -354,7 +354,7 @@ function findCorrespondingCloseParenthesisPosition(
const importPrefix = '__vite_glob_'
const { basename, dirname, relative, join } = posix
const { basename, dirname, relative } = posix
export interface TransformGlobImportResult {
s: MagicString
@ -396,13 +396,12 @@ export async function transformGlobImport(
async ({ globsResolved, isRelative, options, index, start, end }) => {
const cwd = getCommonBase(globsResolved) ?? root
const files = (
await fg(globsResolved, {
cwd,
await glob(globsResolved, {
absolute: true,
cwd,
dot: !!options.exhaustive,
ignore: options.exhaustive
? []
: [join(cwd, '**/node_modules/**')],
expandDirectories: false,
ignore: options.exhaustive ? [] : ['**/node_modules/**'],
})
)
.filter((file) => file !== id)
@ -515,8 +514,7 @@ type IdResolver = (
function globSafePath(path: string) {
// slash path to ensure \ is converted to / as \ could lead to a double escape scenario
// see https://github.com/mrmlnc/fast-glob#advanced-syntax
return fg.escapePath(normalizePath(path))
return escapePath(normalizePath(path))
}
function lastNthChar(str: string, n: number) {

View File

@ -1,7 +1,7 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import glob from 'fast-glob'
import colors from 'picocolors'
import { glob } from 'tinyglobby'
import { FS_PREFIX } from '../constants'
import { normalizePath } from '../utils'
import type { ViteDevServer } from '../index'
@ -71,7 +71,8 @@ function fileToUrl(file: string, root: string) {
function mapFiles(files: string[], root: string) {
return glob(files, {
cwd: root,
absolute: true,
cwd: root,
expandDirectories: false,
})
}

View File

@ -1,9 +1,9 @@
import { EventEmitter } from 'node:events'
import path from 'node:path'
import glob from 'fast-glob'
import type { FSWatcher, WatchOptions } from 'dep-types/chokidar'
import type { OutputOptions } from 'rollup'
import colors from 'picocolors'
import { escapePath } from 'tinyglobby'
import { withTrailingSlash } from '../shared/utils'
import { arraify, normalizePath } from './utils'
import type { Logger } from './logger'
@ -59,12 +59,12 @@ export function resolveChokidarOptions(
'**/.git/**',
'**/node_modules/**',
'**/test-results/**', // Playwright
glob.escapePath(cacheDir) + '/**',
escapePath(cacheDir) + '/**',
...arraify(ignoredList || []),
]
if (emptyOutDir) {
ignored.push(
...[...resolvedOutDirs].map((outDir) => glob.escapePath(outDir) + '/**'),
...[...resolvedOutDirs].map((outDir) => escapePath(outDir) + '/**'),
)
}

View File

@ -12,6 +12,6 @@
"devDependencies": {
"sass": "^1.79.5",
"tailwindcss": "^3.4.13",
"fast-glob": "^3.3.2"
"tinyglobby": "^0.2.9"
}
}

View File

@ -1,5 +1,5 @@
import path from 'node:path'
import glob from 'fast-glob'
import { globSync } from 'tinyglobby'
import { defineConfig, normalizePath } from 'vite'
/**
@ -14,9 +14,11 @@ function BackendIntegrationExample() {
const root = path.join(sourceCodeDir, 'entrypoints')
const outDir = path.relative(root, path.join(projectRoot, 'dist/dev'))
const entrypoints = glob
.sync(`${normalizePath(root)}/**/*`, { onlyFiles: true })
.map((filename) => [path.relative(root, filename), filename])
const entrypoints = globSync(`${normalizePath(root)}/**/*`, {
absolute: true,
expandDirectories: false,
onlyFiles: true,
}).map((filename) => [path.relative(root, filename), filename])
entrypoints.push(['tailwindcss-colors', 'tailwindcss/colors.js'])
entrypoints.push(['bar.css', path.resolve(__dirname, './dir/foo.css')])

View File

@ -21,12 +21,12 @@
"@vitejs/test-css-js-dep": "file:./css-js-dep",
"@vitejs/test-css-proxy-dep": "file:./css-proxy-dep",
"@vitejs/test-scss-proxy-dep": "file:./scss-proxy-dep",
"fast-glob": "^3.3.2",
"less": "^4.2.0",
"postcss-nested": "^6.2.0",
"sass": "^1.79.5",
"stylus": "^0.63.0",
"sugarss": "^4.0.1"
"sugarss": "^4.0.1",
"tinyglobby": "^0.2.9"
},
"imports": {
"#imports": "./imports-field.css"

View File

@ -1,6 +1,6 @@
import fs from 'node:fs'
import path from 'node:path'
import glob from 'fast-glob'
import { globSync } from 'tinyglobby'
import { normalizePath } from 'vite'
import postcssNested from 'postcss-nested'
@ -19,7 +19,7 @@ function testDirDep() {
const pattern = normalizePath(
path.resolve(path.dirname(result.opts.from), './glob-dep/**/*.css'),
)
const files = glob.sync(pattern)
const files = globSync(pattern, { expandDirectories: false })
const text = files.map((f) => fs.readFileSync(f, 'utf-8')).join('\n')
atRule.parent.insertAfter(atRule, text)
atRule.remove()

View File

@ -322,9 +322,6 @@ importers:
etag:
specifier: ^1.8.1
version: 1.8.1
fast-glob:
specifier: ^3.3.2
version: 3.3.2
http-proxy:
specifier: ^1.18.1
version: 1.18.1(patch_hash=qqiqxx62zlcu62nljjmhlvexni)(debug@4.3.7)
@ -406,6 +403,9 @@ importers:
strip-literal:
specifier: ^2.1.0
version: 2.1.0
tinyglobby:
specifier: ^0.2.9
version: 0.2.9
tsconfck:
specifier: ^3.1.4
version: 3.1.4(typescript@5.5.3)
@ -510,15 +510,15 @@ importers:
playground/backend-integration:
devDependencies:
fast-glob:
specifier: ^3.3.2
version: 3.3.2
sass:
specifier: ^1.79.5
version: 1.79.5
tailwindcss:
specifier: ^3.4.13
version: 3.4.13(ts-node@10.9.2(@types/node@20.16.11)(typescript@5.5.3))
tinyglobby:
specifier: ^0.2.9
version: 0.2.9
playground/build-old: {}
@ -566,9 +566,6 @@ importers:
'@vitejs/test-scss-proxy-dep':
specifier: file:./scss-proxy-dep
version: file:playground/css/scss-proxy-dep
fast-glob:
specifier: ^3.3.2
version: 3.3.2
less:
specifier: ^4.2.0
version: 4.2.0
@ -584,6 +581,9 @@ importers:
sugarss:
specifier: ^4.0.1
version: 4.0.1(postcss@8.4.47)
tinyglobby:
specifier: ^0.2.9
version: 0.2.9
playground/css-codesplit: {}
@ -4829,6 +4829,14 @@ packages:
picomatch:
optional: true
fdir@6.4.0:
resolution: {integrity: sha512-3oB133prH1o4j/L5lLW7uOCF1PlD+/It2L0eL/iAqWMB91RBbqTewABqxhj0ibBd90EEmWZq7ntIWzVaWcXTGQ==}
peerDependencies:
picomatch: ^3 || ^4
peerDependenciesMeta:
picomatch:
optional: true
feed@4.2.2:
resolution: {integrity: sha512-u5/sxGfiMfZNtJ3OvQpXcvotFpYkL0n9u9mM2vkui2nGo8b4wvDkJ8gAkYqbA8QpGyFCv3RK0Z+Iv+9veCS9bQ==}
engines: {node: '>=0.4.0'}
@ -5984,6 +5992,10 @@ packages:
resolution: {integrity: sha512-JU3teHTNjmE2VCGFzuY8EXzCDVwEqB2a8fsIvwaStHhAWJEeVd1o1QD80CU6+ZdEXXSLbSsuLwJjkCBWqRQUVA==}
engines: {node: '>=8.6'}
picomatch@4.0.2:
resolution: {integrity: sha512-M7BAV6Rlcy5u+m6oPhAPFgJTzAioX/6B0DxyvDlo9l8+T3nLKbrczg2WLUyzd45L8RqfUMyGPzekbMvX2Ldkwg==}
engines: {node: '>=12'}
pidtree@0.6.0:
resolution: {integrity: sha512-eG2dWTVw5bzqGRztnHExczNxt5VGsE6OwTeCG3fdUf9KBsZzO3R5OIIIzWR+iZA0NtZ+RDVdaoE2dK1cn6jH4g==}
engines: {node: '>=0.10'}
@ -6801,6 +6813,10 @@ packages:
tinyexec@0.3.0:
resolution: {integrity: sha512-tVGE0mVJPGb0chKhqmsoosjsS+qUnJVGJpZgsHYQcGoPlG3B51R3PouqTgEGH2Dc9jjFyOqOpix6ZHNMXp1FZg==}
tinyglobby@0.2.9:
resolution: {integrity: sha512-8or1+BGEdk1Zkkw2ii16qSS7uVrQJPre5A9o/XkWPATkk23FZh/15BKFxPnlTy6vkljZxLqYCzzBMj30ZrSvjw==}
engines: {node: '>=12.0.0'}
tinypool@1.0.0:
resolution: {integrity: sha512-KIKExllK7jp3uvrNtvRBYBWBOAXSX8ZvoaD8T+7KB/QHIuoJW3Pmr60zucywjAlMb5TeXUkcs/MWeWLu0qvuAQ==}
engines: {node: ^18.0.0 || >=20.0.0}
@ -10546,6 +10562,10 @@ snapshots:
optionalDependencies:
picomatch: 2.3.1
fdir@6.4.0(picomatch@4.0.2):
optionalDependencies:
picomatch: 4.0.2
feed@4.2.2:
dependencies:
xml-js: 1.6.11
@ -11806,6 +11826,8 @@ snapshots:
picomatch@2.3.1: {}
picomatch@4.0.2: {}
pidtree@0.6.0: {}
pify@2.3.0: {}
@ -12681,6 +12703,11 @@ snapshots:
tinyexec@0.3.0: {}
tinyglobby@0.2.9:
dependencies:
fdir: 6.4.0(picomatch@4.0.2)
picomatch: 4.0.2
tinypool@1.0.0: {}
tinyrainbow@1.2.0: {}