aleclarson / vite-tsconfig-paths

Support for TypeScript's path mapping in Vite
MIT License
1.24k stars 43 forks source link

Avoid picking up tsconfig.json if projects is provided #53

Closed guillenotfound closed 2 years ago

guillenotfound commented 2 years ago
❯ mv tsconfig.json.bak tsconfig.json

❯ npx vite build -d
✘ [ERROR] Transforming object literal extensions to the configured target environment ("es5") is not supported yet

    vite.config.ts:31:22:
      31 │         assetFileNames(assetInfo) {
         ╵                       ^

  The target environment was set to "es5" here:

    tsconfig.json:4:14:
      4 │     "target": "es5",
        ╵               ~~~~~

failed to load config from /project/vite.config.ts
error during build:
Error: Build failed with 1 error:
vite.config.ts:31:22: ERROR: Transforming object literal extensions to the configured target environment ("es5") is not supported yet

❯ mv tsconfig.json tsconfig.json.bak

❯ npx vite build -d
  vite:config bundled config file loaded in 171.61ms +0ms
  vite-tsconfig-paths options: {
  projects: [
    '/project/tsconfig.vite.json'
  ],
  extensions: [ '.ts', '.tsx', '.js', '.jsx', '.mjs' ]
} +0ms
  vite-tsconfig-paths [!] missing baseUrl and paths: "/project/tsconfig.vite.json" +5ms

As you can see, when tsconfig.json exists it will pick it up, I know it might not be the plugin but vite itself, is there any way to bypass this behaviour?

guillenotfound commented 2 years ago

Maybe it's because vite.config.ts it's a ts file and therefor needs a tsconfig file in place? But the output looks like esbuild...

aleclarson commented 2 years ago

Are you sure this plugin has any effect on that error?

guillenotfound commented 2 years ago

Sorry for the delay, here is a repo with the reproduction, readme contains all the information I've found: https://github.com/guillenotfound/preact-tsconfig

aleclarson commented 2 years ago

I don't see either tsconfig.nodeA.json (referenced here and here) or tsconfig.nodeB.json (referenced by nothing, except in the readme). Shouldn't those files actually exist in the repo?

guillenotfound commented 2 years ago

Hey aleclarson, sorry I was missing one commit.

Basically what matters is that I'm referencing tsconfig.custom.json here in order to avoid default tsconfig.json file:

https://github.com/guillenotfound/preact-tsconfig/blob/main/vite.config.ts#L8

Then, tsconfig.custom.json points to nodeB file which does not exist, is just a placeholder in order to debug which tsconfig.* is getting picked:

https://github.com/guillenotfound/preact-tsconfig/blob/main/tsconfig.custom.json#L22

But when running npm run dev I'd be expecting nodeB missing, but instead nodeA (the one referenced in tsconfig.json) is the one missing. So it seems like for some reason it's using tsconfig.json rather than tsconfig.custom.json which is the one I'm referencing in the config.

Further checks stats that if I remove default tsconfig file, this plugin works as expected but not if there's default tsconfig.json file in place.

Let me know if you follow me know, otherwise I'll record a quick video to do the showcase.

aleclarson commented 2 years ago

This plugin only crawls the filesystem if the projects option is undefined. See here: https://github.com/aleclarson/vite-tsconfig-paths/blob/7dd87731d6753aad08fe8e948eb5783942e477ec/src/index.ts#L204-L211

The logs in your OP seem to suggest the tsconfig.vite.json file is being loaded:

vite-tsconfig-paths [!] missing baseUrl and paths: "/project/tsconfig.vite.json"
aleclarson commented 2 years ago

I think you need to tell Esbuild which tsconfig to use:

// vite.config.ts

export default defineConfig({
  esbuild: {
    tsconfig: 'tsconfig.vite.json',
  },
})
guillenotfound commented 2 years ago

Didn't know about that config, thanks! 🙏

BTW project option is set and it still uses tsconfig.json for some reason, were you able to reproduce it?

But when running npm run dev I'd be expecting nodeB missing, but instead nodeA (the one referenced in tsconfig.json) is the one missing. So it seems like for some reason it's using tsconfig.json rather than tsconfig.custom.json which is the one I'm referencing in the config.

aleclarson commented 2 years ago

When I run the reproduction with DEBUG="vite-tsconfig-paths" vite dev, I get these logs:

vite-tsconfig-paths options: {
  projects: [ '/workspaces/preact-tsconfig/tsconfig.custom.json' ],
  extensions: [ '.ts', '.tsx', '.js', '.jsx', '.mjs' ]
} +0ms
vite-tsconfig-paths [!] missing baseUrl and paths: "/workspaces/preact-tsconfig/tsconfig.custom.json" +3ms

…which indicate only tsconfig.custom.json is being loaded.

Otherwise, you'd see this debug log: https://github.com/aleclarson/vite-tsconfig-paths/blob/7dd87731d6753aad08fe8e948eb5783942e477ec/src/index.ts#L67