2021-04-02 11:47:01 +00:00
|
|
|
import defu from 'defu'
|
|
|
|
import { applyDefaults } from 'untyped'
|
|
|
|
import { useNuxt, nuxtCtx } from '../nuxt'
|
|
|
|
import type { Nuxt } from '../types/nuxt'
|
|
|
|
import type { NuxtModule, LegacyNuxtModule, ModuleOptions } from '../types/module'
|
|
|
|
|
2021-04-15 18:49:29 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-04-02 11:47:01 +00:00
|
|
|
export function defineNuxtModule<OptionsT extends ModuleOptions> (input: NuxtModule<OptionsT> | ((nuxt: Nuxt) => NuxtModule<OptionsT>)): LegacyNuxtModule {
|
|
|
|
let mod: NuxtModule<OptionsT>
|
|
|
|
|
|
|
|
function wrappedModule (inlineOptions: OptionsT) {
|
|
|
|
// Get nuxt context
|
|
|
|
const nuxt: Nuxt = this.nuxt || useNuxt()
|
|
|
|
|
|
|
|
// Resolve function
|
|
|
|
if (typeof input === 'function') {
|
|
|
|
const fn = input
|
|
|
|
mod = nuxtCtx.call(nuxt, () => fn(nuxt))
|
|
|
|
} else {
|
|
|
|
mod = input
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install hooks
|
|
|
|
if (mod.hooks) {
|
|
|
|
nuxt.hooks.addHooks(mod.hooks)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop if no install provided
|
|
|
|
if (typeof mod.setup !== 'function') {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve options
|
|
|
|
const configKey = mod.configKey || mod.name
|
|
|
|
const userOptions = defu(inlineOptions, nuxt.options[configKey]) as OptionsT
|
|
|
|
const resolvedOptions = applyDefaults(mod.defaults as any, userOptions) as OptionsT
|
|
|
|
|
|
|
|
// Call setup
|
|
|
|
return nuxtCtx.call(nuxt, () => mod.setup.call(null, resolvedOptions, nuxt))
|
|
|
|
}
|
|
|
|
|
|
|
|
wrappedModule.meta = mod
|
|
|
|
|
|
|
|
return wrappedModule
|
|
|
|
}
|