fix(kit): reorder #build to the end of tsConfig paths (#30520)

This commit is contained in:
Saeid Zareie 2025-01-10 17:07:45 +03:30 committed by GitHub
parent 842531d2e5
commit 47f94cf5b5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 53 additions and 0 deletions

View File

@ -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))]
@ -330,6 +334,17 @@ export async function writeTypes (nuxt: Nuxt) {
await writeFile()
}
function sortTsPaths (paths: Record<string, string[]>) {
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<string, string>) {
const attrs: string[] = []
for (const key in obj) {

View File

@ -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/*',
],
})
})
})