2021-04-02 11:47:01 +00:00
|
|
|
import { getContext } from 'unctx'
|
2021-11-04 12:33:33 +00:00
|
|
|
import { readPackageJSON, resolvePackageJSON } from 'pkg-types'
|
|
|
|
import { importModule, tryImportModule, RequireModuleOptions } from './utils/cjs'
|
2021-04-02 11:47:01 +00:00
|
|
|
import type { Nuxt } from './types/nuxt'
|
|
|
|
import type { NuxtConfig } from './types/config'
|
2021-04-15 19:17:44 +00:00
|
|
|
import type { LoadNuxtConfigOptions } from './config/load'
|
2021-04-02 11:47:01 +00:00
|
|
|
|
2021-04-15 18:49:29 +00:00
|
|
|
/** Direct access to the Nuxt context - see https://github.com/unjs/unctx. */
|
2021-04-02 11:47:01 +00:00
|
|
|
export const nuxtCtx = getContext<Nuxt>('nuxt')
|
2021-04-15 19:17:44 +00:00
|
|
|
|
2021-04-15 18:49:29 +00:00
|
|
|
/**
|
|
|
|
* Get access to Nuxt (if run within the Nuxt context) - see https://github.com/unjs/unctx.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```js
|
|
|
|
* const nuxt = useNuxt()
|
|
|
|
* ```
|
|
|
|
*/
|
2021-04-02 11:47:01 +00:00
|
|
|
export const useNuxt = nuxtCtx.use
|
|
|
|
|
2021-04-15 19:17:44 +00:00
|
|
|
export interface LoadNuxtOptions extends LoadNuxtConfigOptions {
|
|
|
|
rootDir: string
|
|
|
|
dev?: boolean
|
|
|
|
config?: NuxtConfig
|
|
|
|
configFile?: string
|
|
|
|
ready?: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
|
2021-10-06 08:23:40 +00:00
|
|
|
const resolveOpts: RequireModuleOptions = { paths: opts.rootDir }
|
2021-04-23 20:36:43 +00:00
|
|
|
|
2021-11-04 12:33:33 +00:00
|
|
|
const nearestNuxtPkg = await Promise.all(['nuxt3', 'nuxt-edge', 'nuxt']
|
|
|
|
.map(pkg => resolvePackageJSON(pkg, { url: opts.rootDir }).catch(() => null)))
|
|
|
|
.then(r => r.filter(Boolean).sort((a, b) => b.length - a.length)[0])
|
|
|
|
if (!nearestNuxtPkg) {
|
|
|
|
throw new Error(`Cannot find any nuxt version from ${opts.rootDir}`)
|
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) {
|
2021-10-02 16:01:17 +00:00
|
|
|
const { loadNuxt } = await importModule('nuxt3', 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({
|
|
|
|
rootDir: opts.rootDir,
|
|
|
|
for: opts.dev ? 'dev' : 'build',
|
|
|
|
configOverrides: opts.config,
|
2021-09-25 08:18:01 +00:00
|
|
|
ready: opts.ready,
|
|
|
|
envConfig: opts.envConfig
|
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) {
|
2021-10-02 16:01:17 +00:00
|
|
|
const { build } = await importModule('nuxt3', resolveOpts)
|
2021-04-15 19:17:44 +00:00
|
|
|
return build(nuxt)
|
|
|
|
}
|
|
|
|
|
2021-09-05 21:21:33 +00:00
|
|
|
// Nuxt 2
|
2021-10-06 08:23:40 +00:00
|
|
|
const { build } = await tryImportModule('nuxt-edge', resolveOpts) || await tryImportModule('nuxt', resolveOpts)
|
2021-04-15 19:17:44 +00:00
|
|
|
return build(nuxt)
|
|
|
|
}
|