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

babel-register: Don’t rewrite the cache if it’s not dirty #12813

Merged
merged 1 commit into from Feb 18, 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
13 changes: 12 additions & 1 deletion packages/babel-register/src/cache.js
Expand Up @@ -13,6 +13,8 @@ const DEFAULT_FILENAME = path.join(
const FILENAME: string = process.env.BABEL_CACHE_PATH || DEFAULT_FILENAME;
let data: Object = {};

let cacheDirty = false;

let cacheDisabled = false;

function isCacheDisabled() {
Expand All @@ -23,7 +25,9 @@ function isCacheDisabled() {
*/

export function save() {
if (isCacheDisabled()) return;
if (isCacheDisabled() || !cacheDirty) return;
cacheDirty = false;

let serialised: string = "{}";

try {
Expand Down Expand Up @@ -112,6 +116,13 @@ export function get(): Object {
return data;
}

/**
* Set the cache dirty bit.
*/
export function setDirty() {
cacheDirty = true;
}

/**
* Clear the cache object.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/babel-register/src/node.js
Expand Up @@ -69,6 +69,7 @@ function compile(code, filename) {
if (cache) {
cache[cacheKey] = cached;
cached.mtime = mtime(filename);
registerCache.setDirty();
}
}

Expand Down
17 changes: 14 additions & 3 deletions packages/babel-register/test/cache.js
Expand Up @@ -30,7 +30,7 @@ function resetCache() {

describe("@babel/register - caching", () => {
describe("cache", () => {
let load, get, save;
let load, get, setDirty, save;
let consoleWarnSpy;

beforeEach(() => {
Expand All @@ -40,6 +40,7 @@ describe("@babel/register - caching", () => {

load = cache.load;
get = cache.get;
setDirty = cache.setDirty;
save = cache.save;

consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => {});
Expand Down Expand Up @@ -73,15 +74,24 @@ describe("@babel/register - caching", () => {
expect(get()).toEqual({});
});

it("should create the cache on save", () => {
it("should not create the cache if not dirty", () => {
save();

expect(fs.existsSync(testCacheFilename)).toBe(false);
expect(get()).toEqual({});
});

it("should create the cache on save if dirty", () => {
setDirty();
save();

expect(fs.existsSync(testCacheFilename)).toBe(true);
expect(get()).toEqual({});
});

it("should create the cache after load", cb => {
it("should create the cache after dirty", cb => {
load();
setDirty();

process.nextTick(() => {
expect(fs.existsSync(testCacheFilename)).toBe(true);
Expand All @@ -107,6 +117,7 @@ describe("@babel/register - caching", () => {
writeCache({ foo: "bar" }, 0o466);

load();
setDirty();

expect(get()).toEqual({ foo: "bar" });
process.nextTick(() => {
Expand Down