Skip to content

Commit

Permalink
perf(core): check files before interacting with them (#13090)
Browse files Browse the repository at this point in the history
* perf(core): check files before interacting with them

* refactor: inline fs.exists.sync -> nodeFs.existsSync
  • Loading branch information
FauxFaux committed Apr 27, 2021
1 parent 4753768 commit d0fcbfc
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 26 deletions.
9 changes: 5 additions & 4 deletions packages/babel-core/src/config/files/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import buildDebug from "debug";
import nodeFs from "fs";
import path from "path";
import json5 from "json5";
import gensync from "gensync";
Expand Down Expand Up @@ -36,11 +37,11 @@ const RELATIVE_CONFIG_FILENAMES = [

const BABELIGNORE_FILENAME = ".babelignore";

export function* findConfigUpwards(rootDir: string): Handler<string | null> {
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;
}
}
Expand Down Expand Up @@ -165,7 +166,7 @@ const readConfigJS = makeStrongCache(function* readConfigJS(
caller: CallerMetadata | void;
}>,
): Handler<ConfigFile | null> {
if (!fs.exists.sync(filepath)) {
if (!nodeFs.existsSync(filepath)) {
cache.never();
return null;
}
Expand Down
5 changes: 2 additions & 3 deletions packages/babel-core/src/config/files/index-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
): string | null {
return null;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/babel-core/src/config/files/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export function makeStaticFileCache<T>(
}

function fileMtime(filepath: string): number | null {
if (!nodeFs.existsSync(filepath)) return null;

try {
return +nodeFs.statSync(filepath).mtime;
} catch (e) {
Expand Down
11 changes: 4 additions & 7 deletions packages/babel-core/src/config/partial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,18 @@ import {
import type { ConfigFile, IgnoreFile } from "./files";
import { resolveTargets } from "./resolve-targets";

function* resolveRootMode(
rootDir: string,
rootMode: RootMode,
): Handler<string> {
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(
Expand Down Expand Up @@ -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,
);
Expand Down
12 changes: 0 additions & 12 deletions packages/babel-core/src/gensync-utils/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof fs.statSync>({
sync: fs.statSync,
errback: fs.stat,
Expand Down

0 comments on commit d0fcbfc

Please sign in to comment.