Skip to content

Commit

Permalink
feat: Implement getCacheKey
Browse files Browse the repository at this point in the history
  • Loading branch information
nvh95 committed Apr 17, 2022
1 parent ae3294a commit 76841ca
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 25 deletions.
24 changes: 5 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"sort": "npx sort-package-json"
},
"dependencies": {
"@jest/create-cache-key-function": "^27.5.1",
"camelcase": "^6.3.0",
"chokidar": "^3.5.3",
"connect": "^3.7.0",
Expand All @@ -62,6 +63,7 @@
"ws": "^8.5.0"
},
"devDependencies": {
"@jest/transform": "^27.5.1",
"@swc/core": "^1.2.154",
"@swc/jest": "^0.2.20",
"@testing-library/jest-dom": "^5.16.2",
Expand Down
4 changes: 2 additions & 2 deletions src/preconfigTransform/css.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

import { processCss } from '../transform';
import { processCss, getCacheKey } from '../transform';

function process(src: string, filename: string) {
return processCss(src, filename);
}
export default { process };
export default { process, getCacheKey };
4 changes: 2 additions & 2 deletions src/preconfigTransform/file.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

import { processFile } from '../transform';
import { processFile, getCacheKey } from '../transform';

function process(src: string, filename: string) {
return processFile(src, filename);
}
export default { process };
export default { process, getCacheKey };
4 changes: 2 additions & 2 deletions src/preconfigTransform/fileCRA.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

import { processFileCRA } from '../transform';
import { processFileCRA, getCacheKey } from '../transform';

function process(src: string, filename: string) {
return processFileCRA(src, filename);
}
export default { process };
export default { process, getCacheKey };
17 changes: 17 additions & 0 deletions src/transform.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import path from 'path';
import * as crypto from 'crypto';
import camelcase from 'camelcase';
import createCacheKey from '@jest/create-cache-key-function';
import type { TransformOptions } from '@jest/transform';

function getRelativeFilename(filename: string): string {
return filename.split(process.cwd())[1];
Expand Down Expand Up @@ -105,3 +108,17 @@ document.body.appendChild(style);
module.exports = exportedTokens`;
}

// Without implementing `cacheKeyFunction`, Jest still caches the result of the transformer
// So what's the point of write a custom `getCacheKey`???
const cacheKeyFunction = createCacheKey();
export function getCacheKey(src: string, filename: string, ...rest: any[]) {
// @ts-expect-error - type overload is confused
const baseCacheKey = cacheKeyFunction(src, filename, ...rest);

// signature mismatch between Jest <27 og >=27
const options: TransformOptions =
typeof rest[0] === 'string' ? rest[1] : rest[0];

return crypto.createHash('md5').update(baseCacheKey).digest('hex');
}

0 comments on commit 76841ca

Please sign in to comment.