Skip to content

Commit 4c887b7

Browse files
authored
fix: add cache invalidation on config change (#871)
1 parent f33fb78 commit 4c887b7

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

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

+23
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ describe('cache mechanism works as expected', () => {
290290
expect(cacheContent).toHaveProperty('inputHash');
291291
expect(cacheContent).toHaveProperty('outputHash');
292292
expect(cacheContent).toHaveProperty('collectedCSS');
293+
expect(cacheContent).toHaveProperty('configHash');
293294
}
294295
});
295296

@@ -301,6 +302,28 @@ describe('cache mechanism works as expected', () => {
301302
writeSpy.mockRestore();
302303
});
303304

305+
test('recompiles when config changes', async () => {
306+
config.styleXBundleName = 'stylex_bundle_new.css';
307+
await compileDirectory(config);
308+
309+
// Ensure cache is rewritten due to cache invalidation
310+
expect(writeSpy).toHaveBeenCalledTimes(3);
311+
312+
const cacheFiles = await fs.readdir(cachePath);
313+
expect(cacheFiles.length).toEqual(3);
314+
315+
for (const cacheFile of cacheFiles) {
316+
const cacheFilePath = path.join(cachePath, cacheFile);
317+
const cacheContent = JSON.parse(
318+
await fs.readFile(cacheFilePath, 'utf-8'),
319+
);
320+
expect(cacheContent).toHaveProperty('inputHash');
321+
expect(cacheContent).toHaveProperty('outputHash');
322+
expect(cacheContent).toHaveProperty('collectedCSS');
323+
expect(cacheContent).toHaveProperty('configHash');
324+
}
325+
});
326+
304327
test('recompiles when input changes', async () => {
305328
const mockFilePath = path.join(config.input, 'index.js');
306329
const mockFileOutputPath = path.join(config.output, 'index.js');

packages/cli/src/cache.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,23 @@ export async function deleteCache(cachePath, filePath) {
5757
}
5858
}
5959

60-
export async function computeHash(filePath) {
60+
export function computeStyleXConfigHash(config) {
61+
// Excluding `input` and `output` paths to hash config settings
62+
const configOptions = Object.fromEntries(
63+
Object.entries(config).filter(
64+
([key]) => key !== 'input' && key !== 'output',
65+
),
66+
);
67+
68+
const jsonRepresentation = JSON.stringify(
69+
configOptions,
70+
Object.keys(configOptions).sort(),
71+
);
72+
73+
return hash(jsonRepresentation);
74+
}
75+
76+
export async function computeFilePathHash(filePath) {
6177
const absoluteFilePath = path.resolve(filePath);
6278
const parsedFile = path.parse(absoluteFilePath);
6379

packages/cli/src/transform.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ import fs from 'fs';
2727
import {
2828
writeCache,
2929
readCache,
30-
computeHash,
30+
computeFilePathHash,
31+
computeStyleXConfigHash,
3132
getDefaultCachePath,
3233
} from './cache';
3334
import {
@@ -102,19 +103,22 @@ export async function compileFile(
102103
const outputFilePath = path.join(config.output, filePath);
103104
const cachePath = config.cachePath || getDefaultCachePath();
104105

105-
const inputHash = await computeHash(inputFilePath);
106+
const inputHash = await computeFilePathHash(inputFilePath);
106107
let oldOutputHash = null;
107108
if (fs.existsSync(outputFilePath)) {
108-
oldOutputHash = await computeHash(outputFilePath);
109+
oldOutputHash = await computeFilePathHash(outputFilePath);
109110
}
110111

112+
const configHash = computeStyleXConfigHash(config);
113+
111114
const cacheData = await readCache(cachePath, filePath);
112115

113116
if (
114117
cacheData &&
115118
cacheData.inputHash === inputHash &&
116119
oldOutputHash &&
117-
cacheData.outputHash === oldOutputHash
120+
cacheData.outputHash === oldOutputHash &&
121+
cacheData.configHash === configHash
118122
) {
119123
console.log(`[stylex] Using cached CSS for: ${filePath}`);
120124
config.state.styleXRules.set(filePath, cacheData.collectedCSS);
@@ -133,12 +137,13 @@ export async function compileFile(
133137

134138
writeCompiledJS(outputFilePath, code);
135139

136-
const newOutputHash = await computeHash(outputFilePath);
140+
const newOutputHash = await computeFilePathHash(outputFilePath);
137141

138142
await writeCache(cachePath, filePath, {
139143
inputHash,
140144
outputHash: newOutputHash,
141145
collectedCSS: rules,
146+
configHash,
142147
});
143148
}
144149
}

0 commit comments

Comments
 (0)