From d0fcbfccdd73a4dd822050d168d746cefb1f3152 Mon Sep 17 00:00:00 2001 From: Chris West Date: Tue, 27 Apr 2021 23:41:18 +0100 Subject: [PATCH] perf(core): check files before interacting with them (#13090) * perf(core): check files before interacting with them * refactor: inline fs.exists.sync -> nodeFs.existsSync --- .../babel-core/src/config/files/configuration.ts | 9 +++++---- .../babel-core/src/config/files/index-browser.ts | 5 ++--- packages/babel-core/src/config/files/utils.ts | 2 ++ packages/babel-core/src/config/partial.ts | 11 ++++------- packages/babel-core/src/gensync-utils/fs.ts | 12 ------------ 5 files changed, 13 insertions(+), 26 deletions(-) diff --git a/packages/babel-core/src/config/files/configuration.ts b/packages/babel-core/src/config/files/configuration.ts index df06057cbc..fdd61466a7 100644 --- a/packages/babel-core/src/config/files/configuration.ts +++ b/packages/babel-core/src/config/files/configuration.ts @@ -1,4 +1,5 @@ import buildDebug from "debug"; +import nodeFs from "fs"; import path from "path"; import json5 from "json5"; import gensync from "gensync"; @@ -36,11 +37,11 @@ const RELATIVE_CONFIG_FILENAMES = [ const BABELIGNORE_FILENAME = ".babelignore"; -export function* findConfigUpwards(rootDir: string): Handler { +export function findConfigUpwards(rootDir: string): string | null { let dirname = rootDir; - while (true) { + for (;;) { for (const filename of ROOT_CONFIG_FILENAMES) { - if (yield* fs.exists(path.join(dirname, filename))) { + if (nodeFs.existsSync(path.join(dirname, filename))) { return dirname; } } @@ -165,7 +166,7 @@ const readConfigJS = makeStrongCache(function* readConfigJS( caller: CallerMetadata | void; }>, ): Handler { - if (!fs.exists.sync(filepath)) { + if (!nodeFs.existsSync(filepath)) { cache.never(); return null; } diff --git a/packages/babel-core/src/config/files/index-browser.ts b/packages/babel-core/src/config/files/index-browser.ts index 5740f8210e..ac615d9a15 100644 --- a/packages/babel-core/src/config/files/index-browser.ts +++ b/packages/babel-core/src/config/files/index-browser.ts @@ -11,11 +11,10 @@ import type { CallerMetadata } from "../validation/options"; export type { ConfigFile, IgnoreFile, RelativeConfig, FilePackageData }; -// eslint-disable-next-line require-yield -export function* findConfigUpwards( +export function findConfigUpwards( // eslint-disable-next-line @typescript-eslint/no-unused-vars rootDir: string, -): Handler { +): string | null { return null; } diff --git a/packages/babel-core/src/config/files/utils.ts b/packages/babel-core/src/config/files/utils.ts index 9511a995e8..1604391687 100644 --- a/packages/babel-core/src/config/files/utils.ts +++ b/packages/babel-core/src/config/files/utils.ts @@ -23,6 +23,8 @@ export function makeStaticFileCache( } function fileMtime(filepath: string): number | null { + if (!nodeFs.existsSync(filepath)) return null; + try { return +nodeFs.statSync(filepath).mtime; } catch (e) { diff --git a/packages/babel-core/src/config/partial.ts b/packages/babel-core/src/config/partial.ts index 84dffade41..30ca4fbef8 100644 --- a/packages/babel-core/src/config/partial.ts +++ b/packages/babel-core/src/config/partial.ts @@ -23,21 +23,18 @@ import { import type { ConfigFile, IgnoreFile } from "./files"; import { resolveTargets } from "./resolve-targets"; -function* resolveRootMode( - rootDir: string, - rootMode: RootMode, -): Handler { +function resolveRootMode(rootDir: string, rootMode: RootMode): string { switch (rootMode) { case "root": return rootDir; case "upward-optional": { - const upwardRootDir = yield* findConfigUpwards(rootDir); + const upwardRootDir = findConfigUpwards(rootDir); return upwardRootDir === null ? rootDir : upwardRootDir; } case "upward": { - const upwardRootDir = yield* findConfigUpwards(rootDir); + const upwardRootDir = findConfigUpwards(rootDir); if (upwardRootDir !== null) return upwardRootDir; throw Object.assign( @@ -89,7 +86,7 @@ export default function* loadPrivatePartialConfig( cloneInputAst = true, } = args; const absoluteCwd = path.resolve(cwd); - const absoluteRootDir = yield* resolveRootMode( + const absoluteRootDir = resolveRootMode( path.resolve(absoluteCwd, rootDir), rootMode, ); diff --git a/packages/babel-core/src/gensync-utils/fs.ts b/packages/babel-core/src/gensync-utils/fs.ts index 83681472f2..de0915391e 100644 --- a/packages/babel-core/src/gensync-utils/fs.ts +++ b/packages/babel-core/src/gensync-utils/fs.ts @@ -8,18 +8,6 @@ export const readFile = gensync<(filepath: string, encoding: "utf8") => string>( }, ); -export const exists = gensync<(filepath: string) => boolean>({ - sync(path) { - try { - fs.accessSync(path); - return true; - } catch { - return false; - } - }, - errback: (path, cb) => fs.access(path, undefined, err => cb(null, !err)), -}); - export const stat = gensync({ sync: fs.statSync, errback: fs.stat,