Currently, I am in the process of creating my own compiler for Typescript because I require the usage of transformers.
Within our workflow, we utilize ts-node to execute specific files (such as individual tests), and it is essential that these transformers are incorporated into the ts-node compiler.
Below is the code snippet:
const ts = require('typescript');
const tsNode = require('ts-node').register;
const keysTransformer = require( 'ts-transformer-keys/transformer');
const tsConfig = require( './tsconfig.json');
const compileProject = () => {
const { options, fileNames } = ts.parseJsonConfigFileContent(
tsConfig,
ts.sys,
__dirname
);
const program = ts.createProgram(fileNames, options);
const transformers = {
before: [keysTransformer(program)],
after: []
};
program.emit(undefined, undefined, undefined, false, transformers);
}
const compileAndRun = (files) => {
tsNode({ files, compilerOptions: tsConfig.compilerOptions, transformers: ["ts-transformer-keys/transformer"] });
files.forEach(file => {
require(file);
});
}
module.export = main = (args) => {
if(args.length >= 2) {
const fileNames = args.splice(2);
compileAndRun(fileNames);
} else {
compileProject();
}
}
main(process.argv);
Integrating the transformer into the TypeScript compiler during the project compilation process works smoothly by using:
const transformers = {
before: [keysTransformer(program)],
after: []
};
However, there seems to be a lack of comprehensive documentation regarding performing the same process with ts-node.