2022-02-08 19:09:44 +00:00
|
|
|
import type { Nuxt, NuxtModule } from '@nuxt/schema'
|
2021-12-21 13:57:26 +00:00
|
|
|
import { useNuxt } from '../context'
|
2021-11-21 16:14:46 +00:00
|
|
|
import { resolveModule, requireModule, importModule } from '../internal/cjs'
|
|
|
|
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-01-31 16:44:19 +00:00
|
|
|
const res = await nuxtModule(inlineOptions, nuxt)
|
|
|
|
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?.(),
|
|
|
|
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 {
|
|
|
|
nuxtModule = isESM ? await importModule(_src) : requireModule(_src)
|
|
|
|
} 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
|
|
|
}
|