Skip to content

Commit

Permalink
fix: fix readCache to use custom cache path when set
Browse files Browse the repository at this point in the history
  • Loading branch information
mellyeliu committed Jan 13, 2025
1 parent 1703c35 commit 493c789
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
29 changes: 21 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;

Check failure on line 329 in packages/cli/__tests__/compile-stylex-folder-test.js

View workflow job for this annotation

GitHub Actions / lint

'writeSpy' is defined but never used. Allowed unused vars must match /^_/u
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 });
}
writeSpy2 = jest.spyOn(cacheModule, 'writeCache');
});

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

afterAll(async () => {
Expand Down Expand Up @@ -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 });
});
});
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 493c789

Please sign in to comment.