Skip to content

Commit

Permalink
fix(nuxt): provide typescript aliases for core packages (#26158)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Mar 8, 2024
1 parent f0fd15b commit dd1cdbc
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 19 deletions.
3 changes: 1 addition & 2 deletions packages/nuxt/src/app/components/nuxt-island.ts
Expand Up @@ -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'
Expand Down
3 changes: 1 addition & 2 deletions packages/nuxt/src/app/nuxt.ts
Expand Up @@ -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'
Expand Down
3 changes: 3 additions & 0 deletions packages/nuxt/src/app/types.ts
Expand Up @@ -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'
17 changes: 16 additions & 1 deletion 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'
Expand Down Expand Up @@ -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' })
Expand All @@ -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)) {
Expand Down
18 changes: 7 additions & 11 deletions packages/nuxt/src/core/templates.ts
Expand Up @@ -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'

Expand Down Expand Up @@ -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 */`
/// <reference path="./schema.d.ts" />
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 {
Expand Down Expand Up @@ -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)}
Expand Down Expand Up @@ -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)}
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/types.d.mts
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/types.d.ts
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/basic.test.ts
Expand Up @@ -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'

Expand Down

0 comments on commit dd1cdbc

Please sign in to comment.