mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
fix(nuxt): apply right types to modules and their runtime files (#23240)
This commit is contained in:
parent
a2fb8234ac
commit
3b90f82f8d
@ -133,6 +133,22 @@ export function createResolver (base: string | URL): Resolver {
|
||||
}
|
||||
}
|
||||
|
||||
export async function resolveNuxtModule (base: string, paths: string[]) {
|
||||
const resolved = []
|
||||
const resolver = createResolver(base)
|
||||
|
||||
for (const path of paths) {
|
||||
if (path.startsWith(base)) {
|
||||
resolved.push(path.split('/index.ts')[0])
|
||||
} else {
|
||||
const resolvedPath = await resolver.resolvePath(path)
|
||||
resolved.push(resolvedPath.slice(0, resolvedPath.lastIndexOf(path) + path.length))
|
||||
}
|
||||
}
|
||||
|
||||
return resolved
|
||||
}
|
||||
|
||||
// --- Internal ---
|
||||
|
||||
async function existsSensitive (path: string) {
|
||||
|
@ -10,6 +10,7 @@ import { readPackageJSON } from 'pkg-types'
|
||||
import { tryResolveModule } from './internal/esm'
|
||||
import { tryUseNuxt, useNuxt } from './context'
|
||||
import { getModulePaths } from './internal/cjs'
|
||||
import { resolveNuxtModule } from './resolve'
|
||||
|
||||
/**
|
||||
* Renders given template using lodash template during build into the project buildDir
|
||||
@ -113,6 +114,12 @@ export async function writeTypes (nuxt: Nuxt) {
|
||||
|
||||
const rootDirWithSlash = withTrailingSlash(nuxt.options.rootDir)
|
||||
|
||||
const modules = await resolveNuxtModule(rootDirWithSlash,
|
||||
nuxt.options._installedModules
|
||||
.filter(m => m.entryPath)
|
||||
.map(m => m.entryPath)
|
||||
)
|
||||
|
||||
const tsConfig: TSConfig = defu(nuxt.options.typescript?.tsConfig, {
|
||||
compilerOptions: {
|
||||
forceConsistentCasingInFileNames: true,
|
||||
@ -143,10 +150,12 @@ export async function writeTypes (nuxt: Nuxt) {
|
||||
...nuxt.options._layers.map(layer => layer.config.srcDir ?? layer.cwd)
|
||||
.filter(srcOrCwd => !srcOrCwd.startsWith(rootDirWithSlash) || srcOrCwd.includes('node_modules'))
|
||||
.map(srcOrCwd => join(relative(nuxt.options.buildDir, srcOrCwd), '**/*')),
|
||||
...nuxt.options.typescript.includeWorkspace && nuxt.options.workspaceDir !== nuxt.options.rootDir ? [join(relative(nuxt.options.buildDir, nuxt.options.workspaceDir), '**/*')] : []
|
||||
...nuxt.options.typescript.includeWorkspace && nuxt.options.workspaceDir !== nuxt.options.rootDir ? [join(relative(nuxt.options.buildDir, nuxt.options.workspaceDir), '**/*')] : [],
|
||||
...modules.map(m => join(relativeWithDot(nuxt.options.buildDir, m), 'runtime'))
|
||||
],
|
||||
exclude: [
|
||||
...nuxt.options.modulesDir.map(m => relativeWithDot(nuxt.options.buildDir, m)),
|
||||
...modules.map(m => join(relativeWithDot(nuxt.options.buildDir, m), 'runtime/server')),
|
||||
// nitro generate output: https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/core/nitro.ts#L186
|
||||
relativeWithDot(nuxt.options.buildDir, resolve(nuxt.options.rootDir, 'dist'))
|
||||
]
|
||||
|
@ -3,10 +3,10 @@ import { cpus } from 'node:os'
|
||||
import { join, relative, resolve } from 'pathe'
|
||||
import { createRouter as createRadixRouter, exportMatcher, toRouteMatcher } from 'radix3'
|
||||
import { randomUUID } from 'uncrypto'
|
||||
import { joinURL } from 'ufo'
|
||||
import { joinURL, withTrailingSlash } from 'ufo'
|
||||
import { build, copyPublicAssets, createDevServer, createNitro, prepare, prerender, scanHandlers, writeTypes } from 'nitropack'
|
||||
import type { Nitro, NitroConfig } from 'nitropack'
|
||||
import { logger, resolveIgnorePatterns } from '@nuxt/kit'
|
||||
import { logger, resolveIgnorePatterns, resolveNuxtModule } from '@nuxt/kit'
|
||||
import escapeRE from 'escape-string-regexp'
|
||||
import { defu } from 'defu'
|
||||
import fsExtra from 'fs-extra'
|
||||
@ -33,6 +33,14 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
|
||||
? [new RegExp(`node_modules\\/(?!${excludePaths.join('|')})`)]
|
||||
: [/node_modules/]
|
||||
|
||||
const rootDirWithSlash = withTrailingSlash(nuxt.options.rootDir)
|
||||
|
||||
const modules = await resolveNuxtModule(rootDirWithSlash,
|
||||
nuxt.options._installedModules
|
||||
.filter(m => m.entryPath)
|
||||
.map(m => m.entryPath)
|
||||
)
|
||||
|
||||
const nitroConfig: NitroConfig = defu(_nitroConfig, {
|
||||
debug: nuxt.options.debug,
|
||||
rootDir: nuxt.options.rootDir,
|
||||
@ -106,7 +114,8 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
|
||||
tsconfigPath: 'tsconfig.server.json',
|
||||
tsConfig: {
|
||||
include: [
|
||||
join(nuxt.options.buildDir, 'types/nitro-nuxt.d.ts')
|
||||
join(nuxt.options.buildDir, 'types/nitro-nuxt.d.ts'),
|
||||
...modules.map(m => join(relativeWithDot(nuxt.options.buildDir, m), 'runtime/server'))
|
||||
],
|
||||
exclude: [
|
||||
...nuxt.options.modulesDir.map(m => relativeWithDot(nuxt.options.buildDir, m)),
|
||||
|
Loading…
Reference in New Issue
Block a user