mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-29 09:02:03 +00:00
fix(kit): resolve module node_modules
for modulesDir
(#25548)
This commit is contained in:
parent
c6e1c9bb70
commit
2e8d47c269
@ -20,7 +20,7 @@ export * from './pages'
|
|||||||
export * from './plugin'
|
export * from './plugin'
|
||||||
export * from './resolve'
|
export * from './resolve'
|
||||||
export * from './nitro'
|
export * from './nitro'
|
||||||
export * from './template'
|
export { addTemplate, addTypeTemplate, normalizeTemplate, updateTemplates, writeTypes } from './template'
|
||||||
export * from './logger'
|
export * from './logger'
|
||||||
|
|
||||||
// Internal Utils
|
// Internal Utils
|
||||||
|
@ -38,7 +38,7 @@ export async function installModule (moduleToInstall: string | NuxtModule, inlin
|
|||||||
nuxt.options.build.transpile.push(normalizeModuleTranspilePath(moduleToInstall))
|
nuxt.options.build.transpile.push(normalizeModuleTranspilePath(moduleToInstall))
|
||||||
const directory = getDirectory(moduleToInstall)
|
const directory = getDirectory(moduleToInstall)
|
||||||
if (directory !== moduleToInstall && !localLayerModuleDirs.has(directory)) {
|
if (directory !== moduleToInstall && !localLayerModuleDirs.has(directory)) {
|
||||||
nuxt.options.modulesDir.push(directory)
|
nuxt.options.modulesDir.push(resolve(directory, 'node_modules'))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ import { resolveNuxtModule } from './resolve'
|
|||||||
/**
|
/**
|
||||||
* Renders given template using lodash template during build into the project buildDir
|
* 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()
|
const nuxt = useNuxt()
|
||||||
|
|
||||||
// Normalize template
|
// 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
|
* Renders given types using lodash template during build into the project buildDir
|
||||||
* and register them as types.
|
* and register them as types.
|
||||||
*/
|
*/
|
||||||
export function addTypeTemplate <T>(_template: NuxtTypeTemplate<T>) {
|
export function addTypeTemplate<T> (_template: NuxtTypeTemplate<T>) {
|
||||||
const nuxt = useNuxt()
|
const nuxt = useNuxt()
|
||||||
|
|
||||||
const template = addTemplate(_template)
|
const template = addTemplate(_template)
|
||||||
@ -56,7 +56,7 @@ export function addTypeTemplate <T>(_template: NuxtTypeTemplate<T>) {
|
|||||||
/**
|
/**
|
||||||
* Normalize a nuxt template object
|
* 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) {
|
if (!template) {
|
||||||
throw new Error('Invalid template: ' + JSON.stringify(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 }) {
|
export async function updateTemplates (options?: { filter?: (template: ResolvedNuxtTemplate<any>) => boolean }) {
|
||||||
return await tryUseNuxt()?.hooks.callHook('builder:generateApp', options)
|
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 nodeModulePaths = getModulePaths(nuxt.options.modulesDir)
|
||||||
|
|
||||||
const rootDirWithSlash = withTrailingSlash(nuxt.options.rootDir)
|
const rootDirWithSlash = withTrailingSlash(nuxt.options.rootDir)
|
||||||
@ -248,6 +249,15 @@ export async function writeTypes (nuxt: Nuxt) {
|
|||||||
''
|
''
|
||||||
].join('\n')
|
].join('\n')
|
||||||
|
|
||||||
|
return {
|
||||||
|
declaration,
|
||||||
|
tsConfig
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function writeTypes (nuxt: Nuxt) {
|
||||||
|
const { tsConfig, declaration } = await _generateTypes(nuxt)
|
||||||
|
|
||||||
async function writeFile () {
|
async function writeFile () {
|
||||||
const GeneratedBy = '// Generated by nuxi'
|
const GeneratedBy = '// Generated by nuxi'
|
||||||
|
|
||||||
|
65
packages/kit/test/generate-types.spec.ts
Normal file
65
packages/kit/test/generate-types.spec.ts
Normal 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",
|
||||||
|
]
|
||||||
|
`)
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user