2023-03-10 14:55:01 +00:00
|
|
|
import { pathToFileURL } from 'node:url'
|
2021-11-04 12:33:33 +00:00
|
|
|
import { readPackageJSON, resolvePackageJSON } from 'pkg-types'
|
2022-03-17 20:10:12 +00:00
|
|
|
import type { Nuxt } from '@nuxt/schema'
|
2023-03-10 14:55:01 +00:00
|
|
|
import { importModule, tryImportModule } from '../internal/esm'
|
2021-11-21 16:14:46 +00:00
|
|
|
import type { LoadNuxtConfigOptions } from './config'
|
2021-04-02 11:47:01 +00:00
|
|
|
|
2021-04-15 19:17:44 +00:00
|
|
|
export interface LoadNuxtOptions extends LoadNuxtConfigOptions {
|
2022-03-17 20:10:12 +00:00
|
|
|
/** Load nuxt with development mode */
|
2021-04-15 19:17:44 +00:00
|
|
|
dev?: boolean
|
2022-03-17 20:10:12 +00:00
|
|
|
|
|
|
|
/** Use lazy initialization of nuxt if set to false */
|
2021-04-15 19:17:44 +00:00
|
|
|
ready?: boolean
|
2022-03-17 20:10:12 +00:00
|
|
|
|
|
|
|
/** @deprecated Use cwd option */
|
|
|
|
rootDir?: LoadNuxtConfigOptions['cwd']
|
|
|
|
|
|
|
|
/** @deprecated use overrides option */
|
|
|
|
config?: LoadNuxtConfigOptions['overrides']
|
2021-04-15 19:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
|
2022-03-17 20:10:12 +00:00
|
|
|
// Backward compatibility
|
|
|
|
opts.cwd = opts.cwd || opts.rootDir
|
|
|
|
opts.overrides = opts.overrides || opts.config || {}
|
|
|
|
|
|
|
|
// Apply dev as config override
|
|
|
|
opts.overrides.dev = !!opts.dev
|
2021-04-23 20:36:43 +00:00
|
|
|
|
2023-10-12 14:17:38 +00:00
|
|
|
const nearestNuxtPkg = await Promise.all(['nuxt-nightly', 'nuxt3', 'nuxt', 'nuxt-edge']
|
2022-03-17 20:10:12 +00:00
|
|
|
.map(pkg => resolvePackageJSON(pkg, { url: opts.cwd }).catch(() => null)))
|
2022-08-22 10:12:02 +00:00
|
|
|
.then(r => (r.filter(Boolean) as string[]).sort((a, b) => b.length - a.length)[0])
|
2021-11-04 12:33:33 +00:00
|
|
|
if (!nearestNuxtPkg) {
|
2022-03-17 20:10:12 +00:00
|
|
|
throw new Error(`Cannot find any nuxt version from ${opts.cwd}`)
|
2021-04-23 20:36:43 +00:00
|
|
|
}
|
2021-11-04 12:33:33 +00:00
|
|
|
const pkg = await readPackageJSON(nearestNuxtPkg)
|
2024-04-05 18:08:32 +00:00
|
|
|
const majorVersion = pkg.version ? Number.parseInt(pkg.version.split('.')[0]) : ''
|
2021-04-23 20:36:43 +00:00
|
|
|
|
2023-03-10 14:55:01 +00:00
|
|
|
const rootDir = pathToFileURL(opts.cwd || process.cwd()).href
|
|
|
|
|
2021-04-15 19:17:44 +00:00
|
|
|
// Nuxt 3
|
2021-11-04 12:33:33 +00:00
|
|
|
if (majorVersion === 3) {
|
2023-03-10 14:55:01 +00:00
|
|
|
const { loadNuxt } = await importModule((pkg as any)._name || pkg.name, rootDir)
|
2021-04-15 19:17:44 +00:00
|
|
|
const nuxt = await loadNuxt(opts)
|
|
|
|
return nuxt
|
|
|
|
}
|
|
|
|
|
2021-09-05 21:21:33 +00:00
|
|
|
// Nuxt 2
|
2023-03-10 14:55:01 +00:00
|
|
|
const { loadNuxt } = await tryImportModule('nuxt-edge', rootDir) || await importModule('nuxt', rootDir)
|
2021-04-15 19:17:44 +00:00
|
|
|
const nuxt = await loadNuxt({
|
2022-03-17 20:10:12 +00:00
|
|
|
rootDir: opts.cwd,
|
2021-04-15 19:17:44 +00:00
|
|
|
for: opts.dev ? 'dev' : 'build',
|
2022-03-17 20:10:12 +00:00
|
|
|
configOverrides: opts.overrides,
|
2021-09-25 08:18:01 +00:00
|
|
|
ready: opts.ready,
|
2024-04-05 18:08:32 +00:00
|
|
|
envConfig: opts.dotenv, // TODO: Backward format conversion
|
2021-04-15 19:17:44 +00:00
|
|
|
})
|
2021-11-04 12:33:33 +00:00
|
|
|
|
2023-10-02 12:42:34 +00:00
|
|
|
// Mock new hookable methods
|
|
|
|
nuxt.removeHook ||= nuxt.clearHook.bind(nuxt)
|
|
|
|
nuxt.removeAllHooks ||= nuxt.clearHooks.bind(nuxt)
|
|
|
|
nuxt.hookOnce ||= (name: string, fn: (...args: any[]) => any, ...hookArgs: any[]) => {
|
|
|
|
const unsub = nuxt.hook(name, (...args: any[]) => {
|
|
|
|
unsub()
|
|
|
|
return fn(...args)
|
|
|
|
}, ...hookArgs)
|
|
|
|
return unsub
|
|
|
|
}
|
|
|
|
// https://github.com/nuxt/nuxt/tree/main/packages/kit/src/module/define.ts#L111-L113
|
|
|
|
nuxt.hooks ||= nuxt
|
|
|
|
|
2021-04-15 19:17:44 +00:00
|
|
|
return nuxt as Nuxt
|
|
|
|
}
|
|
|
|
|
2021-10-02 16:01:17 +00:00
|
|
|
export async function buildNuxt (nuxt: Nuxt): Promise<any> {
|
2023-03-10 14:55:01 +00:00
|
|
|
const rootDir = pathToFileURL(nuxt.options.rootDir).href
|
2021-04-28 15:51:42 +00:00
|
|
|
|
2021-04-15 19:17:44 +00:00
|
|
|
// Nuxt 3
|
|
|
|
if (nuxt.options._majorVersion === 3) {
|
2023-10-12 14:17:38 +00:00
|
|
|
const { build } = await tryImportModule('nuxt-nightly', rootDir) || await tryImportModule('nuxt3', rootDir) || await importModule('nuxt', rootDir)
|
2021-04-15 19:17:44 +00:00
|
|
|
return build(nuxt)
|
|
|
|
}
|
|
|
|
|
2021-09-05 21:21:33 +00:00
|
|
|
// Nuxt 2
|
2023-03-10 14:55:01 +00:00
|
|
|
const { build } = await tryImportModule('nuxt-edge', rootDir) || await importModule('nuxt', rootDir)
|
2021-04-15 19:17:44 +00:00
|
|
|
return build(nuxt)
|
|
|
|
}
|