privatenumber / tsx

⚡️ TypeScript Execute | The easiest way to run TypeScript in Node.js
https://tsx.is
MIT License
8.61k stars 132 forks source link

Apply rootDir/outDir when resolving imports #603

Open JavaScriptBach opened 5 days ago

JavaScriptBach commented 5 days ago

Acknowledgements

Minimal reproduction URL

https://github.com/JavaScriptBach/ts-repo-structure

Problem & expected behavior (under 200 words)

The Problem

I have a Typescript monorepo with source files in <package>/src/*.ts and js files outputted to <package>/dist/*.js. Each package also uses imports and exports in the package.json to control entry points and allow for bare imports. A small repro is linked below.

This configuration works perfectly in Typescript and Node, but not in tsx.

I think the key missing section is here. It looks like we did some extension remapping in https://github.com/privatenumber/tsx/issues/59, but I think we also need handle rootDir and outDir to have full parity with Typescript.

If the package.json is part of the local project, an additional remapping step is performed in order to find the input TypeScript implementation file that will eventually produce the output JavaScript or declaration file path that was resolved from "imports". Without this step, any compilation that resolves an "imports" path would be referencing output files from the previous compilation instead of other input files that are intended to be included in the current compilation. This remapping uses the outDir/declarationDir and rootDir from the tsconfig.json, so using "imports" usually requires an explicit rootDir to be set.

Repro

  1. Check out https://github.com/JavaScriptBach/ts-repo-structure and run yarn
  2. Run npx tsx a/src/a.ts
node:internal/modules/cjs/loader:1068
  const err = new Error(`Cannot find module '${request}'`);
              ^

Error: Cannot find module '/Users/philliphuang/ts-repo-structure/node_modules/b/dist/a.js'
    at createEsmNotFoundErr (node:internal/modules/cjs/loader:1068:15)
    at finalizeEsmResolution (node:internal/modules/cjs/loader:1061:15)
    at resolveExports (node:internal/modules/cjs/loader:538:14)
    at Module._findPath (node:internal/modules/cjs/loader:607:31)
    at Module._resolveFilename (node:internal/modules/cjs/loader:1033:27)
    at resolve (/Users/philliphuang/ts-repo-structure/node_modules/tsx/dist/register-BujtrvNV.cjs:1:3084)
    at resolveRequest (/Users/philliphuang/ts-repo-structure/node_modules/tsx/dist/register-BujtrvNV.cjs:1:2618)
    at Function._resolveFilename (/Users/philliphuang/ts-repo-structure/node_modules/tsx/dist/register-BujtrvNV.cjs:1:3400)
    at Module._load (node:internal/modules/cjs/loader:893:27)
    at Module.require (node:internal/modules/cjs/loader:1113:19) {
  code: 'MODULE_NOT_FOUND',
  path: '/Users/philliphuang/ts-repo-structure/node_modules/b/package.json'
}

Bugs are expected to be fixed by those affected by it

Compensating engineering work will speed up resolution and support the project