Skip to content

Commit

Permalink
Use gensync fs.stat
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Oct 2, 2019
1 parent 69778ab commit a77def8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
8 changes: 3 additions & 5 deletions packages/babel-core/src/config/caching.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ export function makeWeakCache<ArgT, ResultT, SideChannel>(

export function makeWeakCacheSync<ArgT, ResultT, SideChannel>(
handler: (ArgT, CacheConfigurator<SideChannel>) => ResultT,
invalidateAsync?: boolean,
): (ArgT, SideChannel) => ResultT {
return synchronize<[ArgT, SideChannel], ResultT>(
makeWeakCache<ArgT, ResultT, SideChannel>(handler, invalidateAsync),
makeWeakCache<ArgT, ResultT, SideChannel>(handler, false),
);
}

Expand All @@ -70,10 +69,9 @@ export function makeStrongCache<ArgT, ResultT, SideChannel>(

export function makeStrongCacheSync<ArgT, ResultT, SideChannel>(
handler: (ArgT, CacheConfigurator<SideChannel>) => ResultT,
invalidateAsync?: boolean,
): (ArgT, SideChannel) => ResultT {
return synchronize<[ArgT, SideChannel], ResultT>(
makeStrongCache<ArgT, ResultT, SideChannel>(handler, invalidateAsync),
makeStrongCache<ArgT, ResultT, SideChannel>(handler, false),
);
}

Expand All @@ -89,7 +87,7 @@ function makeCachedFunction<ArgT, ResultT, SideChannel, Cache: *>(
const callCache =
invalidateAsync && (yield* isAsync()) ? callCacheAsync : callCacheSync;

const cached = yield* getCachedValue(callCacheAsync, arg, data);
const cached = yield* getCachedValue(callCache, arg, data);
if (cached.valid) {
return cached.value;
}
Expand Down
1 change: 1 addition & 0 deletions packages/babel-core/src/config/files/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ const readConfigJS = makeStrongCache(function* readConfigJS(
}

if (typeof options.then === "function") {
// When removing this error, ENABLE the invalidateAsync param of makeStrongCache
throw new Error(
`You appear to be using an async configuration, ` +
`which your current version of Babel does not support. ` +
Expand Down
24 changes: 11 additions & 13 deletions packages/babel-core/src/config/files/utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// @flow

import nodeFs from "fs";
import type { Gensync, Handler } from "gensync";

import { makeStrongCache, type CacheConfigurator } from "../caching";
import * as fs from "../../gensync-utils/fs";
import { forwardAsync } from "../../gensync-utils/async";

export function makeStaticFileCache<T>(
fn: (string, string) => T,
Expand All @@ -13,26 +13,24 @@ export function makeStaticFileCache<T>(
filepath: string,
cache: CacheConfigurator<?void>,
): Handler<null | T> {
if (cache.invalidate(() => fileMtimeSync(filepath)) === null) {
const cached = yield* forwardAsync(fileMtime, run =>
cache.invalidate(() => run(filepath)),
);

if (cached === null) {
cache.forever();
return null;
}

return fn(filepath, yield* fs.readFile(filepath, "utf8"));
}): Gensync<any, *>);
},
/* invalidateAsync */ true): Gensync<any, *>);
}

// NOTE: While an asynchronous version of `statSync` exists,
// this function always uses the synchronous version.
// If we sometimes used the async version, we would need to
// the cache generated from an async call to Babel if the
// current call is sync, because the callback passed to
// cache.invalidate must now be synchronous.
// This is technically a breaking change in the cache's behavior,
// and it's probably not worth it anyway.
function fileMtimeSync(filepath: string): number | null {
function* fileMtime(filepath: string): Handler<number | null> {
try {
return +nodeFs.statSync(filepath).mtime;
const stats = yield* fs.stat(filepath);
return +stats.mtime;
} catch (e) {
if (e.code !== "ENOENT" && e.code !== "ENOTDIR") throw e;
}
Expand Down
7 changes: 5 additions & 2 deletions packages/babel-core/src/config/full.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ const loadDescriptor = makeWeakCache(function*(
}

if (typeof item.then === "function") {
yield* [];
yield* []; // if we want to support async plugins

// When enabling this, ENABLE the invalidateAsync param of makeStrongCache
throw new Error(
`You appear to be using an async plugin, ` +
`which your current version of Babel does not support. ` +
Expand Down Expand Up @@ -285,7 +287,8 @@ const instantiatePlugin = makeWeakCache(function*(
}

return new Plugin(plugin, options, alias);
});
},
/* invalidateAsync */ true);

const validateIfOptionNeedsFilename = (
options: ValidatedOptions,
Expand Down
7 changes: 6 additions & 1 deletion packages/babel-core/src/gensync-utils/fs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow

import fs from "fs";
import fs, { Stats } from "fs";
import gensync from "gensync";

export const readFile = gensync<[string, "utf8"], string>({
Expand All @@ -12,3 +12,8 @@ export const exists = gensync<[string], boolean>({
sync: fs.existsSync,
errback: (path, cb) => fs.exists(path, res => cb(null, res)),
});

export const stat = gensync<[string], Stats>({
sync: fs.statSync,
errback: fs.stat,
});

0 comments on commit a77def8

Please sign in to comment.