2023-04-19 18:05:46 +00:00
|
|
|
import { lstatSync } from 'node:fs'
|
2022-02-08 19:09:44 +00:00
|
|
|
import type { Nuxt, NuxtModule } from '@nuxt/schema'
|
2023-05-20 22:10:52 +00:00
|
|
|
import { dirname, isAbsolute, normalize, resolve } from 'pathe'
|
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-05-10 12:10:23 +00:00
|
|
|
import { requireModule } 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') {
|
2023-04-19 18:05:46 +00:00
|
|
|
nuxt.options.build.transpile.push(normalizeModuleTranspilePath(moduleToInstall))
|
2022-11-15 14:55:45 +00:00
|
|
|
}
|
|
|
|
|
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 ---
|
|
|
|
|
2023-04-19 18:05:46 +00:00
|
|
|
export const normalizeModuleTranspilePath = (p: string) => {
|
|
|
|
try {
|
|
|
|
// we need to target directories instead of module file paths themselves
|
|
|
|
// /home/user/project/node_modules/module/index.js -> /home/user/project/node_modules/module
|
|
|
|
p = isAbsolute(p) && lstatSync(p).isFile() ? dirname(p) : p
|
|
|
|
} catch (e) {
|
|
|
|
// maybe the path is absolute but does not exist, allow this to bubble up
|
|
|
|
}
|
|
|
|
return p.split('node_modules/').pop() as string
|
|
|
|
}
|
|
|
|
|
2022-02-08 19:09:44 +00:00
|
|
|
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') {
|
2023-05-20 22:10:52 +00:00
|
|
|
let src = resolveAlias(nuxtModule)
|
|
|
|
if (src.match(/^\.{1,2}\//)) {
|
|
|
|
src = resolve(nuxt.options.rootDir, src)
|
|
|
|
}
|
|
|
|
src = normalize(src)
|
2022-10-15 11:35:01 +00:00
|
|
|
try {
|
2023-05-10 12:10:23 +00:00
|
|
|
// Prefer ESM resolution if possible
|
|
|
|
nuxtModule = await importModule(src, nuxt.options.modulesDir).catch(() => null) ?? requireModule(src, { paths: nuxt.options.modulesDir })
|
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
|
|
|
}
|