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: allow configuring identifiers in jest-transform plugin #841

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
22 changes: 22 additions & 0 deletions .changeset/gorgeous-jars-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
'@vanilla-extract/jest-transform': minor
---

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
63 changes: 35 additions & 28 deletions packages/jest-transform/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };