ts2esm icon indicating copy to clipboard operation
ts2esm copied to clipboard

Handling exports in package.json from 3rd party packages

Open bennycode opened this issue 8 months ago • 1 comments

Right now, ts2esm simply checks if a JS file is inside the node_modules directory of the referenced package. It doesn’t parse the package.json file of installed modules at all. It sounds like supporting this would require reading the exports field from package.json. Might take some effort to implement.

Relevant docs: https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/#package.json-exports-imports-and-self-referencing

Currently, my isNodeModuleRoot can already locate the relevant package.json file. That would be a good entrypoint to build the parsing functionality. 💡

Utilizing real TypeScript resolver could help too.

So the logic might be like that:

  • take a current bare id, say firebase-functions/v1/https, try to resolve it with typescript's ts.resolveModuleName (see example here https://github.com/nrwl/nx/blob/master/packages/jest/plugins/resolver.ts#L69-L74)
  • if it resolved with moduleResolution: NodeNext + type: module, means this external import is ok. left it as is
  • If it's not resolved, add .js and try again. If finally you get result, replace import in source file.

bennycode avatar Feb 08 '25 15:02 bennycode