diff --git a/package-lock.json b/package-lock.json index 4733b2bc..8d0794f5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -640,7 +640,6 @@ "version": "27.5.1", "resolved": "https://registry.npmjs.org/@jest/create-cache-key-function/-/create-cache-key-function-27.5.1.tgz", "integrity": "sha512-dmH1yW+makpTSURTy8VzdUwFnfQh1G8R+DxO2Ho2FFmBbKFEVm+3jWdvFhE2VqB/LATCTokkP0dotjyQyw5/AQ==", - "dev": true, "requires": { "@jest/types": "^27.5.1" } @@ -899,7 +898,6 @@ "version": "27.5.1", "resolved": "https://registry.npmjs.org/@jest/types/-/types-27.5.1.tgz", "integrity": "sha512-Cx46iJ9QpwQTjIdq5VJu2QTMMs3QlEjI0x1QbBP5W1+nMzyc2XmimiRR/CbX9TO0cPTeUlxWMOu8mslYsJ8DEw==", - "dev": true, "requires": { "@types/istanbul-lib-coverage": "^2.0.0", "@types/istanbul-reports": "^3.0.0", @@ -912,7 +910,6 @@ "version": "4.3.0", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", - "dev": true, "requires": { "color-convert": "^2.0.1" } @@ -921,7 +918,6 @@ "version": "4.1.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", - "dev": true, "requires": { "ansi-styles": "^4.1.0", "supports-color": "^7.1.0" @@ -931,7 +927,6 @@ "version": "2.0.1", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", - "dev": true, "requires": { "color-name": "~1.1.4" } @@ -939,20 +934,17 @@ "color-name": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", - "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", - "dev": true + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" }, "has-flag": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", - "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", - "dev": true + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==" }, "supports-color": { "version": "7.2.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", - "dev": true, "requires": { "has-flag": "^4.0.0" } @@ -1364,14 +1356,12 @@ "@types/istanbul-lib-coverage": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.4.tgz", - "integrity": "sha512-z/QT1XN4K4KYuslS23k62yDIDLwLFkzxOuMplDtObz0+y7VqJCaO2o+SPwHCvLFZh7xazvvoor2tA/hPz9ee7g==", - "dev": true + "integrity": "sha512-z/QT1XN4K4KYuslS23k62yDIDLwLFkzxOuMplDtObz0+y7VqJCaO2o+SPwHCvLFZh7xazvvoor2tA/hPz9ee7g==" }, "@types/istanbul-lib-report": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/@types/istanbul-lib-report/-/istanbul-lib-report-3.0.0.tgz", "integrity": "sha512-plGgXAPfVKFoYfa9NpYDAkseG+g6Jr294RqeqcqDixSbU34MZVJRi/P+7Y8GDpzkEwLaGZZOpKIEmeVZNtKsrg==", - "dev": true, "requires": { "@types/istanbul-lib-coverage": "*" } @@ -1380,7 +1370,6 @@ "version": "3.0.1", "resolved": "https://registry.npmjs.org/@types/istanbul-reports/-/istanbul-reports-3.0.1.tgz", "integrity": "sha512-c3mAZEuK0lvBp8tmuL74XRKn1+y2dcwOUpH7x4WrF6gk1GIgiluDRgMYQtw2OFcBvAJWlt6ASU3tSqxp0Uu0Aw==", - "dev": true, "requires": { "@types/istanbul-lib-report": "*" } @@ -1398,8 +1387,7 @@ "@types/node": { "version": "17.0.21", "resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.21.tgz", - "integrity": "sha512-DBZCJbhII3r90XbQxI8Y9IjjiiOGlZ0Hr32omXIZvwwZ7p4DMMXGrKXVyPfuoBOri9XNtL0UK69jYIBIsRX3QQ==", - "dev": true + "integrity": "sha512-DBZCJbhII3r90XbQxI8Y9IjjiiOGlZ0Hr32omXIZvwwZ7p4DMMXGrKXVyPfuoBOri9XNtL0UK69jYIBIsRX3QQ==" }, "@types/prettier": { "version": "2.4.4", @@ -1469,7 +1457,6 @@ "version": "16.0.4", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-16.0.4.tgz", "integrity": "sha512-T8Yc9wt/5LbJyCaLiHPReJa0kApcIgJ7Bn735GjItUfh08Z1pJvu8QZqb9s+mMvKV6WUQRV7K2R46YbjMXTTJw==", - "dev": true, "requires": { "@types/yargs-parser": "*" } @@ -1477,8 +1464,7 @@ "@types/yargs-parser": { "version": "21.0.0", "resolved": "https://registry.npmjs.org/@types/yargs-parser/-/yargs-parser-21.0.0.tgz", - "integrity": "sha512-iO9ZQHkZxHn4mSakYV0vFHAVDyEOIJQrV2uZ06HxEPcx+mt8swXoZHIbaaJ2crJYFfErySgktuTZ3BeLz+XmFA==", - "dev": true + "integrity": "sha512-iO9ZQHkZxHn4mSakYV0vFHAVDyEOIJQrV2uZ06HxEPcx+mt8swXoZHIbaaJ2crJYFfErySgktuTZ3BeLz+XmFA==" }, "@vitejs/plugin-react": { "version": "1.3.0", diff --git a/package.json b/package.json index 7a3d55da..547002a6 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", diff --git a/src/preconfigTransform/css.ts b/src/preconfigTransform/css.ts index 8bb1edee..5b9cbbab 100644 --- a/src/preconfigTransform/css.ts +++ b/src/preconfigTransform/css.ts @@ -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 }; diff --git a/src/preconfigTransform/file.ts b/src/preconfigTransform/file.ts index 236ea502..882fc524 100644 --- a/src/preconfigTransform/file.ts +++ b/src/preconfigTransform/file.ts @@ -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 }; diff --git a/src/preconfigTransform/fileCRA.ts b/src/preconfigTransform/fileCRA.ts index 1da04089..ea77a21f 100644 --- a/src/preconfigTransform/fileCRA.ts +++ b/src/preconfigTransform/fileCRA.ts @@ -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 }; diff --git a/src/transform.ts b/src/transform.ts index ffefc2f6..d77c4679 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -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]; @@ -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'); +}