2022-02-08 19:09:44 +00:00
|
|
|
import type { Nuxt, NuxtModule } from '@nuxt/schema'
|
2023-04-03 12:04:56 +00:00
|
|
|
import { isNuxt2 } from '../compatibility'
|
2021-12-21 13:57:26 +00:00
|
|
|
import { useNuxt } from '../context'
|
2023-04-07 16:02:47 +00:00
|
|
|
import { requireModule, resolveModule } from '../internal/cjs'
|
2023-03-10 14:55:01 +00:00
|
|
|
import { importModule } from '../internal/esm'
|
2021-11-21 16:14:46 +00:00
|
|
|
import { resolveAlias } from '../resolve'
|
2021-04-02 11:47:01 +00:00
|
|
|
|
2021-04-15 18:49:29 +00:00
|
|
|
/** Installs a module on a Nuxt instance. */
|
2022-02-08 19:09:44 +00:00
|
|
|
export async function installModule (moduleToInstall: string | NuxtModule, _inlineOptions?: any, _nuxt?: Nuxt) {
|
|
|
|
const nuxt = useNuxt()
|
|
|
|
const { nuxtModule, inlineOptions } = await normalizeModule(moduleToInstall, _inlineOptions)
|
|
|
|
|
|
|
|
// Call module
|
2023-04-03 12:04:56 +00:00
|
|
|
const res = (
|
|
|
|
isNuxt2()
|
|
|
|
// @ts-expect-error Nuxt 2 `moduleContainer` is not typed
|
|
|
|
? await nuxtModule.call(nuxt.moduleContainer, inlineOptions, nuxt)
|
|
|
|
: await nuxtModule(inlineOptions, nuxt)
|
|
|
|
) ?? {}
|
2023-01-31 16:44:19 +00:00
|
|
|
if (res === false /* setup aborted */) {
|
|
|
|
return
|
|
|
|
}
|
2022-02-08 19:09:44 +00:00
|
|
|
|
2022-11-15 14:55:45 +00:00
|
|
|
if (typeof moduleToInstall === 'string') {
|
|
|
|
nuxt.options.build.transpile.push(moduleToInstall)
|
|
|
|
}
|
|
|
|
|
2022-02-08 19:09:44 +00:00
|
|
|
nuxt.options._installedModules = nuxt.options._installedModules || []
|
|
|
|
nuxt.options._installedModules.push({
|
|
|
|
meta: await nuxtModule.getMeta?.(),
|
2023-03-10 11:30:22 +00:00
|
|
|
timings: res.timings,
|
2022-02-08 19:09:44 +00:00
|
|
|
entryPath: typeof moduleToInstall === 'string' ? resolveAlias(moduleToInstall) : undefined
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Internal ---
|
|
|
|
|
|
|
|
async function normalizeModule (nuxtModule: string | NuxtModule, inlineOptions?: any) {
|
|
|
|
const nuxt = useNuxt()
|
|
|
|
|
2021-12-21 13:57:26 +00:00
|
|
|
// Import if input is string
|
|
|
|
if (typeof nuxtModule === 'string') {
|
2022-02-07 21:00:20 +00:00
|
|
|
const _src = resolveModule(resolveAlias(nuxtModule), { paths: nuxt.options.modulesDir })
|
2021-10-21 17:02:26 +00:00
|
|
|
// TODO: also check with type: 'module' in closest `package.json`
|
2021-12-21 13:57:26 +00:00
|
|
|
const isESM = _src.endsWith('.mjs')
|
2022-10-15 11:35:01 +00:00
|
|
|
|
|
|
|
try {
|
2023-03-10 14:55:01 +00:00
|
|
|
nuxtModule = isESM ? await importModule(_src, nuxt.options.rootDir) : requireModule(_src)
|
2022-10-15 11:35:01 +00:00
|
|
|
} catch (error: unknown) {
|
|
|
|
console.error(`Error while requiring module \`${nuxtModule}\`: ${error}`)
|
|
|
|
throw error
|
|
|
|
}
|
2021-04-02 11:47:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 13:57:26 +00:00
|
|
|
// Throw error if input is not a function
|
|
|
|
if (typeof nuxtModule !== 'function') {
|
|
|
|
throw new TypeError('Nuxt module should be a function: ' + nuxtModule)
|
2021-04-02 11:47:01 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 19:09:44 +00:00
|
|
|
return { nuxtModule, inlineOptions } as { nuxtModule: NuxtModule<any>, inlineOptions: undefined | Record<string, any> }
|
2021-04-02 11:47:01 +00:00
|
|
|
}
|