Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make filepath relative for cache portability #852

Merged
merged 17 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 19 additions & 24 deletions packages/cli/__tests__/compile-stylex-folder-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,11 @@ describe('cache mechanism works as expected', () => {
copiedNodeModules: false,
},
};

beforeAll(async () => {
await fs.rm(cachePath, { recursive: true, force: true });
});

beforeEach(() => {
writeSpy = jest.spyOn(cacheModule, 'writeCache');
});
Expand Down Expand Up @@ -327,7 +332,8 @@ describe('cache mechanism works as expected', () => {

describe('CLI works with a custom cache path', () => {
let writeSpy;
const customCachePath = path.join(__dirname, '__custom_cache__');
const projectRoot = path.resolve(__dirname, '../../../');
const customCachePath = path.join(projectRoot, '__custom_cache__');
const config: TransformConfig = {
input: path.resolve('./source'),
output: path.resolve('./src'),
Expand Down Expand Up @@ -369,31 +375,20 @@ describe('CLI works with a custom cache path', () => {
test('uses the custom cache path for caching', async () => {
await compileDirectory(config);

const customFilePath = path.join(config.input, 'index.js');

const cacheFilePath = path.join(
customCachePath,
path.relative(config.input, customFilePath) + '.json',
);

expect(
await fs
.access(customCachePath)
.then(() => true)
.catch(() => false),
).toBe(true);
expect(
await fs
.access(cacheFilePath)
.then(() => true)
.catch(() => false),
).toBe(true);
const cacheFiles = await fs.readdir(customCachePath);
expect(cacheFiles.length).toEqual(3);

const cacheData = JSON.parse(await fs.readFile(cacheFilePath, 'utf-8'));
expect(cacheData).toHaveProperty('inputHash');
expect(cacheData).toHaveProperty('outputHash');
expect(cacheData).toHaveProperty('collectedCSS');
for (const cacheFile of cacheFiles) {
const cacheFilePath = path.join(customCachePath, cacheFile);
const cacheContent = JSON.parse(
await fs.readFile(cacheFilePath, 'utf-8'),
);
expect(cacheContent).toHaveProperty('inputHash');
expect(cacheContent).toHaveProperty('outputHash');
expect(cacheContent).toHaveProperty('collectedCSS');
}
});

test('skips transformation when cache is valid', async () => {
await compileDirectory(config);

Expand Down
5 changes: 4 additions & 1 deletion packages/cli/src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export function getDefaultCachePath() {
}

export async function getCacheFilePath(cachePath, filePath) {
const fileName = filePath.replace(/[\\/]/g, '__');
const projectRoot = path.resolve(__dirname, '../../../');
mellyeliu marked this conversation as resolved.
Show resolved Hide resolved
const absoluteFilePath = path.resolve(filePath);
const relativePath = path.relative(projectRoot, absoluteFilePath);
const fileName = relativePath.replace(/[\\/]/g, '__');
return path.join(cachePath, `${fileName}.json`);
}

Expand Down
Loading