fix(nuxt): hoist environment types (#30230)

This commit is contained in:
Daniel Roe 2024-12-11 17:07:21 +00:00
parent 333d71a299
commit 678bf22a41
No known key found for this signature in database
GPG Key ID: CBC814C393D93268

View File

@ -4,7 +4,7 @@ import { join, normalize, relative, resolve } from 'pathe'
import { createDebugger, createHooks } from 'hookable'
import ignore from 'ignore'
import type { LoadNuxtOptions } from '@nuxt/kit'
import { addBuildPlugin, addComponent, addPlugin, addPluginTemplate, addRouteMiddleware, addServerPlugin, addVitePlugin, addWebpackPlugin, installModule, loadNuxtConfig, logger, nuxtCtx, resolveAlias, resolveFiles, resolveIgnorePatterns, resolvePath, tryResolveModule, useNitro } from '@nuxt/kit'
import { addBuildPlugin, addComponent, addPlugin, addPluginTemplate, addRouteMiddleware, addServerPlugin, addTypeTemplate, addVitePlugin, addWebpackPlugin, installModule, loadNuxtConfig, logger, nuxtCtx, resolveAlias, resolveFiles, resolveIgnorePatterns, resolvePath, tryResolveModule, useNitro } from '@nuxt/kit'
import type { Nuxt, NuxtHooks, NuxtModule, NuxtOptions } from 'nuxt/schema'
import type { PackageJson } from 'pkg-types'
import { readPackageJSON } from 'pkg-types'
@ -20,6 +20,7 @@ import { ImpoundPlugin } from 'impound'
import defu from 'defu'
import { gt, satisfies } from 'semver'
import { hasTTY, isCI } from 'std-env'
import { genImport } from 'knitwork'
import pagesModule from '../pages/module'
import metaModule from '../head/module'
@ -175,6 +176,36 @@ async function initNuxt (nuxt: Nuxt) {
nuxt.hook('close', () => nuxtCtx.unset())
const coreTypePackages = nuxt.options.typescript.hoist || []
// Disable environment types entirely if `typescript.builder` is false
if (nuxt.options.typescript.builder !== false) {
const envMap = {
// defaults from `builder` based on package name
'@nuxt/rspack-builder': '@rspack/core/module',
'@nuxt/vite-builder': 'vite/client',
'@nuxt/webpack-builder': 'webpack/module',
// simpler overrides from `typescript.builder` for better DX
'rspack': '@rspack/core/module',
'vite': 'vite/client',
'webpack': 'webpack/module',
// default 'merged' builder environment for module authors
'shared': '@nuxt/schema/builder-env',
}
const overrideEnv = nuxt.options.typescript.builder && envMap[nuxt.options.typescript.builder]
// If there's no override, infer based on builder. If a custom builder is provided, we disable shared types
const defaultEnv = typeof nuxt.options.builder === 'string' ? envMap[nuxt.options.builder] : false
const environmentTypes = overrideEnv || defaultEnv
if (environmentTypes) {
nuxt.options.typescript.hoist.push(environmentTypes)
addTypeTemplate({
filename: 'types/builder-env.d.ts',
getContents: () => genImport(environmentTypes),
})
}
}
const packageJSON = await readPackageJSON(nuxt.options.rootDir).catch(() => ({}) as PackageJson)
const NESTED_PKG_RE = /^[^@]+\//
nuxt._dependencies = new Set([...Object.keys(packageJSON.dependencies || {}), ...Object.keys(packageJSON.devDependencies || {})])
@ -518,31 +549,6 @@ async function initNuxt (nuxt: Nuxt) {
addPlugin(resolve(nuxt.options.appDir, 'plugins/preload.server'))
}
const envMap = {
// defaults from `builder` based on package name
'@nuxt/rspack-builder': '@rspack/core/module',
'@nuxt/vite-builder': 'vite/client',
'@nuxt/webpack-builder': 'webpack/module',
// simpler overrides from `typescript.builder` for better DX
'rspack': '@rspack/core/module',
'vite': 'vite/client',
'webpack': 'webpack/module',
// default 'merged' builder environment for module authors
'shared': '@nuxt/schema/builder-env',
}
nuxt.hook('prepare:types', ({ references }) => {
// Disable entirely if `typescript.builder` is false
if (nuxt.options.typescript.builder === false) { return }
const overrideEnv = nuxt.options.typescript.builder && envMap[nuxt.options.typescript.builder]
// If there's no override, infer based on builder. If a custom builder is provided, we disable shared types
const defaultEnv = typeof nuxt.options.builder === 'string' ? envMap[nuxt.options.builder] : false
const types = overrideEnv || defaultEnv
if (types) { references.push({ types }) }
})
// Add nuxt app debugger
if (nuxt.options.debug) {
addPlugin(resolve(nuxt.options.appDir, 'plugins/debug'))