nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Set ts config paths from `aliases` config

Open robert-gruner opened this issue 9 months ago • 0 comments

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

  • https://github.com/nuxt/nuxt/issues/21484
  • https://github.com/unjs/nitro/pull/1266

Additional information

  • [X] Would you be willing to help implement this feature?

robert-gruner avatar May 28 '24 15:05 robert-gruner