I have a custom TypeScript watcher that looks something like:
const compilerHost = typescript.createWatchCompilerHost(config.fileNames, config.options, typescript.sys, undefined, reportDiagnostic)
typescript.createWatchProgram(compilerHost)
I want to hook up a custom transformer that is used when the watcher compiles my .ts
files. If I was doing a one-time compile, I would do something like this, supplying the transformer as the last parameter.
const program = typescript.createProgram(config.fileNames, config.options)
const emitResult = program.emit(undefined, undefined, undefined, undefined, { before: [ transformer(program) ] })
However, I cannot find any way to add the transformer to either the compiler host or the watch program creation.
How can I add a custom transformer to a watch compiler host?
One way to do it in theory would be to supply a custom CreateProgram
to the createWatchCompilerHost
that has the emit
function on the returned objects overriden with my custom transformer. I'm unclear on how realistic that path would be though and am looking for a better option.