2023-06-06 22:36:35 +00:00
|
|
|
import { existsSync, promises as fsp, lstatSync } from 'node:fs'
|
|
|
|
import type { ModuleMeta, Nuxt, NuxtModule } from '@nuxt/schema'
|
2023-12-20 14:42:42 +00:00
|
|
|
import { dirname, isAbsolute, join, resolve } from 'pathe'
|
2023-06-06 22:36:35 +00:00
|
|
|
import { defu } from 'defu'
|
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'
|
2023-05-20 22:29:32 +00:00
|
|
|
import { resolveAlias, resolvePath } from '../resolve'
|
2023-09-19 21:26:15 +00:00
|
|
|
import { logger } from '../logger'
|
2021-04-02 11:47:01 +00:00
|
|
|
|
2023-12-20 14:42:42 +00:00
|
|
|
const NODE_MODULES_RE = /[/\\]node_modules[/\\]/
|
|
|
|
|
2021-04-15 18:49:29 +00:00
|
|
|
/** Installs a module on a Nuxt instance. */
|
2023-06-06 22:36:35 +00:00
|
|
|
export async function installModule (moduleToInstall: string | NuxtModule, inlineOptions?: any, nuxt: Nuxt = useNuxt()) {
|
|
|
|
const { nuxtModule, buildTimeModuleMeta } = await loadNuxtModuleInstance(moduleToInstall, nuxt)
|
2022-02-08 19:09:44 +00:00
|
|
|
|
2023-12-20 14:42:42 +00:00
|
|
|
const localLayerModuleDirs = new Set<string>()
|
|
|
|
for (const l of nuxt.options._layers) {
|
|
|
|
const srcDir = l.config.srcDir || l.cwd
|
|
|
|
if (!NODE_MODULES_RE.test(srcDir)) {
|
|
|
|
localLayerModuleDirs.add(resolve(srcDir, l.config?.dir?.modules || 'modules'))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-08 19:09:44 +00:00
|
|
|
// 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))
|
2023-08-07 22:05:29 +00:00
|
|
|
const directory = getDirectory(moduleToInstall)
|
2023-12-20 14:42:42 +00:00
|
|
|
if (directory !== moduleToInstall && !localLayerModuleDirs.has(directory)) {
|
2024-03-06 14:27:17 +00:00
|
|
|
nuxt.options.modulesDir.push(resolve(directory, 'node_modules'))
|
2023-08-07 22:05:29 +00:00
|
|
|
}
|
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({
|
2023-06-06 22:36:35 +00:00
|
|
|
meta: defu(await nuxtModule.getMeta?.(), buildTimeModuleMeta),
|
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-10-22 08:08:02 +00:00
|
|
|
export function getDirectory (p: string) {
|
2023-04-19 18:05:46 +00:00
|
|
|
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
|
2023-08-07 22:05:29 +00:00
|
|
|
return isAbsolute(p) && lstatSync(p).isFile() ? dirname(p) : p
|
2023-04-19 18:05:46 +00:00
|
|
|
} catch (e) {
|
|
|
|
// maybe the path is absolute but does not exist, allow this to bubble up
|
|
|
|
}
|
2023-08-07 22:05:29 +00:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
export const normalizeModuleTranspilePath = (p: string) => {
|
|
|
|
return getDirectory(p).split('node_modules/').pop() as string
|
2023-04-19 18:05:46 +00:00
|
|
|
}
|
|
|
|
|
2023-06-06 22:36:35 +00:00
|
|
|
export async function loadNuxtModuleInstance (nuxtModule: string | NuxtModule, nuxt: Nuxt = useNuxt()) {
|
|
|
|
let buildTimeModuleMeta: ModuleMeta = {}
|
2021-12-21 13:57:26 +00:00
|
|
|
// Import if input is string
|
|
|
|
if (typeof nuxtModule === 'string') {
|
2023-12-12 17:55:21 +00:00
|
|
|
const paths = [join(nuxtModule, 'nuxt'), join(nuxtModule, 'module'), nuxtModule]
|
|
|
|
let error: unknown
|
|
|
|
for (const path of paths) {
|
|
|
|
try {
|
2024-03-09 06:30:02 +00:00
|
|
|
const src = await resolvePath(path)
|
|
|
|
// Prefer ESM resolution if possible
|
2023-12-12 17:55:21 +00:00
|
|
|
nuxtModule = await importModule(src, nuxt.options.modulesDir).catch(() => null) ?? requireModule(src, { paths: nuxt.options.modulesDir })
|
|
|
|
|
|
|
|
// nuxt-module-builder generates a module.json with metadata including the version
|
2023-12-14 11:04:20 +00:00
|
|
|
const moduleMetadataPath = join(dirname(src), 'module.json')
|
|
|
|
if (existsSync(moduleMetadataPath)) {
|
|
|
|
buildTimeModuleMeta = JSON.parse(await fsp.readFile(moduleMetadataPath, 'utf-8'))
|
2023-12-12 17:55:21 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
} catch (_err: unknown) {
|
|
|
|
error = _err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2024-01-01 09:14:28 +00:00
|
|
|
if (typeof nuxtModule !== 'function' && error) {
|
2023-09-19 21:26:15 +00:00
|
|
|
logger.error(`Error while requiring module \`${nuxtModule}\`: ${error}`)
|
2022-10-15 11:35:01 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-06-06 22:36:35 +00:00
|
|
|
return { nuxtModule, buildTimeModuleMeta } as { nuxtModule: NuxtModule<any>, buildTimeModuleMeta: ModuleMeta }
|
2021-04-02 11:47:01 +00:00
|
|
|
}
|