From 3b90f82f8dc525f677516f6ca4360832a756ade8 Mon Sep 17 00:00:00 2001 From: Percy Ma Date: Fri, 22 Sep 2023 15:25:52 +0800 Subject: [PATCH] fix(nuxt): apply right types to modules and their runtime files (#23240) --- packages/kit/src/resolve.ts | 16 ++++++++++++++++ packages/kit/src/template.ts | 11 ++++++++++- packages/nuxt/src/core/nitro.ts | 15 ++++++++++++--- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/packages/kit/src/resolve.ts b/packages/kit/src/resolve.ts index 05a74ff635..54fdd96628 100644 --- a/packages/kit/src/resolve.ts +++ b/packages/kit/src/resolve.ts @@ -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) { diff --git a/packages/kit/src/template.ts b/packages/kit/src/template.ts index 61ae1f817c..be70a8c620 100644 --- a/packages/kit/src/template.ts +++ b/packages/kit/src/template.ts @@ -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')) ] diff --git a/packages/nuxt/src/core/nitro.ts b/packages/nuxt/src/core/nitro.ts index d212126de4..772590c7cc 100644 --- a/packages/nuxt/src/core/nitro.ts +++ b/packages/nuxt/src/core/nitro.ts @@ -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)),