Skip to content

Commit

Permalink
Replace globby/fast-glob/tiny-glob with tinyglobby (#13299)
Browse files Browse the repository at this point in the history
Co-authored-by: Emanuele Stoppa <[email protected]>


Co-authored-by: florian-lefebvre <[email protected]>
Co-authored-by: ematipico <[email protected]>
  • Loading branch information
3 people authored Feb 24, 2025
1 parent baf0c40 commit 2e1321e
Show file tree
Hide file tree
Showing 25 changed files with 784 additions and 860 deletions.
8 changes: 8 additions & 0 deletions .changeset/happy-tomatoes-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@astrojs/cloudflare': patch
'@astrojs/netlify': patch
'@astrojs/vercel': patch
'astro': patch
---

Uses `tinyglobby` for globbing files
4 changes: 3 additions & 1 deletion .github/scripts/announce.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { globby as glob } from 'globby';
import { glob } from 'tinyglobby';
import { setOutput } from './utils.mjs';

const { GITHUB_REF = 'main' } = process.env;
Expand Down Expand Up @@ -85,6 +85,8 @@ async function generatePackageMap() {
const packageRoot = new URL('../../packages/', import.meta.url);
const packages = await glob(['*/package.json', '*/*/package.json'], {
cwd: fileURLToPath(packageRoot),
expandDirectories: false,
ignore: ['**/node_modules/**'],
});
await Promise.all(
packages.map(async (pkg) => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@
"esbuild": "^0.24.2",
"eslint": "^9.19.0",
"eslint-plugin-regexp": "^2.7.0",
"globby": "^14.0.2",
"only-allow": "^1.2.1",
"prettier": "^3.4.2",
"prettier-plugin-astro": "^0.14.1",
"publint": "^0.3.2",
"tinyglobby": "^0.2.12",
"turbo": "^2.4.0",
"typescript": "~5.7.3",
"typescript-eslint": "^8.23.0"
Expand Down
6 changes: 3 additions & 3 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@
"es-module-lexer": "^1.6.0",
"esbuild": "^0.24.2",
"estree-walker": "^3.0.3",
"fast-glob": "^3.3.3",
"flattie": "^1.1.1",
"github-slugger": "^2.0.0",
"html-escaper": "3.0.3",
Expand All @@ -152,17 +151,18 @@
"kleur": "^4.1.5",
"magic-string": "^0.30.17",
"magicast": "^0.3.5",
"micromatch": "^4.0.8",
"mrmime": "^2.0.0",
"neotraverse": "^0.6.18",
"p-limit": "^6.2.0",
"p-queue": "^8.1.0",
"picomatch": "^4.0.2",
"preferred-pm": "^4.1.1",
"prompts": "^2.4.2",
"rehype": "^13.0.2",
"semver": "^7.7.1",
"shiki": "^1.29.2",
"tinyexec": "^0.3.2",
"tinyglobby": "^0.2.12",
"tsconfck": "^3.1.4",
"ultrahtml": "^1.5.3",
"unist-util-visit": "^5.0.0",
Expand Down Expand Up @@ -194,7 +194,7 @@
"@types/html-escaper": "3.0.4",
"@types/http-cache-semantics": "^4.0.4",
"@types/js-yaml": "^4.0.9",
"@types/micromatch": "^4.0.9",
"@types/picomatch": "^3.0.2",
"@types/prompts": "^2.4.9",
"@types/semver": "^7.5.8",
"@types/yargs-parser": "^21.0.3",
Expand Down
9 changes: 5 additions & 4 deletions packages/astro/src/content/loaders/glob.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { promises as fs, existsSync } from 'node:fs';
import { relative } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import fastGlob from 'fast-glob';
import { bold, green } from 'kleur/colors';
import micromatch from 'micromatch';
import pLimit from 'p-limit';
import picomatch from 'picomatch';
import { glob as tinyglobby } from 'tinyglobby';
import type { ContentEntryRenderFunction, ContentEntryType } from '../../types/public/content.js';
import type { RenderedContent } from '../data-store.js';
import { getContentEntryIdAndSlug, posixRelative } from '../utils.js';
Expand Down Expand Up @@ -236,8 +236,9 @@ export function glob(globOptions: GlobOptions): Loader {
logger.warn(`The base directory "${fileURLToPath(baseDir)}" does not exist.`);
}

const files = await fastGlob(globOptions.pattern, {
const files = await tinyglobby(globOptions.pattern, {
cwd: fileURLToPath(baseDir),
expandDirectories: false,
});

if (exists && files.length === 0) {
Expand Down Expand Up @@ -321,7 +322,7 @@ export function glob(globOptions: GlobOptions): Loader {
watcher.add(filePath);

const matchesGlob = (entry: string) =>
!entry.startsWith('../') && micromatch.isMatch(entry, globOptions.pattern);
!entry.startsWith('../') && picomatch.isMatch(entry, globOptions.pattern);

const basePath = fileURLToPath(baseDir);

Expand Down
17 changes: 6 additions & 11 deletions packages/astro/src/content/types-generator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type fsMod from 'node:fs';
import * as path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import glob from 'fast-glob';
import { bold, cyan } from 'kleur/colors';
import { glob } from 'tinyglobby';
import { type ViteDevServer, normalizePath } from 'vite';
import { type ZodSchema, z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';
Expand Down Expand Up @@ -94,21 +94,16 @@ export async function createContentTypesGenerator({
}
const globResult = await glob('**', {
cwd: fileURLToPath(contentPaths.contentDir),
fs: {
readdir: fs.readdir.bind(fs),
readdirSync: fs.readdirSync.bind(fs),
},
onlyFiles: false,
objectMode: true,
absolute: true,
});

for (const entry of globResult) {
const fullPath = path.join(fileURLToPath(contentPaths.contentDir), entry.path);
for (const fullPath of globResult) {
const entryURL = pathToFileURL(fullPath);
if (entryURL.href.startsWith(contentPaths.config.url.href)) continue;
if (entry.dirent.isFile()) {
const stat = fs.statSync(fullPath);
if (stat.isFile()) {
events.push({ name: 'add', entry: entryURL });
} else if (entry.dirent.isDirectory()) {
} else if (stat.isDirectory()) {
events.push({ name: 'addDir', entry: entryURL });
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/content/vite-plugin-content-virtual-mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import nodeFs from 'node:fs';
import { extname } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { dataToEsm } from '@rollup/pluginutils';
import glob from 'fast-glob';
import pLimit from 'p-limit';
import { glob } from 'tinyglobby';
import type { Plugin, ViteDevServer } from 'vite';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import { rootRelativePath } from '../core/viteUtils.js';
Expand Down Expand Up @@ -280,7 +280,7 @@ export async function generateLookupMap({
{
absolute: true,
cwd: fileURLToPath(root),
fs,
expandDirectories: false,
},
);

Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/plugins/plugin-manifest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fileURLToPath } from 'node:url';
import glob from 'fast-glob';
import type { OutputChunk } from 'rollup';
import { glob } from 'tinyglobby';
import type { Plugin as VitePlugin } from 'vite';
import { getAssetsPrefix } from '../../../assets/utils/getAssetsPrefix.js';
import { normalizeTheLocale } from '../../../i18n/index.js';
Expand Down
27 changes: 4 additions & 23 deletions packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { teardown } from '@astrojs/compiler';
import glob from 'fast-glob';
import { bgGreen, black, green } from 'kleur/colors';
import { glob } from 'tinyglobby';
import * as vite from 'vite';
import { type BuildInternals, createBuildInternals } from '../../core/build/internal.js';
import { emptyDir, removeEmptyDirs } from '../../core/fs/index.js';
Expand Down Expand Up @@ -247,8 +247,8 @@ async function clientBuild(
// Nothing to do if there is no client-side JS.
if (!input.size) {
// If SSR, copy public over
if (ssr) {
await copyFiles(settings.config.publicDir, out, true);
if (ssr && fs.existsSync(settings.config.publicDir)) {
await fs.promises.cp(settings.config.publicDir, out, { recursive: true, force: true });
}

return null;
Expand Down Expand Up @@ -384,31 +384,12 @@ async function cleanServerOutput(
.map((fileName) => fs.promises.rm(new URL(fileName, out))),
);
// Copy assets before cleaning directory if outside root
await copyFiles(out, opts.settings.config.outDir, true);
await fs.promises.cp(out, opts.settings.config.outDir, { recursive: true, force: true });
await fs.promises.rm(out, { recursive: true });
return;
}
}

export async function copyFiles(fromFolder: URL, toFolder: URL, includeDotfiles = false) {
const files = await glob('**/*', {
cwd: fileURLToPath(fromFolder),
dot: includeDotfiles,
});
if (files.length === 0) return;
return await Promise.all(
files.map(async function copyFile(filename) {
const from = new URL(filename, fromFolder);
const to = new URL(filename, toFolder);
const lastFolder = new URL('./', to);
return fs.promises.mkdir(lastFolder, { recursive: true }).then(async function fsCopyFile() {
const p = await fs.promises.copyFile(from, to, fs.constants.COPYFILE_FICLONE);
return p;
});
}),
);
}

async function ssrMoveAssets(opts: StaticBuildOptions) {
opts.logger.info('build', 'Rearranging server assets...');
const serverRoot =
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/create-vite.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import nodeFs from 'node:fs';
import { fileURLToPath } from 'node:url';
import glob from 'fast-glob';
import { convertPathToPattern } from 'tinyglobby';
import * as vite from 'vite';
import { crawlFrameworkPkgs } from 'vitefu';
import { vitePluginActions, vitePluginUserActions } from '../actions/plugins.js';
Expand Down Expand Up @@ -124,7 +124,7 @@ export async function createVite(
},
});

const srcDirPattern = glob.convertPathToPattern(fileURLToPath(settings.config.srcDir));
const srcDirPattern = convertPathToPattern(fileURLToPath(settings.config.srcDir));
const envLoader = createEnvLoader(mode, settings.config);

// Start with the Vite configuration that Astro core needs
Expand Down
5 changes: 3 additions & 2 deletions packages/astro/test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { stripVTControlCharacters } from 'node:util';
import { execa } from 'execa';
import fastGlob from 'fast-glob';
import { glob } from 'tinyglobby';
import { Agent } from 'undici';
import { check } from '../dist/cli/check/index.js';
import { globalContentLayer } from '../dist/content/content-layer.js';
Expand Down Expand Up @@ -252,8 +252,9 @@ export async function loadFixture(inlineConfig) {
),
readdir: (fp) => fs.promises.readdir(new URL(fp.replace(/^\//, ''), config.outDir)),
glob: (p) =>
fastGlob(p, {
glob(p, {
cwd: fileURLToPath(config.outDir),
expandDirectories: false,
}),
clean: async () => {
await fs.promises.rm(config.outDir, {
Expand Down
3 changes: 1 addition & 2 deletions packages/integrations/cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"estree-walker": "^3.0.3",
"magic-string": "^0.30.17",
"miniflare": "^3.20241230.1",
"tiny-glob": "^0.2.9",
"tinyglobby": "^0.2.12",
"vite": "^6.0.7",
"wrangler": "^3.101.0"
},
Expand All @@ -52,7 +52,6 @@
"astro-scripts": "workspace:*",
"cheerio": "1.0.0",
"execa": "^8.0.1",
"fast-glob": "^3.3.3",
"rollup": "^4.30.1",
"strip-ansi": "^7.1.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
removeLeadingForwardSlash,
removeTrailingForwardSlash,
} from '@astrojs/internal-helpers/path';
import glob from 'tiny-glob';
import { glob } from 'tinyglobby';

// Copied from https://github.com/withastro/astro/blob/3776ecf0aa9e08a992d3ae76e90682fd04093721/packages/astro/src/core/routing/manifest/create.ts#L45-L70
// We're not sure how to improve this regex yet
Expand Down Expand Up @@ -205,9 +205,8 @@ export async function createRoutesFile(
}

if (existsSync(fileURLToPath(_config.publicDir))) {
const staticFiles = await glob(`${fileURLToPath(_config.publicDir)}/**/*`, {
const staticFiles = await glob(`**/*`, {
cwd: fileURLToPath(_config.publicDir),
filesOnly: true,
dot: true,
});
for (const staticFile of staticFiles) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { describe, it } from 'node:test';
import { fileURLToPath } from 'node:url';
import glob from 'tiny-glob';
import { glob } from 'tinyglobby';
import { astroCli } from './_test-utils.js';

const root = new URL('./fixtures/external-image-service/', import.meta.url);
Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/netlify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@netlify/functions": "^2.8.0",
"@vercel/nft": "^0.29.0",
"esbuild": "^0.24.0",
"tinyglobby": "^0.2.12",
"vite": "^6.0.7"
},
"peerDependencies": {
Expand All @@ -56,7 +57,6 @@
"cheerio": "1.0.0",
"devalue": "^5.1.1",
"execa": "^8.0.1",
"fast-glob": "^3.3.3",
"strip-ansi": "^7.1.0",
"typescript": "^5.7.3"
},
Expand Down
5 changes: 3 additions & 2 deletions packages/integrations/netlify/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
IntegrationResolvedRoute,
} from 'astro';
import { build } from 'esbuild';
import glob from 'fast-glob';
import { glob, globSync } from 'tinyglobby';
import { copyDependenciesToFunction } from './lib/nft.js';
import type { Args } from './ssr-function.js';

Expand Down Expand Up @@ -283,6 +283,7 @@ export default function netlifyIntegration(
cwd: fileURLToPath(rootDir),
absolute: true,
ignore: exclude,
expandDirectories: false,
});
return files.map((file) => pathToFileURL(file));
}
Expand All @@ -306,7 +307,7 @@ export default function netlifyIntegration(
if (_config.vite.assetsInclude) {
const mergeGlobbedIncludes = (globPattern: unknown) => {
if (typeof globPattern === 'string') {
const entries = glob.sync(globPattern).map((p) => pathToFileURL(p));
const entries = globSync(globPattern).map((p) => pathToFileURL(p));
extraFilesToInclude.push(...entries);
} else if (Array.isArray(globPattern)) {
for (const pattern of globPattern) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { existsSync } from 'node:fs';
import { after, before, describe, it } from 'node:test';
import netlify from '@astrojs/netlify';
import * as cheerio from 'cheerio';
import glob from 'fast-glob';
import { globSync } from 'tinyglobby';
import { loadFixture } from '../../../../astro/test/test-utils.js';

describe(
Expand Down Expand Up @@ -32,7 +32,7 @@ describe(

it('Emits vite assets files', async () => {
for (const pattern of expectedAssetsInclude) {
const files = glob.sync(pattern);
const files = globSync(pattern);
for (const file of files) {
assert.ok(
existsSync(new URL(file, expectedCwd)),
Expand Down Expand Up @@ -158,7 +158,7 @@ describe(

it('Does not include files when excluded', async () => {
for (const pattern of includeFiles) {
const files = glob.sync(pattern, { ignore: excludedTxt });
const files = globSync(pattern, { ignore: excludedTxt });
for (const file of files) {
assert.ok(
existsSync(new URL(file, expectedCwd)),
Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/vercel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"@vercel/nft": "^0.29.0",
"@vercel/routing-utils": "^5.0.4",
"esbuild": "^0.24.0",
"fast-glob": "^3.3.3"
"tinyglobby": "^0.2.12"
},
"peerDependencies": {
"astro": "^5.0.0"
Expand Down
Loading

0 comments on commit 2e1321e

Please sign in to comment.