Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use tinyglobby and picomatch #12165

Draft
wants to merge 5 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gorgeous-pants-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Refactors to use `tinyglobby` and `picomatch` for internal globbing operations
4 changes: 3 additions & 1 deletion .github/scripts/announce.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { globby as glob } from 'globby';
import { glob } from 'tinyglobby';
import { fileURLToPath } from 'node:url';
import { readFile } from 'node:fs/promises';
import { setOutput } from './utils.mjs';
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 @@ -61,10 +61,10 @@
"esbuild": "^0.21.5",
"eslint": "^9.14.0",
"eslint-plugin-regexp": "^2.6.0",
"globby": "^14.0.2",
"only-allow": "^1.2.1",
"prettier": "^3.3.3",
"prettier-plugin-astro": "^0.14.1",
"tinyglobby": "^0.2.10",
"turbo": "^2.2.3",
"typescript": "~5.6.3",
"typescript-eslint": "^8.13.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 @@ -142,7 +142,6 @@
"es-module-lexer": "^1.5.4",
"esbuild": "^0.21.5",
"estree-walker": "^3.0.3",
"fast-glob": "^3.3.2",
"flattie": "^1.1.1",
"github-slugger": "^2.0.0",
"html-escaper": "^3.0.3",
Expand All @@ -151,17 +150,18 @@
"kleur": "^4.1.5",
"magic-string": "^0.30.12",
"magicast": "^0.3.5",
"micromatch": "^4.0.8",
"mrmime": "^2.0.0",
"neotraverse": "^0.6.18",
"p-limit": "^6.1.0",
"p-queue": "^8.0.1",
"picomatch": "^4.0.2",
"preferred-pm": "^4.0.0",
"prompts": "^2.4.2",
"rehype": "^13.0.2",
"semver": "^7.6.3",
"shiki": "^1.22.2",
"tinyexec": "^0.3.1",
"tinyglobby": "^0.2.10",
"tsconfck": "^3.1.4",
"unist-util-visit": "^5.0.0",
"vfile": "^6.0.3",
Expand Down Expand Up @@ -191,7 +191,7 @@
"@types/html-escaper": "^3.0.2",
"@types/http-cache-semantics": "^4.0.4",
"@types/js-yaml": "^4.0.9",
"@types/micromatch": "^4.0.9",
"@types/picomatch": "^3.0.1",
"@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,9 +1,9 @@
import { promises as fs } from 'node:fs';
import { fileURLToPath, pathToFileURL } from 'node:url';
import fastGlob from 'fast-glob';
import { bold, green } from 'kleur/colors';
import micromatch from 'micromatch';
import picomatch from 'picomatch';
import pLimit from 'p-limit';
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 @@ -215,7 +215,8 @@ export function glob(globOptions: GlobOptions): Loader {
baseDir.pathname = `${baseDir.pathname}/`;
}

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

Expand Down Expand Up @@ -291,7 +292,7 @@ export function glob(globOptions: GlobOptions): Loader {
}

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

const basePath = fileURLToPath(baseDir);

Expand Down
16 changes: 6 additions & 10 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,17 @@ 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 } from 'vite';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import { rootRelativePath } from '../core/viteUtils.js';
Expand Down Expand Up @@ -273,8 +273,8 @@ export async function generateLookupMap({
`${relContentDir}**/*${getExtGlob([...dataEntryExts, ...contentEntryExts])}`,
{
absolute: true,
expandDirectories: false,
cwd: fileURLToPath(root),
fs,
},
);

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 @@ -249,8 +249,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 @@ -387,31 +387,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 @@ -122,7 +122,7 @@ export async function createVite(
},
});

const srcDirPattern = glob.convertPathToPattern(fileURLToPath(settings.config.srcDir));
const srcDirPattern = convertPathToPattern(fileURLToPath(settings.config.srcDir));

// Start with the Vite configuration that Astro core needs
const commonConfig: vite.InlineConfig = {
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 @@ -250,8 +250,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),
expanDirectories: false,
}),
clean: async () => {
await fs.promises.rm(config.outDir, {
Expand Down
Loading
Loading