mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-30 23:32:38 +00:00
fix(nuxt): hoist environment types (#30230)
This commit is contained in:
parent
66bc09bb94
commit
8cb9efa980
@ -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 { installNuxtModule } from '../core/features'
|
||||
import pagesModule from '../pages/module'
|
||||
@ -177,6 +178,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 || {})])
|
||||
@ -519,31 +550,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'))
|
||||
|
Loading…
Reference in New Issue
Block a user