From 493c7892d07adda70b8d9f5fb9dd0db8f9b1a623 Mon Sep 17 00:00:00 2001 From: Melissa Liu Date: Sun, 12 Jan 2025 20:14:48 -0500 Subject: [PATCH] fix: fix readCache to use custom cache path when set --- .../__tests__/compile-stylex-folder-test.js | 29 ++++++++++++++----- packages/cli/src/cache.js | 6 ++-- packages/cli/src/transform.js | 2 +- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/packages/cli/__tests__/compile-stylex-folder-test.js b/packages/cli/__tests__/compile-stylex-folder-test.js index 2287047a..1dcf3443 100644 --- a/packages/cli/__tests__/compile-stylex-folder-test.js +++ b/packages/cli/__tests__/compile-stylex-folder-test.js @@ -326,6 +326,7 @@ describe('cache mechanism works as expected', () => { }); describe('CLI works with a custom cache path', () => { + let writeSpy; const customCachePath = path.join(__dirname, '__custom_cache__'); const config: TransformConfig = { input: path.resolve('./source'), @@ -346,14 +347,11 @@ describe('CLI works with a custom cache path', () => { config.cachePath = customCachePath; beforeEach(async () => { - if ( - await fs - .access(customCachePath) - .then(() => true) - .catch(() => false) - ) { - await fs.rm(customCachePath, { recursive: true, force: true }); - } + writeSpy2 = jest.spyOn(cacheModule, 'writeCache'); + }); + + afterEach(() => { + writeSpy2.mockRestore(); }); afterAll(async () => { @@ -396,6 +394,21 @@ describe('CLI works with a custom cache path', () => { expect(cacheData).toHaveProperty('outputHash'); expect(cacheData).toHaveProperty('collectedCSS'); + }); + test('skips transformation when cache is valid', async () => { + await compileDirectory(config); + + // Ensure no additional writes were made due to no file changes + expect(writeSpy2).toHaveBeenCalledTimes(0); + writeSpy2.mockRestore(); + + const customFilePath = path.join(config.input, 'index.js'); + + const cacheFilePath = path.join( + customCachePath, + path.relative(config.input, customFilePath) + '.json', + ); + await fs.rm(cacheFilePath, { recursive: true, force: true }); }); }); diff --git a/packages/cli/src/cache.js b/packages/cli/src/cache.js index 2346a414..96544aa6 100644 --- a/packages/cli/src/cache.js +++ b/packages/cli/src/cache.js @@ -17,13 +17,13 @@ export function getDefaultCachePath() { return path.join('node_modules', '.stylex-cache'); } -async function getCacheFilePath(cachePath, filePath) { +export async function getCacheFilePath(cachePath, filePath) { const fileName = filePath.replace(/[\\/]/g, '__'); return path.join(cachePath, `${fileName}.json`); } -export async function readCache(filePath) { - const cacheFile = await getCacheFilePath(getDefaultCachePath(), filePath); +export async function readCache(cachePath, filePath) { + const cacheFile = await getCacheFilePath(cachePath, filePath); try { const cacheData = await fs.readFile(cacheFile, 'utf-8'); return JSON.parse(cacheData); diff --git a/packages/cli/src/transform.js b/packages/cli/src/transform.js index 81757b98..8c957e65 100644 --- a/packages/cli/src/transform.js +++ b/packages/cli/src/transform.js @@ -108,7 +108,7 @@ export async function compileFile( oldOutputHash = await computeHash(outputFilePath); } - const cacheData = await readCache(filePath); + const cacheData = await readCache(cachePath, filePath); if ( cacheData &&