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

perf(core): check files before interacting with them #13090

Merged
merged 2 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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