antfu / esbuild-node-loader

Transpile TypeScript to ESM with Node.js loader.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Resolve tsconfig from current working dir

itsMapleLeaf opened this issue · comments

According to the esbuild docs here, the transform API doesn't read from the filesystem, and doesn't resolve the tsconfig. I'm using MobX, and I need to define useDefineForClassFields in tsconfig for it to work correctly, but esbuild node loader doesn't pick it up.

The fix here would be to resolve it and pass it as tsconfigRaw:

  const {
    code: js,
    warnings,
    map: jsSourceMap,
  } = transformSync(rawSource.toString(), {
    sourcefile: filename,
    sourcemap: 'both',
    loader: new URL(url).pathname.match(extensionsRegex)[1],
    target: `node${process.versions.node}`,
    format: format === 'module' ? 'esm' : 'cjs',
+   tsconfigRaw: resolveTsConfig(),
  })
function resolveTsConfig(fromDir = process.cwd()) {
  const tsconfigPath = join(fromDir, 'tsconfig.json')
  if (fs.existsSync(tsconfigPath)) {
    return fs.readFileSync(tsconfigPath, 'utf8')
  }

  const parentDir = dirname(fromDir)
  if (parentDir === fromDir) return undefined

  return resolveTsConfig(parentDir)
}