From dd1cdbc052eb3daf7a9a9b29f6c3effdb6a30e4a Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 8 Mar 2024 22:58:37 +0000 Subject: [PATCH] fix(nuxt): provide typescript aliases for core packages (#26158) --- .../nuxt/src/app/components/nuxt-island.ts | 3 +-- packages/nuxt/src/app/nuxt.ts | 3 +-- packages/nuxt/src/app/types.ts | 3 +++ packages/nuxt/src/core/nuxt.ts | 17 ++++++++++++++++- packages/nuxt/src/core/templates.ts | 18 +++++++----------- packages/nuxt/types.d.mts | 2 +- packages/nuxt/types.d.ts | 2 +- test/basic.test.ts | 3 ++- 8 files changed, 32 insertions(+), 19 deletions(-) diff --git a/packages/nuxt/src/app/components/nuxt-island.ts b/packages/nuxt/src/app/components/nuxt-island.ts index 80e64a6ea8..65a2a9ddc9 100644 --- a/packages/nuxt/src/app/components/nuxt-island.ts +++ b/packages/nuxt/src/app/components/nuxt-island.ts @@ -9,8 +9,7 @@ import { joinURL, withQuery } from 'ufo' import type { FetchResponse } from 'ofetch' import { join } from 'pathe' -// eslint-disable-next-line import/no-restricted-paths -import type { NuxtIslandResponse } from '../../core/runtime/nitro/renderer' +import type { NuxtIslandResponse } from '../types' import { useNuxtApp, useRuntimeConfig } from '../nuxt' import { prerenderRoutes, useRequestEvent } from '../composables/ssr' import { getFragmentHTML } from './utils' diff --git a/packages/nuxt/src/app/nuxt.ts b/packages/nuxt/src/app/nuxt.ts index 8c069985ba..4ba36f4186 100644 --- a/packages/nuxt/src/app/nuxt.ts +++ b/packages/nuxt/src/app/nuxt.ts @@ -11,8 +11,7 @@ import type { AppConfig, AppConfigInput, RuntimeConfig } from 'nuxt/schema' import type { RenderResponse } from 'nitropack' import type { MergeHead, VueHeadClient } from '@unhead/vue' -// eslint-disable-next-line import/no-restricted-paths -import type { NuxtIslandContext } from '../core/runtime/nitro/renderer' +import type { NuxtIslandContext } from '../app/types' import type { RouteMiddleware } from '../app/composables/router' import type { NuxtError } from '../app/composables/error' import type { AsyncDataRequestStatus } from '../app/composables/asyncData' diff --git a/packages/nuxt/src/app/types.ts b/packages/nuxt/src/app/types.ts index 0ec9c30f44..2a7df92c5e 100644 --- a/packages/nuxt/src/app/types.ts +++ b/packages/nuxt/src/app/types.ts @@ -4,3 +4,6 @@ export type { PageMeta } from '../pages/runtime/index' export interface NuxtAppLiterals { [key: string]: string } + +// eslint-disable-next-line import/no-restricted-paths +export type { NuxtIslandContext, NuxtIslandResponse, NuxtRenderHTMLContext } from '../core/runtime/nitro/renderer' diff --git a/packages/nuxt/src/core/nuxt.ts b/packages/nuxt/src/core/nuxt.ts index adc2aac1fa..7195fc9575 100644 --- a/packages/nuxt/src/core/nuxt.ts +++ b/packages/nuxt/src/core/nuxt.ts @@ -1,8 +1,9 @@ -import { join, normalize, relative, resolve } from 'pathe' +import { dirname, join, normalize, relative, resolve } from 'pathe' import { createDebugger, createHooks } from 'hookable' import type { LoadNuxtOptions } from '@nuxt/kit' import { addBuildPlugin, addComponent, addPlugin, addRouteMiddleware, addVitePlugin, addWebpackPlugin, installModule, loadNuxtConfig, logger, nuxtCtx, resolveAlias, resolveFiles, resolvePath, tryResolveModule, useNitro } from '@nuxt/kit' import type { Nuxt, NuxtHooks, NuxtOptions } from 'nuxt/schema' +import { resolvePackageJSON } from 'pkg-types' import escapeRE from 'escape-string-regexp' import fse from 'fs-extra' @@ -61,6 +62,17 @@ async function initNuxt (nuxt: Nuxt) { nuxtCtx.set(nuxt) nuxt.hook('close', () => nuxtCtx.unset()) + const coreTypePackages = ['nitropack', 'defu', 'h3', '@unhead/vue', 'vue', 'vue-router', '@nuxt/schema'] + const paths = Object.fromEntries(await Promise.all(coreTypePackages.map(async pkg => { + const path = await resolvePath(pkg).then(r => resolvePackageJSON(r)) + return [pkg, [dirname(path)]] + }))) + + // Set nitro resolutions for types that might be obscured with shamefully-hoist=false + nuxt.options.nitro.typescript = defu(nuxt.options.nitro.typescript, { + tsConfig: { compilerOptions: { paths } } + }) + // Add nuxt types nuxt.hook('prepare:types', (opts) => { opts.references.push({ types: 'nuxt' }) @@ -73,6 +85,9 @@ async function initNuxt (nuxt: Nuxt) { opts.references.push({ path: resolve(nuxt.options.buildDir, 'types/schema.d.ts') }) opts.references.push({ path: resolve(nuxt.options.buildDir, 'types/app.config.d.ts') }) + // Set Nuxt resolutions for types that might be obscured with shamefully-hoist=false + opts.tsConfig.compilerOptions = defu(opts.tsConfig.compilerOptions, { paths }) + for (const layer of nuxt.options._layers) { const declaration = join(layer.cwd, 'index.d.ts') if (fse.existsSync(declaration)) { diff --git a/packages/nuxt/src/core/templates.ts b/packages/nuxt/src/core/templates.ts index 5a820ac2b1..c43964e363 100644 --- a/packages/nuxt/src/core/templates.ts +++ b/packages/nuxt/src/core/templates.ts @@ -8,7 +8,6 @@ import { hash } from 'ohash' import { camelCase } from 'scule' import { filename } from 'pathe/utils' import type { NuxtTemplate } from 'nuxt/schema' -import { tryResolveModule } from '@nuxt/kit' import { annotatePlugins, checkForCircularDependencies } from './app' @@ -223,14 +222,13 @@ export const middlewareTemplate: NuxtTemplate = { export const nitroSchemaTemplate: NuxtTemplate = { filename: 'types/nitro-nuxt.d.ts', - async getContents ({ nuxt }) { - const localH3 = await tryResolveModule('h3', nuxt.options.modulesDir) || 'h3' + getContents () { return /* typescript */` /// import type { RuntimeConfig } from 'nuxt/schema' -import type { H3Event } from '${localH3}' -import type { NuxtIslandContext, NuxtIslandResponse, NuxtRenderHTMLContext } from 'nuxt/dist/core/runtime/nitro/renderer' +import type { H3Event } from 'h3' +import type { NuxtIslandContext, NuxtIslandResponse, NuxtRenderHTMLContext } from 'nuxt/app' declare module 'nitropack' { interface NitroRuntimeConfigApp { @@ -264,11 +262,10 @@ export const useRuntimeConfig = () => window?.__NUXT__?.config || {} export const appConfigDeclarationTemplate: NuxtTemplate = { filename: 'types/app.config.d.ts', - async getContents ({ app, nuxt }) { - const localDefu = await tryResolveModule('defu', nuxt.options.modulesDir) || 'defu' + getContents ({ app, nuxt }) { return ` import type { CustomAppConfig } from 'nuxt/schema' -import type { Defu } from '${localDefu}' +import type { Defu } from 'defu' ${app.configs.map((id: string, index: number) => `import ${`cfg${index}`} from ${JSON.stringify(id.replace(/(?<=\w)\.\w+$/g, ''))}`).join('\n')} declare const inlineConfig = ${JSON.stringify(nuxt.options.appConfig, null, 2)} @@ -302,11 +299,10 @@ declare module '@nuxt/schema' { export const appConfigTemplate: NuxtTemplate = { filename: 'app.config.mjs', write: true, - async getContents ({ app, nuxt }) { - const localDefu = await tryResolveModule('defu', nuxt.options.modulesDir) || 'defu' + getContents ({ app, nuxt }) { return ` import { updateAppConfig } from '#app/config' -import { defuFn } from '${localDefu}' +import { defuFn } from 'defu' const inlineConfig = ${JSON.stringify(nuxt.options.appConfig, null, 2)} diff --git a/packages/nuxt/types.d.mts b/packages/nuxt/types.d.mts index 210e33bd5c..a2d0de646c 100644 --- a/packages/nuxt/types.d.mts +++ b/packages/nuxt/types.d.mts @@ -4,7 +4,7 @@ export * from './dist/index.js' import type { DefineNuxtConfig } from 'nuxt/config' import type { RuntimeConfig, SchemaDefinition } from 'nuxt/schema' import type { H3Event } from 'h3' -import type { NuxtIslandContext, NuxtIslandResponse, NuxtRenderHTMLContext } from './dist/core/runtime/nitro/renderer.js' +import type { NuxtIslandContext, NuxtIslandResponse, NuxtRenderHTMLContext } from './dist/app/types.js' declare global { const defineNuxtConfig: DefineNuxtConfig diff --git a/packages/nuxt/types.d.ts b/packages/nuxt/types.d.ts index 04663194d6..47afddd4ad 100644 --- a/packages/nuxt/types.d.ts +++ b/packages/nuxt/types.d.ts @@ -4,7 +4,7 @@ export * from './dist/index' import type { DefineNuxtConfig } from 'nuxt/config' import type { RuntimeConfig, SchemaDefinition } from 'nuxt/schema' import type { H3Event } from 'h3' -import type { NuxtIslandContext, NuxtIslandResponse, NuxtRenderHTMLContext } from './dist/core/runtime/nitro/renderer' +import type { NuxtIslandContext, NuxtIslandResponse, NuxtRenderHTMLContext } from './dist/app/types' declare global { const defineNuxtConfig: DefineNuxtConfig diff --git a/test/basic.test.ts b/test/basic.test.ts index c865cfd5b8..b9a8f55c10 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -7,9 +7,10 @@ import { join, normalize } from 'pathe' import { $fetch, createPage, fetch, isDev, setup, startServer, url, useTestContext } from '@nuxt/test-utils/e2e' import { $fetchComponent } from '@nuxt/test-utils/experimental' -import type { NuxtIslandResponse } from '../packages/nuxt/src/core/runtime/nitro/renderer' import { expectNoClientErrors, expectWithPolling, gotoPath, isRenderingJson, parseData, parsePayload, renderPage } from './utils' +import type { NuxtIslandResponse } from '#app' + const isWebpack = process.env.TEST_BUILDER === 'webpack' const isTestingAppManifest = process.env.TEST_MANIFEST !== 'manifest-off'