Barrel Jest Transformer
Motivation
Barrels are helpful when re-group different modules for easier code management and when trying to reduce imported code in big projects. However, @swc/jest execute all imported modules instead of importing only the useful ones per single test. This limitation unnecessarily increases loading time in big projects that contain many tests and heavy barrels. This transformer significantly reduces their build time.
How to use barrel-transformer
You only need to pass a module path of at least one barrel module inside project.source.files.barrelModules array within the project's archibald.json config. If you pass an empty array, the barrel transformer is skipped. The passed module path should match the exact path in your source TS files. For example, if your source imports from barrel: import { AuthHttpService } from 'shop/server/api/services'; then barrelModules array should have shop/server/api/services - the exact path. Also, ensure the barrel module file is named index.ts (e.g. shop/server/api/services/index.ts).
Limitations
The transformer can only work with named exports. Make sure you don't use global re-exports:
export * from './path/to/component'; // skipped!
Also, be aware that the transformer will not execute the contents of the barrelModules index.ts, so make sure the barrel index.ts files don't contain any other code other than named re-exports.
Helpful links for internal contribution with TS Jest transformers:
- https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API
- https://github.com/Grohden/babel-plugin-resolve-barrel-files/blob/main/src/collect-esm-exports.js
- https://github.com/madou/typescript-transformer-handbook#replacing-a-node-with-multiple-nodes
- https://blog.scottlogic.com/2017/05/02/typescript-compiler-api-revisited.html
- https://github.com/madou/typescript-transformer-handbook#visiteachchild
- https://github.com/microsoft/TypeScript/blob/1e65b330a71cc09065b31f1724d78e931aafed51/src/compiler/factory/nodeFactory.ts