mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
fix(kit,schema): revert changes to module options types (#26850)
This reverts commit c06a683e49
.
This commit is contained in:
parent
7bb02735e7
commit
018b03588a
@ -3,7 +3,7 @@ import { performance } from 'node:perf_hooks'
|
||||
import { defu } from 'defu'
|
||||
import { applyDefaults } from 'untyped'
|
||||
import { dirname } from 'pathe'
|
||||
import type { ModuleDefinition, ModuleOptions, ModuleSetupInstallResult, ModuleSetupReturn, Nuxt, NuxtModule, NuxtOptions, ResolvedModuleOptions, ResolvedNuxtTemplate } from '@nuxt/schema'
|
||||
import type { ModuleDefinition, ModuleOptions, ModuleSetupReturn, Nuxt, NuxtModule, NuxtOptions, ResolvedNuxtTemplate } from '@nuxt/schema'
|
||||
import { logger } from '../logger'
|
||||
import { nuxtCtx, tryUseNuxt, useNuxt } from '../context'
|
||||
import { checkNuxtCompatibility, isNuxt2 } from '../compatibility'
|
||||
@ -13,53 +13,28 @@ import { compileTemplate, templateUtils } from '../internal/template'
|
||||
* Define a Nuxt module, automatically merging defaults with user provided options, installing
|
||||
* any hooks that are provided, and calling an optional setup function for full control.
|
||||
*/
|
||||
export function defineNuxtModule<TOptions extends ModuleOptions> (definition: ModuleDefinition<TOptions> | NuxtModule<TOptions>): NuxtModule<TOptions>
|
||||
|
||||
export function defineNuxtModule<TOptions extends ModuleOptions> (): {
|
||||
with: <TOptionsDefaults extends Partial<TOptions>> (
|
||||
definition: ModuleDefinition<TOptions, TOptionsDefaults> | NuxtModule<TOptions, TOptionsDefaults>
|
||||
) => NuxtModule<TOptions, TOptionsDefaults>
|
||||
}
|
||||
|
||||
export function defineNuxtModule<TOptions extends ModuleOptions> (definition?: ModuleDefinition<TOptions> | NuxtModule<TOptions>) {
|
||||
if (definition) {
|
||||
return _defineNuxtModule(definition)
|
||||
}
|
||||
|
||||
return {
|
||||
with: <TOptionsDefaults extends Partial<TOptions>>(
|
||||
definition: ModuleDefinition<TOptions, TOptionsDefaults> | NuxtModule<TOptions, TOptionsDefaults>,
|
||||
) => _defineNuxtModule(definition),
|
||||
}
|
||||
}
|
||||
|
||||
function _defineNuxtModule<TOptions extends ModuleOptions, TOptionsDefaults extends Partial<TOptions>> (definition: ModuleDefinition<TOptions, TOptionsDefaults> | NuxtModule<TOptions, TOptionsDefaults>): NuxtModule<TOptions, TOptionsDefaults> {
|
||||
if (typeof definition === 'function') { return _defineNuxtModule<TOptions, TOptionsDefaults>({ setup: definition }) }
|
||||
export function defineNuxtModule<OptionsT extends ModuleOptions> (definition: ModuleDefinition<OptionsT> | NuxtModule<OptionsT>): NuxtModule<OptionsT> {
|
||||
if (typeof definition === 'function') { return defineNuxtModule({ setup: definition }) }
|
||||
|
||||
// Normalize definition and meta
|
||||
const module: ModuleDefinition<TOptions, TOptionsDefaults> & Required<Pick<ModuleDefinition<TOptions, TOptionsDefaults>, 'meta'>> = defu(definition, { meta: {} })
|
||||
|
||||
module.meta.configKey ||= module.meta.name
|
||||
const module: ModuleDefinition<OptionsT> & Required<Pick<ModuleDefinition<OptionsT>, 'meta'>> = defu(definition, { meta: {} })
|
||||
if (module.meta.configKey === undefined) {
|
||||
module.meta.configKey = module.meta.name
|
||||
}
|
||||
|
||||
// Resolves module options from inline options, [configKey] in nuxt.config, defaults and schema
|
||||
async function getOptions (inlineOptions?: Partial<TOptions>, nuxt: Nuxt = useNuxt()): Promise<ResolvedModuleOptions<TOptions, TOptionsDefaults>> {
|
||||
const nuxtConfigOptionsKey = module.meta.configKey || module.meta.name
|
||||
|
||||
const nuxtConfigOptions: Partial<TOptions> = nuxtConfigOptionsKey && nuxtConfigOptionsKey in nuxt.options ? nuxt.options[<keyof NuxtOptions> nuxtConfigOptionsKey] : {}
|
||||
|
||||
const optionsDefaults: TOptionsDefaults = module.defaults instanceof Function ? module.defaults(nuxt) : module.defaults ?? <TOptionsDefaults> {}
|
||||
|
||||
let options: ResolvedModuleOptions<TOptions, TOptionsDefaults> = defu(inlineOptions, nuxtConfigOptions, optionsDefaults)
|
||||
|
||||
async function getOptions (inlineOptions?: OptionsT, nuxt: Nuxt = useNuxt()) {
|
||||
const configKey = module.meta.configKey || module.meta.name!
|
||||
const _defaults = module.defaults instanceof Function ? module.defaults(nuxt) : module.defaults
|
||||
let _options = defu(inlineOptions, nuxt.options[configKey as keyof NuxtOptions], _defaults) as OptionsT
|
||||
if (module.schema) {
|
||||
options = await applyDefaults(module.schema, options) as any
|
||||
_options = await applyDefaults(module.schema, _options) as OptionsT
|
||||
}
|
||||
|
||||
return Promise.resolve(options)
|
||||
return Promise.resolve(_options)
|
||||
}
|
||||
|
||||
// Module format is always a simple function
|
||||
async function normalizedModule (this: any, inlineOptions: Partial<TOptions>, nuxt: Nuxt): Promise<ModuleSetupReturn> {
|
||||
async function normalizedModule (this: any, inlineOptions: OptionsT, nuxt: Nuxt) {
|
||||
if (!nuxt) {
|
||||
nuxt = tryUseNuxt() || this.nuxt /* invoked by nuxt 2 */
|
||||
}
|
||||
@ -112,7 +87,7 @@ function _defineNuxtModule<TOptions extends ModuleOptions, TOptionsDefaults exte
|
||||
if (res === false) { return false }
|
||||
|
||||
// Return module install result
|
||||
return defu(res, <ModuleSetupInstallResult> {
|
||||
return defu(res, <ModuleSetupReturn> {
|
||||
timings: {
|
||||
setup: setupTime,
|
||||
},
|
||||
@ -123,7 +98,7 @@ function _defineNuxtModule<TOptions extends ModuleOptions, TOptionsDefaults exte
|
||||
normalizedModule.getMeta = () => Promise.resolve(module.meta)
|
||||
normalizedModule.getOptions = getOptions
|
||||
|
||||
return <NuxtModule<TOptions, TOptionsDefaults>> normalizedModule
|
||||
return normalizedModule as NuxtModule<OptionsT>
|
||||
}
|
||||
|
||||
// -- Nuxt 2 compatibility shims --
|
||||
|
@ -31,7 +31,7 @@ export async function installModule<
|
||||
isNuxt2()
|
||||
// @ts-expect-error Nuxt 2 `moduleContainer` is not typed
|
||||
? await nuxtModule.call(nuxt.moduleContainer, inlineOptions, nuxt)
|
||||
: await nuxtModule(inlineOptions || {}, nuxt)
|
||||
: await nuxtModule(inlineOptions, nuxt)
|
||||
) ?? {}
|
||||
if (res === false /* setup aborted */) {
|
||||
return
|
||||
|
@ -1,4 +1,3 @@
|
||||
import type { Defu } from 'defu'
|
||||
import type { NuxtHooks } from './hooks'
|
||||
import type { Nuxt } from './nuxt'
|
||||
import type { NuxtCompatibility } from './compatibility'
|
||||
@ -27,7 +26,8 @@ export interface ModuleMeta {
|
||||
/** The options received. */
|
||||
export type ModuleOptions = Record<string, any>
|
||||
|
||||
export type ModuleSetupInstallResult = {
|
||||
/** Optional result for nuxt modules */
|
||||
export interface ModuleSetupReturn {
|
||||
/**
|
||||
* Timing information for the initial setup
|
||||
*/
|
||||
@ -39,37 +39,19 @@ export type ModuleSetupInstallResult = {
|
||||
}
|
||||
|
||||
type Awaitable<T> = T | Promise<T>
|
||||
type _ModuleSetupReturn = Awaitable<void | false | ModuleSetupReturn>
|
||||
|
||||
type Prettify<T> = {
|
||||
[K in keyof T]: T[K];
|
||||
} & {}
|
||||
|
||||
export type ModuleSetupReturn = Awaitable<false | void | ModuleSetupInstallResult>
|
||||
|
||||
export type ResolvedModuleOptions<TOptions extends ModuleOptions, TOptionsDefaults extends Partial<TOptions>> = Prettify<
|
||||
Defu<
|
||||
Partial<TOptions>,
|
||||
[Partial<TOptions>, TOptionsDefaults]
|
||||
>
|
||||
>
|
||||
|
||||
/** Module definition passed to 'defineNuxtModule(...)' or 'defineNuxtModule().with(...)'. */
|
||||
export interface ModuleDefinition<
|
||||
TOptions extends ModuleOptions,
|
||||
TOptionsDefaults extends Partial<TOptions> = Partial<TOptions>,
|
||||
> {
|
||||
/** Input module passed to defineNuxtModule. */
|
||||
export interface ModuleDefinition<T extends ModuleOptions = ModuleOptions> {
|
||||
meta?: ModuleMeta
|
||||
defaults?: TOptionsDefaults | ((nuxt: Nuxt) => TOptionsDefaults)
|
||||
schema?: TOptions
|
||||
defaults?: T | ((nuxt: Nuxt) => T)
|
||||
schema?: T
|
||||
hooks?: Partial<NuxtHooks>
|
||||
setup?: (this: void, resolvedOptions: ResolvedModuleOptions<TOptions, TOptionsDefaults>, nuxt: Nuxt) => ModuleSetupReturn
|
||||
setup?: (this: void, resolvedOptions: T, nuxt: Nuxt) => _ModuleSetupReturn
|
||||
}
|
||||
|
||||
export interface NuxtModule<
|
||||
TOptions extends ModuleOptions = ModuleOptions,
|
||||
TOptionsDefaults extends Partial<TOptions> = Partial<TOptions>,
|
||||
> {
|
||||
(this: void, resolvedOptions: ResolvedModuleOptions<TOptions, TOptionsDefaults>, nuxt: Nuxt): ModuleSetupReturn
|
||||
getOptions?: (inlineOptions?: Partial<TOptions>, nuxt?: Nuxt) => Promise<ResolvedModuleOptions<TOptions, TOptionsDefaults>>
|
||||
export interface NuxtModule<T extends ModuleOptions = ModuleOptions> {
|
||||
(this: void, inlineOptions: T, nuxt: Nuxt): _ModuleSetupReturn
|
||||
getOptions?: (inlineOptions?: T, nuxt?: Nuxt) => Promise<T>
|
||||
getMeta?: () => Promise<ModuleMeta>
|
||||
}
|
||||
|
18
test/fixtures/basic-types/types.ts
vendored
18
test/fixtures/basic-types/types.ts
vendored
@ -5,7 +5,6 @@ import type { NavigationFailure, RouteLocationNormalized, RouteLocationRaw, Rout
|
||||
|
||||
import type { AppConfig, RuntimeValue, UpperSnakeCase } from 'nuxt/schema'
|
||||
import { defineNuxtConfig } from 'nuxt/config'
|
||||
import { defineNuxtModule } from 'nuxt/kit'
|
||||
import { callWithNuxt, isVue3 } from '#app'
|
||||
import type { NuxtError } from '#app'
|
||||
import type { NavigateToOptions } from '#app/composables/router'
|
||||
@ -243,23 +242,6 @@ describe('modules', () => {
|
||||
// @ts-expect-error we want to ensure we throw type error on invalid key
|
||||
defineNuxtConfig({ undeclaredKey: { other: false } })
|
||||
})
|
||||
|
||||
it('correctly typed resolved options in defineNuxtModule setup using `.with()`', () => {
|
||||
defineNuxtModule<{
|
||||
foo?: string
|
||||
baz: number
|
||||
}>().with({
|
||||
defaults: {
|
||||
foo: 'bar',
|
||||
},
|
||||
setup: (resolvedOptions) => {
|
||||
expectTypeOf(resolvedOptions).toEqualTypeOf<{
|
||||
foo: string
|
||||
baz?: number | undefined
|
||||
}>()
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('nuxtApp', () => {
|
||||
|
Loading…
Reference in New Issue
Block a user