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'
|
2022-12-11 21:44:52 +00:00
|
|
|
import type { RequireModuleOptions } from '../internal/cjs'
|
|
|
|
import { importModule, tryImportModule } from '../internal/cjs'
|
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 || {}
|
|
|
|
|
|
|
|
const resolveOpts: RequireModuleOptions = { paths: opts.cwd }
|
|
|
|
|
|
|
|
// Apply dev as config override
|
|
|
|
opts.overrides.dev = !!opts.dev
|
2021-04-23 20:36:43 +00:00
|
|
|
|
2022-04-20 08:52:39 +00:00
|
|
|
const nearestNuxtPkg = await Promise.all(['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)
|
|
|
|
const majorVersion = parseInt((pkg.version || '').split('.')[0])
|
2021-04-23 20:36:43 +00:00
|
|
|
|
2021-04-15 19:17:44 +00:00
|
|
|
// Nuxt 3
|
2021-11-04 12:33:33 +00:00
|
|
|
if (majorVersion === 3) {
|
2022-04-20 10:35:09 +00:00
|
|
|
const { loadNuxt } = await importModule((pkg as any)._name || pkg.name, resolveOpts)
|
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
|
2021-10-02 16:01:17 +00:00
|
|
|
const { loadNuxt } = await tryImportModule('nuxt-edge', resolveOpts) || await importModule('nuxt', resolveOpts)
|
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,
|
2023-02-12 19:16:42 +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
|
|
|
|
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> {
|
2021-10-06 08:23:40 +00:00
|
|
|
const resolveOpts: RequireModuleOptions = { paths: nuxt.options.rootDir }
|
2021-04-28 15:51:42 +00:00
|
|
|
|
2021-04-15 19:17:44 +00:00
|
|
|
// Nuxt 3
|
|
|
|
if (nuxt.options._majorVersion === 3) {
|
2022-04-20 08:52:39 +00:00
|
|
|
const { build } = await tryImportModule('nuxt3', resolveOpts) || await importModule('nuxt', resolveOpts)
|
2021-04-15 19:17:44 +00:00
|
|
|
return build(nuxt)
|
|
|
|
}
|
|
|
|
|
2021-09-05 21:21:33 +00:00
|
|
|
// Nuxt 2
|
2022-04-20 08:52:39 +00:00
|
|
|
const { build } = await tryImportModule('nuxt-edge', resolveOpts) || await importModule('nuxt', resolveOpts)
|
2021-04-15 19:17:44 +00:00
|
|
|
return build(nuxt)
|
|
|
|
}
|