Visitar URL original
perf(core): check files before interacting with them (#13090) · babel/babel@d0fcbfc · GitHub
Skip to content

Commit d0fcbfc

Browse files
authored
perf(core): check files before interacting with them (#13090)
* perf(core): check files before interacting with them * refactor: inline fs.exists.sync -> nodeFs.existsSync
1 parent 4753768 commit d0fcbfc

5 files changed

Lines changed: 13 additions & 26 deletions

File tree

‎packages/babel-core/src/config/files/configuration.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import buildDebug from "debug";
2+
import nodeFs from "fs";
23
import path from "path";
34
import json5 from "json5";
45
import gensync from "gensync";
@@ -36,11 +37,11 @@ const RELATIVE_CONFIG_FILENAMES = [
3637

3738
const BABELIGNORE_FILENAME = ".babelignore";
3839

39-
export function* findConfigUpwards(rootDir: string): Handler<string | null> {
40+
export function findConfigUpwards(rootDir: string): string | null {
4041
let dirname = rootDir;
41-
while (true) {
42+
for (;;) {
4243
for (const filename of ROOT_CONFIG_FILENAMES) {
43-
if (yield* fs.exists(path.join(dirname, filename))) {
44+
if (nodeFs.existsSync(path.join(dirname, filename))) {
4445
return dirname;
4546
}
4647
}
@@ -165,7 +166,7 @@ const readConfigJS = makeStrongCache(function* readConfigJS(
165166
caller: CallerMetadata | void;
166167
}>,
167168
): Handler<ConfigFile | null> {
168-
if (!fs.exists.sync(filepath)) {
169+
if (!nodeFs.existsSync(filepath)) {
169170
cache.never();
170171
return null;
171172
}

‎packages/babel-core/src/config/files/index-browser.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@ import type { CallerMetadata } from "../validation/options";
1111

1212
export type { ConfigFile, IgnoreFile, RelativeConfig, FilePackageData };
1313

14-
// eslint-disable-next-line require-yield
15-
export function* findConfigUpwards(
14+
export function findConfigUpwards(
1615
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1716
rootDir: string,
18-
): Handler<string | null> {
17+
): string | null {
1918
return null;
2019
}
2120

‎packages/babel-core/src/config/files/utils.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export function makeStaticFileCache<T>(
2323
}
2424

2525
function fileMtime(filepath: string): number | null {
26+
if (!nodeFs.existsSync(filepath)) return null;
27+
2628
try {
2729
return +nodeFs.statSync(filepath).mtime;
2830
} catch (e) {

‎packages/babel-core/src/config/partial.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,18 @@ import {
2323
import type { ConfigFile, IgnoreFile } from "./files";
2424
import { resolveTargets } from "./resolve-targets";
2525

26-
function* resolveRootMode(
27-
rootDir: string,
28-
rootMode: RootMode,
29-
): Handler<string> {
26+
function resolveRootMode(rootDir: string, rootMode: RootMode): string {
3027
switch (rootMode) {
3128
case "root":
3229
return rootDir;
3330

3431
case "upward-optional": {
35-
const upwardRootDir = yield* findConfigUpwards(rootDir);
32+
const upwardRootDir = findConfigUpwards(rootDir);
3633
return upwardRootDir === null ? rootDir : upwardRootDir;
3734
}
3835

3936
case "upward": {
40-
const upwardRootDir = yield* findConfigUpwards(rootDir);
37+
const upwardRootDir = findConfigUpwards(rootDir);
4138
if (upwardRootDir !== null) return upwardRootDir;
4239

4340
throw Object.assign(
@@ -89,7 +86,7 @@ export default function* loadPrivatePartialConfig(
8986
cloneInputAst = true,
9087
} = args;
9188
const absoluteCwd = path.resolve(cwd);
92-
const absoluteRootDir = yield* resolveRootMode(
89+
const absoluteRootDir = resolveRootMode(
9390
path.resolve(absoluteCwd, rootDir),
9491
rootMode,
9592
);

‎packages/babel-core/src/gensync-utils/fs.ts‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,6 @@ export const readFile = gensync<(filepath: string, encoding: "utf8") => string>(
88
},
99
);
1010

11-
export const exists = gensync<(filepath: string) => boolean>({
12-
sync(path) {
13-
try {
14-
fs.accessSync(path);
15-
return true;
16-
} catch {
17-
return false;
18-
}
19-
},
20-
errback: (path, cb) => fs.access(path, undefined, err => cb(null, !err)),
21-
});
22-
2311
export const stat = gensync<typeof fs.statSync>({
2412
sync: fs.statSync,
2513
errback: fs.stat,

0 commit comments

Comments
 (0)