Skip to content

Commit 4cea20d

Browse files
authored
Use built-in glob in Node.js (#18097)
1 parent b536617 commit 4cea20d

11 files changed

Lines changed: 50 additions & 58 deletions

File tree

‎Gulpfile.ts‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import rollupReplace from "@rollup/plugin-replace";
2121
import rollupTerser from "@rollup/plugin-terser";
2222
import rollupDts from "rollup-plugin-dts";
2323
import { Worker as JestWorker } from "jest-worker";
24-
import { Glob } from "glob";
2524
import { patchErrorInfo } from "./packages/babel-parser/scripts/build-error-info.ts";
2625

2726
import rollupBabelSource from "./scripts/rollup-plugin-babel-source.ts";
@@ -41,6 +40,13 @@ const defaultSourcesGlob = [
4140
`${defaultPackagesGlob}/src/**/{*.js,*.cjs,!(*.d).ts,!(*.d).cts,!(*.d).mts}`,
4241
"!./packages/babel-helpers/src/helpers/*",
4342
];
43+
function globDefaultSources() {
44+
return fs
45+
.globSync(defaultSourcesGlob[0], {
46+
exclude: ["./packages/babel-helpers/src/helpers/*"],
47+
})
48+
.map(file => file.replaceAll("\\", "/"));
49+
}
4450

4551
const babelStandalonePluginConfigGlob =
4652
"./packages/babel-standalone/scripts/pluginConfig.json";
@@ -271,13 +277,12 @@ function createWorker(useWorker: boolean): any {
271277

272278
async function buildBabel(useWorker: boolean, ignore: PackageInfo[] = []) {
273279
const worker = await createWorker(useWorker);
274-
const files = new Glob(defaultSourcesGlob, {
275-
ignore: ignore.map(p => `${p.src}/**`),
276-
posix: true,
277-
});
278-
279280
const promises = [];
280-
for await (const file of files) {
281+
for (const file of globDefaultSources()) {
282+
if (ignore.some(pkg => file.startsWith(`${pkg.src}/`))) {
283+
continue;
284+
}
285+
281286
// @example ./packages/babel-parser/src/index.js
282287
const dest = "./" + mapSrcToLib(file);
283288
promises.push(
@@ -997,7 +1002,7 @@ gulp.task("generate-runtime-helpers", async () => {
9971002
gulp.task("generate-standalone", () => generateStandalone());
9981003

9991004
gulp.task("materialize-babel-8-src", () =>
1000-
applyBabelToGlob(new Glob(defaultSourcesGlob, { posix: true }), {
1005+
applyBabelToGlob(globDefaultSources(), {
10011006
plugins: [
10021007
[
10031008
babelPluginToggleBooleanFlag,
@@ -1033,8 +1038,8 @@ gulp.task("materialize-babel-8-src", () =>
10331038

10341039
gulp.task("materialize-babel-8-tests", () =>
10351040
applyBabelToGlob(
1036-
new Glob(`${defaultPackagesGlob}/test/**/{*.js,*.cjs,*.mjs}`, {
1037-
ignore: [
1041+
fs.globSync(`${defaultPackagesGlob}/test/**/{*.js,*.cjs,*.mjs}`, {
1042+
exclude: [
10381043
`**/test/fixtures/**`,
10391044
`**/test/regenerator-fixtures/**`,
10401045
`**/test/tmp/**`,

‎package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
"eslint-plugin-regexp": "^3.0.0",
6363
"eslint-plugin-unicorn": "^56.0.0",
6464
"execa": "^9.0.0",
65-
"glob": "^13.0.0",
6665
"globals": "^17.6.0",
6766
"gulp": "^5.0.1",
6867
"husky": "^9.0.11",

packages/babel-core/test/fixtures/resolution/throw-missing-paths/.gitignore renamed to packages/babel-core/test/fixtures/resolution/throw-missing-paths/.gitkeep

File renamed without changes.

‎packages/babel-helper-check-duplicate-nodes/.gitignore‎

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/babel-helpers/test/unittests/tsRewriteRelativeImportExtensions.skip-bundled.js renamed to packages/babel-helpers/test/unittests/tsRewriteRelativeImportExtensions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import tsRewriteRelativeImportExtensions from "../../lib/helpers/tsRewriteRelativeImportExtensions.js";
1+
import tsRewriteRelativeImportExtensions from "../../src/helpers/tsRewriteRelativeImportExtensions.ts";
22

33
describe("tsRewriteRelativeImportExtensions", () => {
44
it.each([

‎packages/babel-preset-env/.gitignore‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎scripts/cleanup-fixtures.ts‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
1-
import { glob } from "glob";
21
import { repoRoot } from "$repo-utils";
3-
import { existsSync, readFileSync, rmSync, writeFileSync } from "node:fs";
2+
import {
3+
existsSync,
4+
globSync,
5+
readFileSync,
6+
rmSync,
7+
writeFileSync,
8+
} from "node:fs";
49
import path from "node:path";
510

611
const hasCategories = ["babel-parser"];
712

8-
const fixtures = glob
9-
.sync("./@(codemods|packages|eslint)/*/test/fixtures/", {
10-
cwd: repoRoot,
11-
absolute: true,
12-
})
13+
const fixtures = globSync("./@(codemods|packages|eslint)/*/test/fixtures/", {
14+
cwd: repoRoot,
15+
})
16+
.map(fixture => path.join(repoRoot, fixture))
1317
.map(fixture => {
1418
if (
1519
hasCategories.some(name =>
1620
fixture.replace(/\\/g, "/").includes(`packages/${name}/`)
1721
)
1822
) {
19-
return glob.sync("*/", {
23+
return globSync("*/", {
2024
cwd: fixture,
21-
absolute: true,
22-
});
25+
}).map(v => path.join(fixture, v));
2326
}
2427
return fixture;
2528
})
2629
.flat();
27-
2830
for (const fixture of fixtures) {
2931
const optionsPath = path.join(fixture, "options.json");
3032

3133
const fixtureOptions = existsSync(optionsPath)
3234
? JSON.parse(readFileSync(optionsPath, "utf-8"))
3335
: null;
3436

35-
const suites = glob.sync("*/", {
37+
const suites = globSync("*/", {
3638
cwd: fixture,
37-
absolute: true,
38-
});
39+
}).map(suite => path.join(fixture, suite));
3940

4041
for (const suite of suites) {
4142
const optionsPath = path.join(suite, "options.json");
@@ -45,10 +46,9 @@ for (const fixture of fixtures) {
4546
: fixtureOptions;
4647

4748
if (suiteOptions) {
48-
const tests = glob.sync("*/", {
49+
const tests = globSync("*/", {
4950
cwd: suite,
50-
absolute: true,
51-
});
51+
}).map(test => path.join(suite, test));
5252

5353
for (const test of tests) {
5454
const optionsPath = path.join(test, "options.json");

‎scripts/generators/npm-ignore.ts‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import { glob } from "glob";
21
import { repoRoot } from "$repo-utils";
3-
import { existsSync, readFileSync, rmSync, writeFileSync } from "node:fs";
2+
import {
3+
existsSync,
4+
globSync,
5+
readFileSync,
6+
rmSync,
7+
writeFileSync,
8+
} from "node:fs";
49
import path from "node:path";
510

611
const commonIgnore = [
@@ -17,14 +22,11 @@ const extraIgnore: Record<string, string[]> = {
1722
"babel-plugin-proposal-decorators": ["CONTRIB.md"],
1823
};
1924

20-
const packages = glob
21-
.sync("./@(codemods|packages|eslint)/*", {
22-
cwd: repoRoot,
23-
absolute: true,
24-
})
25-
.filter(packageDir => {
26-
return existsSync(path.join(packageDir, "package.json"));
27-
});
25+
const packages = globSync("./@(codemods|packages|eslint)/*", {
26+
cwd: repoRoot,
27+
}).filter(packageDir => {
28+
return existsSync(path.join(packageDir, "package.json"));
29+
});
2830

2931
for (const packageDir of packages) {
3032
const packageJson = JSON.parse(

‎scripts/parallel-tsc/tsc.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import cp from "node:child_process";
1212
import os from "node:os";
1313

1414
import JSON5 from "json5";
15-
import { glob } from "glob";
1615
import { setTimeout } from "node:timers/promises";
16+
import { glob } from "node:fs/promises";
1717

1818
const tscPath = require.resolve("typescript/lib/tsc.js");
1919

@@ -211,8 +211,8 @@ const pool = new Pool({
211211
.map(
212212
file => `${project}/${file.slice(0, -3)}*.d.ts`.replace(/\\/g, "/") // glob doesn't support \ on Windows
213213
);
214-
const files = await glob(globPatterns);
215-
for (const file of files) {
214+
const files = glob(globPatterns);
215+
for await (const file of files) {
216216
const out = joinPath(
217217
process.cwd(),
218218
"dts",

‎scripts/rename-proposal-to-transform.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import path from "node:path";
44
import fs from "node:fs";
5-
import { Glob } from "glob";
65
import { execSync } from "node:child_process";
6+
import { glob } from "node:fs/promises";
77

88
if (process.argv.length < 3) {
99
throw new Error(
@@ -108,7 +108,7 @@ await globRename([
108108
* @param {string | string[]} pattern - The glob pattern to match files.
109109
*/
110110
async function globRename(pattern: string | string[]) {
111-
for await (const filename of new Glob(pattern, {})) {
111+
for await (const filename of glob(pattern, {})) {
112112
const oldContents = fs.readFileSync(filename, "utf8");
113113
const newContents = oldContents.replaceAll(oldName, newName);
114114
if (oldContents !== newContents) {

0 commit comments

Comments
 (0)