detective-typescript icon indicating copy to clipboard operation
detective-typescript copied to clipboard

Support ESM typescript modules

Open achingbrain opened this issue 2 years ago • 0 comments

If your tsconfig.json has "module": "ES2020" or later (e.g you are outputting ESM and not CJS), TypeScript doesn't rewrite import './foo' to import './foo.js' in the transpiled output - instead you have to use import './foo.js' in your .ts file.

This module then breaks because it (quite reasonably) detects './foo.js' as a dependency which of course doesn't exist.

I've hacked round it by replacing the file extension here and here but it doesn't take into account if ./foo.js does actually exist and that's what you meant to import. I don't know the AST well enough to make that change, but a simple fs.existsSync should be enough?

Another workaround is to run the dep checker over the transpiled output instead of the ts source, but then you lose any import type declarations, so it's not ideal.

Refs: https://devblogs.microsoft.com/typescript/announcing-typescript-4-7/#type-in-package-json-and-new-extensions

achingbrain avatar Jun 14 '22 10:06 achingbrain