esbuild-node-loader icon indicating copy to clipboard operation
esbuild-node-loader copied to clipboard

Resolve tsconfig from current working dir

Open itsMapleLeaf opened this issue 3 years ago • 0 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)
}

itsMapleLeaf avatar Dec 12 '21 00:12 itsMapleLeaf