Skip to content

Commit

Permalink
fix: fix readCache to use custom cache path when set (#851)
Browse files Browse the repository at this point in the history
* feat: replace crypto with murmurhash for cli caching
* fix: fix readCache to use custom cache path when set
  • Loading branch information
mellyeliu authored Jan 17, 2025
1 parent 95caddc commit 24e5158
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
28 changes: 20 additions & 8 deletions packages/cli/__tests__/compile-stylex-folder-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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 });
}
writeSpy = jest.spyOn(cacheModule, 'writeCache');
});

afterEach(() => {
writeSpy.mockRestore();
});

afterAll(async () => {
Expand Down Expand Up @@ -395,6 +393,20 @@ describe('CLI works with a custom cache path', () => {
expect(cacheData).toHaveProperty('inputHash');
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(writeSpy).toHaveBeenCalledTimes(0);
writeSpy.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 });
});
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down

0 comments on commit 24e5158

Please sign in to comment.