Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kit): correctly exclude local modules in tsconfig.json #25548

Merged
merged 10 commits into from
Mar 6, 2024
2 changes: 1 addition & 1 deletion packages/kit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export * from './pages'
export * from './plugin'
export * from './resolve'
export * from './nitro'
export * from './template'
export { addTemplate, addTypeTemplate, normalizeTemplate, updateTemplates, writeTypes } from './template'
export * from './logger'

// Internal Utils
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/module/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export async function installModule (moduleToInstall: string | NuxtModule, inlin
nuxt.options.build.transpile.push(normalizeModuleTranspilePath(moduleToInstall))
const directory = getDirectory(moduleToInstall)
if (directory !== moduleToInstall && !localLayerModuleDirs.has(directory)) {
nuxt.options.modulesDir.push(directory)
nuxt.options.modulesDir.push(resolve(directory, 'node_modules'))
}
}

Expand Down
18 changes: 14 additions & 4 deletions packages/kit/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { resolveNuxtModule } from './resolve'
/**
* Renders given template using lodash template during build into the project buildDir
*/
export function addTemplate <T>(_template: NuxtTemplate<T> | string) {
export function addTemplate<T> (_template: NuxtTemplate<T> | string) {
const nuxt = useNuxt()

// Normalize template
Expand All @@ -36,7 +36,7 @@ export function addTemplate <T>(_template: NuxtTemplate<T> | string) {
* Renders given types using lodash template during build into the project buildDir
* and register them as types.
*/
export function addTypeTemplate <T>(_template: NuxtTypeTemplate<T>) {
export function addTypeTemplate<T> (_template: NuxtTypeTemplate<T>) {
const nuxt = useNuxt()

const template = addTemplate(_template)
Expand All @@ -56,7 +56,7 @@ export function addTypeTemplate <T>(_template: NuxtTypeTemplate<T>) {
/**
* Normalize a nuxt template object
*/
export function normalizeTemplate <T>(template: NuxtTemplate<T> | string): ResolvedNuxtTemplate<T> {
export function normalizeTemplate<T> (template: NuxtTemplate<T> | string): ResolvedNuxtTemplate<T> {
if (!template) {
throw new Error('Invalid template: ' + JSON.stringify(template))
}
Expand Down Expand Up @@ -110,7 +110,8 @@ export function normalizeTemplate <T>(template: NuxtTemplate<T> | string): Resol
export async function updateTemplates (options?: { filter?: (template: ResolvedNuxtTemplate<any>) => boolean }) {
return await tryUseNuxt()?.hooks.callHook('builder:generateApp', options)
}
export async function writeTypes (nuxt: Nuxt) {

export async function _generateTypes (nuxt: Nuxt) {
const nodeModulePaths = getModulePaths(nuxt.options.modulesDir)

const rootDirWithSlash = withTrailingSlash(nuxt.options.rootDir)
Expand Down Expand Up @@ -248,6 +249,15 @@ export async function writeTypes (nuxt: Nuxt) {
''
].join('\n')

return {
declaration,
tsConfig
}
}

export async function writeTypes (nuxt: Nuxt) {
const { tsConfig, declaration } = await _generateTypes(nuxt)

async function writeFile () {
const GeneratedBy = '// Generated by nuxi'

Expand Down
65 changes: 65 additions & 0 deletions packages/kit/test/generate-types.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { describe, expect, it } from 'vitest'
import type { Nuxt, NuxtConfig } from '@nuxt/schema'
import { defu } from 'defu'

import { _generateTypes } from '../src/template'

type DeepPartial<T> = {
[P in keyof T]?: T[P] extends Record<string, any> ? DeepPartial<T[P]> : T[P]
}

const mockNuxt = {
options: {
rootDir: '/my-app',
srcDir: '/my-app',
alias: {
'~': '/my-app',
'some-custom-alias': '/my-app/some-alias'
},
typescript: { includeWorkspace: false },
buildDir: '/my-app/.nuxt',
modulesDir: ['/my-app/node_modules', '/node_modules'],
modules: [],
_layers: [{ config: { srcDir: '/my-app' } }],
_installedModules: [],
_modules: [],
},
callHook: () => {},
} satisfies DeepPartial<Nuxt> as unknown as Nuxt

const mockNuxtWithOptions = (options: NuxtConfig) => defu({ options }, mockNuxt) as Nuxt

describe('tsConfig generation', () => {
it('should add add correct relative paths for aliases', async () => {
const { tsConfig } = await _generateTypes(mockNuxt)
expect(tsConfig.compilerOptions?.paths).toMatchInlineSnapshot(`
{
"#build": [
".",
],
"some-custom-alias": [
"../some-alias",
],
"~": [
"..",
],
}
`)
})

it('should add add exclude for module paths', async () => {
const { tsConfig } = await _generateTypes(mockNuxtWithOptions({
modulesDir: ['/my-app/modules/test/node_modules', '/my-app/modules/node_modules', '/my-app/node_modules/@some/module/node_modules']
}))
expect(tsConfig.exclude).toMatchInlineSnapshot(`
[
"../modules/test/node_modules",
"../modules/node_modules",
"../node_modules/@some/module/node_modules",
"../node_modules",
"../../node_modules",
"../dist",
]
`)
})
})