2022-04-15 15:19:05 +00:00
|
|
|
import { promises as fsp } from 'node:fs'
|
2023-03-10 11:30:22 +00:00
|
|
|
import { performance } from 'node:perf_hooks'
|
2023-01-30 11:50:24 +00:00
|
|
|
import { defu } from 'defu'
|
2021-04-02 11:47:01 +00:00
|
|
|
import { applyDefaults } from 'untyped'
|
2021-10-26 14:42:10 +00:00
|
|
|
import { dirname } from 'pathe'
|
2023-04-07 16:02:47 +00:00
|
|
|
import type { ModuleDefinition, ModuleOptions, ModuleSetupReturn, Nuxt, NuxtModule, NuxtOptions, ResolvedNuxtTemplate } from '@nuxt/schema'
|
2022-02-16 21:34:32 +00:00
|
|
|
import { logger } from '../logger'
|
2023-04-07 16:02:47 +00:00
|
|
|
import { nuxtCtx, tryUseNuxt, useNuxt } from '../context'
|
|
|
|
import { checkNuxtCompatibility, isNuxt2 } from '../compatibility'
|
|
|
|
import { compileTemplate, templateUtils } from '../internal/template'
|
2021-04-02 11:47:01 +00:00
|
|
|
|
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.
|
|
|
|
*/
|
2023-06-16 14:47:38 +00:00
|
|
|
export function defineNuxtModule<OptionsT extends ModuleOptions> (definition: ModuleDefinition<OptionsT> | NuxtModule<OptionsT>): NuxtModule<OptionsT> {
|
|
|
|
if (typeof definition === 'function') { return defineNuxtModule({ setup: definition }) }
|
|
|
|
|
2021-12-21 13:57:26 +00:00
|
|
|
// Normalize definition and meta
|
2023-06-16 14:47:38 +00:00
|
|
|
const module: ModuleDefinition<OptionsT> & Required<Pick<ModuleDefinition<OptionsT>, 'meta'>> = defu(definition, { meta: {} })
|
|
|
|
if (module.meta.configKey === undefined) {
|
|
|
|
module.meta.configKey = module.meta.name
|
2021-12-21 13:57:26 +00:00
|
|
|
}
|
2021-04-02 11:47:01 +00:00
|
|
|
|
2021-12-21 13:57:26 +00:00
|
|
|
// Resolves module options from inline options, [configKey] in nuxt.config, defaults and schema
|
2022-09-12 18:22:41 +00:00
|
|
|
async function getOptions (inlineOptions?: OptionsT, nuxt: Nuxt = useNuxt()) {
|
2023-06-16 14:47:38 +00:00
|
|
|
const configKey = module.meta.configKey || module.meta.name!
|
|
|
|
const _defaults = module.defaults instanceof Function ? module.defaults(nuxt) : module.defaults
|
2022-08-22 10:12:02 +00:00
|
|
|
let _options = defu(inlineOptions, nuxt.options[configKey as keyof NuxtOptions], _defaults) as OptionsT
|
2023-06-16 14:47:38 +00:00
|
|
|
if (module.schema) {
|
|
|
|
_options = await applyDefaults(module.schema, _options) as OptionsT
|
2021-04-02 11:47:01 +00:00
|
|
|
}
|
2021-12-21 13:57:26 +00:00
|
|
|
return Promise.resolve(_options)
|
|
|
|
}
|
2021-04-02 11:47:01 +00:00
|
|
|
|
2021-12-21 13:57:26 +00:00
|
|
|
// Module format is always a simple function
|
2022-08-22 10:12:02 +00:00
|
|
|
async function normalizedModule (this: any, inlineOptions: OptionsT, nuxt: Nuxt) {
|
2021-12-21 13:57:26 +00:00
|
|
|
if (!nuxt) {
|
2022-04-04 09:41:48 +00:00
|
|
|
nuxt = tryUseNuxt() || this.nuxt /* invoked by nuxt 2 */
|
2021-04-02 11:47:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 13:57:26 +00:00
|
|
|
// Avoid duplicate installs
|
2023-06-16 14:47:38 +00:00
|
|
|
const uniqueKey = module.meta.name || module.meta.configKey
|
2021-12-21 13:57:26 +00:00
|
|
|
if (uniqueKey) {
|
|
|
|
nuxt.options._requiredModules = nuxt.options._requiredModules || {}
|
|
|
|
if (nuxt.options._requiredModules[uniqueKey]) {
|
2023-01-31 16:44:19 +00:00
|
|
|
return false
|
2021-12-21 13:57:26 +00:00
|
|
|
}
|
|
|
|
nuxt.options._requiredModules[uniqueKey] = true
|
2021-04-02 11:47:01 +00:00
|
|
|
}
|
|
|
|
|
2022-02-16 21:34:32 +00:00
|
|
|
// Check compatibility constraints
|
2023-06-16 14:47:38 +00:00
|
|
|
if (module.meta.compatibility) {
|
|
|
|
const issues = await checkNuxtCompatibility(module.meta.compatibility, nuxt)
|
2021-10-02 18:31:00 +00:00
|
|
|
if (issues.length) {
|
2023-06-16 14:47:38 +00:00
|
|
|
logger.warn(`Module \`${module.meta.name}\` is disabled due to incompatibility issues:\n${issues.toString()}`)
|
2021-10-02 18:31:00 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-21 13:57:26 +00:00
|
|
|
// Prepare
|
|
|
|
nuxt2Shims(nuxt)
|
2021-04-02 11:47:01 +00:00
|
|
|
|
2021-12-21 13:57:26 +00:00
|
|
|
// Resolve module and options
|
|
|
|
const _options = await getOptions(inlineOptions, nuxt)
|
2021-06-24 14:06:16 +00:00
|
|
|
|
2021-12-21 13:57:26 +00:00
|
|
|
// Register hooks
|
2023-06-16 14:47:38 +00:00
|
|
|
if (module.hooks) {
|
|
|
|
nuxt.hooks.addHooks(module.hooks)
|
2021-09-29 10:10:46 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 11:47:01 +00:00
|
|
|
// Call setup
|
2023-03-15 11:26:01 +00:00
|
|
|
const key = `nuxt:module:${uniqueKey || (Math.round(Math.random() * 10000))}`
|
|
|
|
const mark = performance.mark(key)
|
2023-06-16 14:47:38 +00:00
|
|
|
const res = await module.setup?.call(null as any, _options, nuxt) ?? {}
|
2023-03-17 10:08:21 +00:00
|
|
|
const perf = performance.measure(key, mark?.name) // TODO: remove when Node 14 reaches EOL
|
|
|
|
const setupTime = perf ? Math.round((perf.duration * 100)) / 100 : 0 // TODO: remove when Node 14 reaches EOL
|
2023-03-10 11:30:22 +00:00
|
|
|
|
|
|
|
// Measure setup time
|
2023-07-05 14:04:37 +00:00
|
|
|
if (setupTime > 5000 && uniqueKey !== '@nuxt/telemetry') {
|
2023-03-10 11:30:22 +00:00
|
|
|
logger.warn(`Slow module \`${uniqueKey || '<no name>'}\` took \`${setupTime}ms\` to setup.`)
|
|
|
|
} else if (nuxt.options.debug) {
|
|
|
|
logger.info(`Module \`${uniqueKey || '<no name>'}\` took \`${setupTime}ms\` to setup.`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if module is ignored
|
|
|
|
if (res === false) { return false }
|
|
|
|
|
|
|
|
// Return module install result
|
|
|
|
return defu(res, <ModuleSetupReturn> {
|
|
|
|
timings: {
|
|
|
|
setup: setupTime
|
|
|
|
}
|
|
|
|
})
|
2021-04-02 11:47:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 13:57:26 +00:00
|
|
|
// Define getters for options and meta
|
2023-06-16 14:47:38 +00:00
|
|
|
normalizedModule.getMeta = () => Promise.resolve(module.meta)
|
2021-12-21 13:57:26 +00:00
|
|
|
normalizedModule.getOptions = getOptions
|
|
|
|
|
|
|
|
return normalizedModule as NuxtModule<OptionsT>
|
|
|
|
}
|
|
|
|
|
|
|
|
// -- Nuxt 2 compatibility shims --
|
|
|
|
const NUXT2_SHIMS_KEY = '__nuxt2_shims_key__'
|
|
|
|
function nuxt2Shims (nuxt: Nuxt) {
|
|
|
|
// Avoid duplicate install and only apply to Nuxt2
|
2023-04-14 12:53:21 +00:00
|
|
|
if (!isNuxt2(nuxt) || nuxt[NUXT2_SHIMS_KEY as keyof Nuxt]) { return }
|
|
|
|
nuxt[NUXT2_SHIMS_KEY as keyof Nuxt] = true
|
2021-12-21 13:57:26 +00:00
|
|
|
|
|
|
|
// Allow using nuxt.hooks
|
2022-08-22 10:12:02 +00:00
|
|
|
// @ts-expect-error Nuxt 2 extends hookable
|
2021-12-21 13:57:26 +00:00
|
|
|
nuxt.hooks = nuxt
|
|
|
|
|
|
|
|
// Allow using useNuxt()
|
2022-08-04 11:00:01 +00:00
|
|
|
if (!nuxtCtx.tryUse()) {
|
2021-12-21 13:57:26 +00:00
|
|
|
nuxtCtx.set(nuxt)
|
|
|
|
nuxt.hook('close', () => nuxtCtx.unset())
|
|
|
|
}
|
2021-04-02 11:47:01 +00:00
|
|
|
|
2021-12-21 13:57:26 +00:00
|
|
|
// Support virtual templates with getContents() by writing them to .nuxt directory
|
2022-08-22 10:12:02 +00:00
|
|
|
let virtualTemplates: ResolvedNuxtTemplate[]
|
2023-04-14 12:53:21 +00:00
|
|
|
// @ts-expect-error Nuxt 2 hook
|
2021-12-21 13:57:26 +00:00
|
|
|
nuxt.hook('builder:prepared', (_builder, buildOptions) => {
|
2022-10-27 10:36:37 +00:00
|
|
|
virtualTemplates = buildOptions.templates.filter((t: any) => t.getContents)
|
2021-12-21 13:57:26 +00:00
|
|
|
for (const template of virtualTemplates) {
|
|
|
|
buildOptions.templates.splice(buildOptions.templates.indexOf(template), 1)
|
|
|
|
}
|
|
|
|
})
|
2023-04-14 12:53:21 +00:00
|
|
|
// @ts-expect-error Nuxt 2 hook
|
2021-12-21 13:57:26 +00:00
|
|
|
nuxt.hook('build:templates', async (templates) => {
|
|
|
|
const context = {
|
|
|
|
nuxt,
|
|
|
|
utils: templateUtils,
|
|
|
|
app: {
|
|
|
|
dir: nuxt.options.srcDir,
|
|
|
|
extensions: nuxt.options.extensions,
|
|
|
|
plugins: nuxt.options.plugins,
|
|
|
|
templates: [
|
|
|
|
...templates.templatesFiles,
|
|
|
|
...virtualTemplates
|
|
|
|
],
|
|
|
|
templateVars: templates.templateVars
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for await (const template of virtualTemplates) {
|
|
|
|
const contents = await compileTemplate({ ...template, src: '' }, context)
|
|
|
|
await fsp.mkdir(dirname(template.dst), { recursive: true })
|
|
|
|
await fsp.writeFile(template.dst, contents)
|
|
|
|
}
|
|
|
|
})
|
2021-04-02 11:47:01 +00:00
|
|
|
}
|