From 0c1f9438bc450d7f86605c5ee5bfbe27f162f67d Mon Sep 17 00:00:00 2001 From: Saeid Zareie <65568529+Saeid-Za@users.noreply.github.com> Date: Fri, 10 Jan 2025 17:07:45 +0330 Subject: [PATCH] fix(kit): reorder `#build` to the end of `tsConfig` paths (#30520) --- packages/kit/src/template.ts | 15 ++++++++++ packages/kit/test/generate-types.spec.ts | 38 ++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/packages/kit/src/template.ts b/packages/kit/src/template.ts index 6fd9f0947d..a438044c56 100644 --- a/packages/kit/src/template.ts +++ b/packages/kit/src/template.ts @@ -291,6 +291,10 @@ export async function _generateTypes (nuxt: Nuxt) { })) } + // Ensure `#build` is placed at the end of the paths object. + // https://github.com/nuxt/nuxt/issues/30325 + sortTsPaths(tsConfig.compilerOptions.paths) + tsConfig.include = [...new Set(tsConfig.include.map(p => isAbsolute(p) ? relativeWithDot(nuxt.options.buildDir, p) : p))] tsConfig.exclude = [...new Set(tsConfig.exclude!.map(p => isAbsolute(p) ? relativeWithDot(nuxt.options.buildDir, p) : p))] @@ -335,6 +339,17 @@ export async function writeTypes (nuxt: Nuxt) { await writeFile() } +function sortTsPaths (paths: Record) { + for (const pathKey in paths) { + if (pathKey.startsWith('#build')) { + const pathValue = paths[pathKey]! + // Delete & Reassign to ensure key is inserted at the end of object. + delete paths[pathKey] + paths[pathKey] = pathValue + } + } +} + function renderAttrs (obj: Record) { const attrs: string[] = [] for (const key in obj) { diff --git a/packages/kit/test/generate-types.spec.ts b/packages/kit/test/generate-types.spec.ts index d65d6809bd..b01811e131 100644 --- a/packages/kit/test/generate-types.spec.ts +++ b/packages/kit/test/generate-types.spec.ts @@ -59,4 +59,42 @@ describe('tsConfig generation', () => { ] `) }) + + it('should add #build after #components to paths', async () => { + const { tsConfig } = await _generateTypes(mockNuxtWithOptions({ + alias: { + '~': '/my-app', + '@': '/my-app', + 'some-custom-alias': '/my-app/some-alias', + '#build': './build-dir', + '#build/*': './build-dir/*', + '#imports': './imports', + '#components': './components', + }, + })) + + expect(tsConfig.compilerOptions?.paths).toMatchObject({ + '~': [ + '..', + ], + 'some-custom-alias': [ + '../some-alias', + ], + '@': [ + '..', + ], + '#imports': [ + './imports', + ], + '#components': [ + './components', + ], + '#build': [ + './build-dir', + ], + '#build/*': [ + './build-dir/*', + ], + }) + }) })