Skip to content

Commit 5db7b69

Browse files
committed
feat: add cache invalidation on update of nearest .babelrc update
1 parent 67835a4 commit 5db7b69

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ describe('cache mechanism works as expected', () => {
261261

262262
beforeAll(async () => {
263263
await fs.rm(cachePath, { recursive: true, force: true });
264+
await fs.writeFile(path.join(process.cwd(), '.babelrc'), JSON.stringify({ presets: ['@babel/preset-env'] }, null, 2));
264265
});
265266

266267
beforeEach(() => {
@@ -274,6 +275,7 @@ describe('cache mechanism works as expected', () => {
274275
afterAll(async () => {
275276
await fs.rm(config.output, { recursive: true, force: true });
276277
await fs.rm(cachePath, { recursive: true, force: true });
278+
await fs.rm(path.join(process.cwd(), '.babelrc'));
277279
});
278280

279281
test('first compilation populates the cache', async () => {
@@ -329,6 +331,28 @@ describe('cache mechanism works as expected', () => {
329331
}
330332
});
331333

334+
test('recompiles when babelrc changes', async () => {
335+
await fs.writeFile(path.join(process.cwd(), '.babelrc'), JSON.stringify({ presets: ['@babel/preset-react'] }, null, 2));
336+
await compileDirectory(config);
337+
338+
// Ensure cache is rewritten due to cache invalidation
339+
expect(writeSpy).toHaveBeenCalledTimes(3);
340+
341+
const cacheFiles = await fs.readdir(cachePath);
342+
expect(cacheFiles.length).toEqual(3);
343+
344+
for (const cacheFile of cacheFiles) {
345+
const cacheFilePath = path.join(cachePath, cacheFile);
346+
const cacheContent = JSON.parse(
347+
await fs.readFile(cacheFilePath, 'utf-8'),
348+
);
349+
expect(cacheContent).toHaveProperty('inputHash');
350+
expect(cacheContent).toHaveProperty('outputHash');
351+
expect(cacheContent).toHaveProperty('collectedCSS');
352+
expect(cacheContent).toHaveProperty('configHash');
353+
}
354+
});
355+
332356
test('recompiles when input changes', async () => {
333357
const mockFilePath = path.join(config.input, 'index.js');
334358
const mockFileOutputPath = path.join(config.output, 'index.js');

packages/cli/src/cache.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,24 @@ export function getDefaultCachePath() {
1717
return path.join('node_modules', '.stylex-cache');
1818
}
1919

20+
async function findNearestBabelRC(dir) {
21+
let currentDir = dir;
22+
23+
while (currentDir !== path.parse(currentDir).root) {
24+
const babelrcPath = path.join(currentDir, '.babelrc');
25+
try {
26+
await fs.access(babelrcPath);
27+
console.log('Found babelrc:', babelrcPath);
28+
return babelrcPath;
29+
} catch {
30+
currentDir = path.dirname(currentDir);
31+
}
32+
}
33+
console.log('Found no babelrc:');
34+
return null;
35+
}
36+
37+
2038
export async function getCacheFilePath(cachePath, filePath) {
2139
const projectRoot = path.resolve(__dirname, '../../../');
2240
const absoluteFilePath = path.resolve(filePath);
@@ -60,6 +78,22 @@ export async function deleteCache(cachePath, filePath) {
6078
}
6179
}
6280

81+
export async function computeBabelRCHash(path) {
82+
const babelPath = await findNearestBabelRC(path);
83+
if (!babelPath) {
84+
return null; // No .babelrc found
85+
}
86+
87+
try {
88+
const fileBuffer = await fs.readFile(babelPath);
89+
const fileContent = fileBuffer.toString('utf8');
90+
return hash(fileContent);
91+
} catch (error) {
92+
console.error(`Error reading or hashing file: ${error.message}`);
93+
throw error;
94+
}
95+
}
96+
6397
export function computeStyleXConfigHash(config) {
6498
// Excluding `input` and `output` paths to hash config settings
6599
const configOptions = Object.fromEntries(

packages/cli/src/transform.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
readCache,
3030
computeFilePathHash,
3131
computeStyleXConfigHash,
32+
computeBabelRCHash,
3233
getDefaultCachePath,
3334
} from './cache';
3435
import {
@@ -110,6 +111,8 @@ export async function compileFile(
110111
}
111112

112113
const configHash = computeStyleXConfigHash(config);
114+
const babelHash = await computeBabelRCHash(inputFilePath);
115+
console.log(`[stylex] babelHash: ${babelHash}`)
113116

114117
const cacheData = await readCache(cachePath, filePath);
115118

@@ -119,6 +122,7 @@ export async function compileFile(
119122
oldOutputHash &&
120123
cacheData.outputHash === oldOutputHash &&
121124
cacheData.configHash === configHash
125+
&& cacheData.babelHash === babelHash
122126
) {
123127
console.log(`[stylex] Using cached CSS for: ${filePath}`);
124128
config.state.styleXRules.set(filePath, cacheData.collectedCSS);
@@ -144,6 +148,7 @@ export async function compileFile(
144148
outputHash: newOutputHash,
145149
collectedCSS: rules,
146150
configHash,
151+
babelHash,
147152
});
148153
}
149154
}

0 commit comments

Comments
 (0)