fix(nuxt): apply right types to modules and their runtime files (#23240)

This commit is contained in:
Percy Ma 2023-09-22 15:25:52 +08:00 committed by GitHub
parent a2fb8234ac
commit 3b90f82f8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 4 deletions

View File

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

View File

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

View File

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