Nuxt/packages/kit/src/loader/nuxt.ts

86 lines
2.9 KiB
TypeScript
Raw Normal View History

import { pathToFileURL } from 'node:url'
import { readPackageJSON, resolvePackageJSON } from 'pkg-types'
import type { Nuxt } from '@nuxt/schema'
import { importModule, tryImportModule } from '../internal/esm'
import type { LoadNuxtConfigOptions } from './config'
2021-04-15 19:17:44 +00:00
export interface LoadNuxtOptions extends LoadNuxtConfigOptions {
/** Load nuxt with development mode */
2021-04-15 19:17:44 +00:00
dev?: boolean
/** Use lazy initialization of nuxt if set to false */
2021-04-15 19:17:44 +00:00
ready?: boolean
/** @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> {
// Backward compatibility
opts.cwd = opts.cwd || opts.rootDir
opts.overrides = opts.overrides || opts.config || {}
// Apply dev as config override
opts.overrides.dev = !!opts.dev
const nearestNuxtPkg = await Promise.all(['nuxt-nightly', 'nuxt3', 'nuxt', 'nuxt-edge']
.map(pkg => resolvePackageJSON(pkg, { url: opts.cwd }).catch(() => null)))
.then(r => (r.filter(Boolean) as string[]).sort((a, b) => b.length - a.length)[0])
if (!nearestNuxtPkg) {
throw new Error(`Cannot find any nuxt version from ${opts.cwd}`)
}
const pkg = await readPackageJSON(nearestNuxtPkg)
const majorVersion = pkg.version ? Number.parseInt(pkg.version.split('.')[0]) : ''
const rootDir = pathToFileURL(opts.cwd || process.cwd()).href
2021-04-15 19:17:44 +00:00
// Nuxt 3
if (majorVersion === 3) {
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
const { loadNuxt } = await tryImportModule('nuxt-edge', rootDir) || await importModule('nuxt', rootDir)
2021-04-15 19:17:44 +00:00
const nuxt = await loadNuxt({
rootDir: opts.cwd,
2021-04-15 19:17:44 +00:00
for: opts.dev ? 'dev' : 'build',
configOverrides: opts.overrides,
ready: opts.ready,
envConfig: opts.dotenv, // TODO: Backward format conversion
2021-04-15 19:17:44 +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
}
export async function buildNuxt (nuxt: Nuxt): Promise<any> {
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) {
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
const { build } = await tryImportModule('nuxt-edge', rootDir) || await importModule('nuxt', rootDir)
2021-04-15 19:17:44 +00:00
return build(nuxt)
}