unjs / nitro

Next Generation Server Toolkit. Create web servers with everything you need and deploy them wherever you prefer.
https://nitro.unjs.io
MIT License
5.89k stars 496 forks source link

Set ts config paths from `aliases` config #2476

Open robert-gruner opened 4 months ago

robert-gruner commented 4 months ago

Describe the feature

Context

In the past I used Nuxt extensively, now I am also using Nitro by itself to build a standalone server. The Nuxt/Nitro configs are quite similar. The defineNitroConfig takes an input with type NitroConfig which extends NitroOptions. We have the option to set an alias which is described in the docs here. For Nuxt there is a similar option but the docs are different though.

Goal

My expectation would be that the configs work in the same way. I should be able to put aliases in the Nitro config and they are put to the .nitro/types/tsconfig.json under compilerOptions.paths like Nuxt does.

Findings

I found out that I can achieve the desired behaviour by setting both alias and tsConfig.compilerOptions. Example:

export default defineNitroConfig({
  alias: {
    foo: fileURLToPath(new URL('../bar', import.meta.url)),
  },
  typescript: {
      tsConfig: {
        compilerOptions: {
          paths: {
            foo: [fileURLToPath(new URL('../bar', import.meta.url))],
          },
        },
      },
    },
});

At least I hope that this recreates the same behaviour, first tests look good. In Nuxt it is not necessary to also set the typescript.tsConfig part.

Solution

In the perfect world both configs behave the same way. I cannot really assess which implications a change of the Nitro config has. Can we connect alias/typescript settings? For me also an extension/adjustment of the Nitro docs would be ok, since I needed quite some time and digging to figure out how to configure Nitro correctly (assuming the finding above is correct). Anyways I would be open to contribute/suggest a change.

Related

Additional information