babel-register: Don’t rewrite the cache if it’s not dirty (#12813)
This saves time and avoids unnecessary SSD wear. Signed-off-by: Anders Kaseorg <andersk@mit.edu>
This commit is contained in:
parent
6af24480c4
commit
a940c0984f
@ -13,6 +13,8 @@ const DEFAULT_FILENAME = path.join(
|
|||||||
const FILENAME: string = process.env.BABEL_CACHE_PATH || DEFAULT_FILENAME;
|
const FILENAME: string = process.env.BABEL_CACHE_PATH || DEFAULT_FILENAME;
|
||||||
let data: Object = {};
|
let data: Object = {};
|
||||||
|
|
||||||
|
let cacheDirty = false;
|
||||||
|
|
||||||
let cacheDisabled = false;
|
let cacheDisabled = false;
|
||||||
|
|
||||||
function isCacheDisabled() {
|
function isCacheDisabled() {
|
||||||
@ -23,7 +25,9 @@ function isCacheDisabled() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
export function save() {
|
export function save() {
|
||||||
if (isCacheDisabled()) return;
|
if (isCacheDisabled() || !cacheDirty) return;
|
||||||
|
cacheDirty = false;
|
||||||
|
|
||||||
let serialised: string = "{}";
|
let serialised: string = "{}";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -112,6 +116,13 @@ export function get(): Object {
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the cache dirty bit.
|
||||||
|
*/
|
||||||
|
export function setDirty() {
|
||||||
|
cacheDirty = true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clear the cache object.
|
* Clear the cache object.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -69,6 +69,7 @@ function compile(code, filename) {
|
|||||||
if (cache) {
|
if (cache) {
|
||||||
cache[cacheKey] = cached;
|
cache[cacheKey] = cached;
|
||||||
cached.mtime = mtime(filename);
|
cached.mtime = mtime(filename);
|
||||||
|
registerCache.setDirty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -30,7 +30,7 @@ function resetCache() {
|
|||||||
|
|
||||||
describe("@babel/register - caching", () => {
|
describe("@babel/register - caching", () => {
|
||||||
describe("cache", () => {
|
describe("cache", () => {
|
||||||
let load, get, save;
|
let load, get, setDirty, save;
|
||||||
let consoleWarnSpy;
|
let consoleWarnSpy;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@ -40,6 +40,7 @@ describe("@babel/register - caching", () => {
|
|||||||
|
|
||||||
load = cache.load;
|
load = cache.load;
|
||||||
get = cache.get;
|
get = cache.get;
|
||||||
|
setDirty = cache.setDirty;
|
||||||
save = cache.save;
|
save = cache.save;
|
||||||
|
|
||||||
consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => {});
|
consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => {});
|
||||||
@ -73,15 +74,24 @@ describe("@babel/register - caching", () => {
|
|||||||
expect(get()).toEqual({});
|
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();
|
save();
|
||||||
|
|
||||||
expect(fs.existsSync(testCacheFilename)).toBe(true);
|
expect(fs.existsSync(testCacheFilename)).toBe(true);
|
||||||
expect(get()).toEqual({});
|
expect(get()).toEqual({});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should create the cache after load", cb => {
|
it("should create the cache after dirty", cb => {
|
||||||
load();
|
load();
|
||||||
|
setDirty();
|
||||||
|
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
expect(fs.existsSync(testCacheFilename)).toBe(true);
|
expect(fs.existsSync(testCacheFilename)).toBe(true);
|
||||||
@ -107,6 +117,7 @@ describe("@babel/register - caching", () => {
|
|||||||
writeCache({ foo: "bar" }, 0o466);
|
writeCache({ foo: "bar" }, 0o466);
|
||||||
|
|
||||||
load();
|
load();
|
||||||
|
setDirty();
|
||||||
|
|
||||||
expect(get()).toEqual({ foo: "bar" });
|
expect(get()).toEqual({ foo: "bar" });
|
||||||
process.nextTick(() => {
|
process.nextTick(() => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user