cevek / ttypescript

Over TypeScript tool to use custom transformers in the tsconfig.json
1.53k stars 56 forks source link

How to run a transformer prior to the time-checking phase #116

Closed TomasHubelbauer closed 3 years ago

TomasHubelbauer commented 3 years ago

Hi, I am working on https://github.com/TomasHubelbauer/ttypescript-esm. This transformer walks ESM imports and if the path of the import is a relative file path and it ends with ?search or #fragment part as it if was a URL and not a path, that part gets removed. I want to remove it for the purposes of type checking (I don't care whether it says there for the emit phase or doesn't as I only use TypeScript for type-checking my JavaScript project so I don't do any complication).

I pretty much just copied Zoltu's https://github.com/Zoltu/typescript-transformer-append-js-extension which does something very similar to imports, but his project does it for the compile-time so his project works great. In my project, the transformer runs too late. When I invoked ttsc, I see that in the output, the URL parts get removed indeed, but the compilation still fails because it looks like in the type-checking phase, the transformer has not run yet:

index.ts:1:17 - error TS2307: Cannot find module './mod?test' or its corresponding type declarations.

1 import mod from './mod?test';
                  ~~~~~~~~~~~~

I tried setting after: false at Zoltu's advice (Zoltu/typescript-transformer-append-js-extension#13) in the transformer config, but it did not have any effect.

I also tried to switch the transformer type to checker, but that only seems to change the outermost function argument from program to checker. I checked the checker API and it doesn't have any methods for updating or adjusting the resolutions of imports/modules. Are there any other options to use to get the transformer to run at type-check time?

TomasHubelbauer commented 3 years ago

FWIW I have found an alternative way of doing this. I have developed a TS Server plugin which monkey-patches the module resolution logic in TypeScript to remove the URL parts which would otherwise break the module resolution and I have created a VS Code extension which distributes this plugin without the need for ttypescript, it works just by installing the extension. https://github.com/TomasHubelbauer/vscode-esm-url

I am going to close this issue, because the above has rendered the question here outdated.