From 14b1d867d47dd7d28e05dadd44811aefb9faa138 Mon Sep 17 00:00:00 2001 From: Mihkel Eidast Date: Tue, 22 Aug 2023 15:18:36 +0300 Subject: [PATCH 1/3] feat: allow configuring identifiers in jest plugin --- packages/jest-transform/src/index.ts | 63 +++++++++++++++------------- 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/packages/jest-transform/src/index.ts b/packages/jest-transform/src/index.ts index 28cfdbfa2..5a649e1cd 100644 --- a/packages/jest-transform/src/index.ts +++ b/packages/jest-transform/src/index.ts @@ -4,39 +4,46 @@ import { transformSync, getPackageInfo, cssFileFilter, + IdentifierOption, } from '@vanilla-extract/integration'; import * as esbuild from 'esbuild'; -const vanillaTransformer: Transformer = { - canInstrument: false, - process(source, filePath, options) { - if (!cssFileFilter.test(filePath)) { - // If the file that passes through to the transformer is not a VE file, - // then it's likely a vanilla .css file (because Jest can't differentiate - // between them) - return { - code: `module.exports = ${JSON.stringify(path.basename(filePath))};`, - }; - } +interface TransformerConfig { + identifiers: IdentifierOption; +} - const { name: packageName } = getPackageInfo(options.config.rootDir); +function createTransformer(config: TransformerConfig): Transformer { + const { identifiers = 'debug' } = config; + return { + canInstrument: false, + process(source, filePath, options) { + if (!cssFileFilter.test(filePath)) { + // If the file that passes through to the transformer is not a VE file, + // then it's likely a vanilla .css file (because Jest can't differentiate + // between them) + return { + code: `module.exports = ${JSON.stringify(path.basename(filePath))};`, + }; + } + const { name: packageName } = getPackageInfo(options.config.rootDir); - const code = transformSync({ - source, - filePath, - rootPath: options.config.rootDir, - packageName: packageName, - identOption: 'debug', - }); + const code = transformSync({ + source, + filePath, + rootPath: options.config.rootDir, + packageName: packageName, + identOption: identifiers, + }); - const result = esbuild.transformSync(code, { - format: options.supportsStaticESM ? 'esm' : 'cjs', - target: 'es2018', - loader: 'ts', - }); + const result = esbuild.transformSync(code, { + format: options.supportsStaticESM ? 'esm' : 'cjs', + target: 'es2018', + loader: 'ts', + }); - return result; - }, -}; + return result; + }, + }; +} -export default vanillaTransformer; +export default { createTransformer }; From 47cfb40b317e87be0f63b9b309702c104ffe7583 Mon Sep 17 00:00:00 2001 From: Adam Skoufis Date: Wed, 28 Feb 2024 20:05:39 +1100 Subject: [PATCH 2/3] Add changeset --- .changeset/gorgeous-jars-hear.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/gorgeous-jars-hear.md diff --git a/.changeset/gorgeous-jars-hear.md b/.changeset/gorgeous-jars-hear.md new file mode 100644 index 000000000..b9c4e109b --- /dev/null +++ b/.changeset/gorgeous-jars-hear.md @@ -0,0 +1,5 @@ +--- +'@vanilla-extract/jest-transform': minor +--- + +Allow configuring identifiers From 789f33abdb712c966a8324012a2454a64ca67dbd Mon Sep 17 00:00:00 2001 From: Adam Skoufis Date: Wed, 28 Feb 2024 22:22:40 +1100 Subject: [PATCH 3/3] Update changeset --- .changeset/gorgeous-jars-hear.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/.changeset/gorgeous-jars-hear.md b/.changeset/gorgeous-jars-hear.md index b9c4e109b..ee8b23131 100644 --- a/.changeset/gorgeous-jars-hear.md +++ b/.changeset/gorgeous-jars-hear.md @@ -2,4 +2,21 @@ '@vanilla-extract/jest-transform': minor --- -Allow configuring identifiers +Allow configuring [custom identifiers] in the jest transformer + +**EXAMPLE USAGE**: + +```js +// jest.config.js + +export default { + transform: { + '^.+\\.css.ts$': [ + '@vanilla-extract/jest-transform', + { identifiers: ({ hash }) => `${hash}_myCustomIdentifier` }, + ], + }, +} +``` + +[custom identifiers]: https://vanilla-extract.style/documentation/integrations/webpack/#identifiers