Esbuild: Support for custom TSConfig path

Created on 6 Jul 2020  Â·  4Comments  Â·  Source: evanw/esbuild

Hey,

Is there a chance ESBuild would support specifying a custom path to tsconfig.json/jsconfig.json?

Prior art

TypeScript supports passing a custom path to tsconfig.json using the -p (or --project) CLI parameter:

tsc -p tsconfig.tests.json

Use cases

My use-case: the project I’m working on uses ESBuild for development and webpack for production. To run ESBuild, we have to replace one package with a custom mock. We do this through tsconfig.json’s paths:

{
    "compilerOptions": {
        "paths": {
            ...
            "linaria": ["./environment/linaria-esbuild-mock.ts"]
        },
    }
}

However, putting this into the primary tsconfig.json results in a bad DX. (E.g., Ctrl+clicking linaria imports now leads to the mock instead of the original file.) It would be great if we had tsconfig.esbuild.json with necessary overrides – and were able to point ESBuild to use it.

Other use-cases: A few projects I saw used tsconfig.test.json for custom test configs (e.g., for custom include/exclude configuration).

Most helpful comment

Thanks for finding this information! After reading that, I agree that following the TypeScript implementation does sound like the right thing to do.

All 4 comments

I've just come across a need for this as well.

The project I'm working on has a setup where the entry point and its tsconfig.json exist in different dirs, which means esbuild can't read the associated tsconfig.json. Within tsconfig.json, I'm defining compilerOptions.baseUrl (similar to OP's compilerOptions.paths).

In my case, I'm using the build() API, so I need to the ability to set a tsconfig.json path programmatically.

So something like:

build({
  entryPoints: ['index.ts'],
  outfile: './dist/index.js',
  tsConfig: './some-other-path/tsconfig.json'
}).catch(() => process.exit(1))

This would provide a ton of flexibility for project organization and build setups. Thanks!

Edit: It's important to point out compilerOptions.baseUrl and compilerOptions.paths should resolve relative to the custom tsconfig.json (tsConfig) path.

I see why this would be useful, but I'm having trouble figuring out what the right implementation would be. There can be many tsconfig.json files spread throughout a directory tree. Does this force-override all of them? Just one of them? Is it only a fallback if no tsconfig.json file is found? Does there need to be some way of configuring different overrides for different directories?

I see why this would be useful, but I'm having trouble figuring out what the right implementation would be. There can be many tsconfig.json files spread throughout a directory tree. Does this force-override all of them? Just one of them? Is it only a fallback if no tsconfig.json file is found? Does there need to be some way of configuring different overrides for different directories?

I’d follow the TS implementation:

Using tsconfig.json or jsconfig.json

  • By invoking tsc with no input files, in which case the compiler searches for the tsconfig.json file starting in the current directory and continuing up the parent directory chain.
  • By invoking tsc with no input files and a --project (or just -p) command line option that specifies the path of a directory containing a tsconfig.json file, or a path to a valid .json file containing the configurations.

— Source

Which means:

  • tsconfig.jsons in child directories are ignored. (E.g., see this test project: here, both root.ts and a/a.ts are compiled to ES5 – even though a/tsconfig.json specifies "target": "ESNext".)
  • If you specify an explicit tsconfig.json path, it will override any automatically detected tsconfig.json

Thanks for finding this information! After reading that, I agree that following the TypeScript implementation does sound like the right thing to do.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

qnp picture qnp  Â·  4Comments

OneOfOne picture OneOfOne  Â·  3Comments

frandiox picture frandiox  Â·  3Comments

ayox picture ayox  Â·  4Comments

ojanvafai picture ojanvafai  Â·  3Comments