fix(kit): resolve module node_modules for modulesDir (#25548)

This commit is contained in:
Damian Głowala 2024-03-06 15:27:17 +01:00 committed by GitHub
parent c6e1c9bb70
commit 2e8d47c269
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 81 additions and 6 deletions

View File

@ -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

View File

@ -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'))
}
}

View File

@ -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
@ -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)
@ -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))
}
@ -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)
@ -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'

View File

@ -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",
]
`)
})
})