Skip to content

Commit b070c56

Browse files
authored
feat: make filepath relative for cache portability (#852)
1 parent b58b29e commit b070c56

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

packages/cli/__tests__/compile-stylex-folder-test.js

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,11 @@ describe('cache mechanism works as expected', () => {
258258
copiedNodeModules: false,
259259
},
260260
};
261+
262+
beforeAll(async () => {
263+
await fs.rm(cachePath, { recursive: true, force: true });
264+
});
265+
261266
beforeEach(() => {
262267
writeSpy = jest.spyOn(cacheModule, 'writeCache');
263268
});
@@ -350,7 +355,8 @@ describe('cache mechanism works as expected', () => {
350355

351356
describe('CLI works with a custom cache path', () => {
352357
let writeSpy;
353-
const customCachePath = path.join(__dirname, '__custom_cache__');
358+
const projectRoot = path.resolve(__dirname, '../../../');
359+
const customCachePath = path.join(projectRoot, '__custom_cache__');
354360
const config: TransformConfig = {
355361
input: path.resolve('./source'),
356362
output: path.resolve('./src'),
@@ -392,31 +398,21 @@ describe('CLI works with a custom cache path', () => {
392398
test('uses the custom cache path for caching', async () => {
393399
await compileDirectory(config);
394400

395-
const customFilePath = path.join(config.input, 'index.js');
396-
397-
const cacheFilePath = path.join(
398-
customCachePath,
399-
path.relative(config.input, customFilePath) + '.json',
400-
);
401-
402-
expect(
403-
await fs
404-
.access(customCachePath)
405-
.then(() => true)
406-
.catch(() => false),
407-
).toBe(true);
408-
expect(
409-
await fs
410-
.access(cacheFilePath)
411-
.then(() => true)
412-
.catch(() => false),
413-
).toBe(true);
401+
const cacheFiles = await fs.readdir(customCachePath);
402+
expect(cacheFiles.length).toEqual(3);
414403

415-
const cacheData = JSON.parse(await fs.readFile(cacheFilePath, 'utf-8'));
416-
expect(cacheData).toHaveProperty('inputHash');
417-
expect(cacheData).toHaveProperty('outputHash');
418-
expect(cacheData).toHaveProperty('collectedCSS');
404+
for (const cacheFile of cacheFiles) {
405+
const cacheFilePath = path.join(customCachePath, cacheFile);
406+
const cacheContent = JSON.parse(
407+
await fs.readFile(cacheFilePath, 'utf-8'),
408+
);
409+
expect(cacheContent).toHaveProperty('inputHash');
410+
expect(cacheContent).toHaveProperty('outputHash');
411+
expect(cacheContent).toHaveProperty('collectedCSS');
412+
expect(cacheContent).toHaveProperty('configHash');
413+
}
419414
});
415+
420416
test('skips transformation when cache is valid', async () => {
421417
await compileDirectory(config);
422418

packages/cli/src/cache.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,25 @@ export function getDefaultCachePath() {
1717
return path.join('node_modules', '.stylex-cache');
1818
}
1919

20+
async function findProjectRoot(startDir = __dirname) {
21+
let currentDir = path.resolve(startDir);
22+
while (currentDir !== path.parse(currentDir).root) {
23+
const packageJsonPath = path.join(currentDir, 'package.json');
24+
try {
25+
await fs.access(packageJsonPath);
26+
return currentDir;
27+
} catch (error) {
28+
currentDir = path.dirname(currentDir);
29+
}
30+
}
31+
throw new Error('Project root not found');
32+
}
33+
2034
export async function getCacheFilePath(cachePath, filePath) {
21-
const fileName = filePath.replace(/[\\/]/g, '__');
35+
const projectRoot = await findProjectRoot(filePath);
36+
const absoluteFilePath = path.resolve(filePath);
37+
const relativePath = path.relative(projectRoot, absoluteFilePath);
38+
const fileName = relativePath.replace(/[\\/]/g, '__');
2239
return path.join(cachePath, `${fileName}.json`);
2340
}
2441

0 commit comments

Comments
 (0)