feat(kit, nuxt3): use single nuxt instance (#271)

This commit is contained in:
pooya parsa 2021-06-24 16:06:16 +02:00 committed by GitHub
parent a25d7b1617
commit debe5a98b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 12 deletions

View File

@ -27,7 +27,7 @@
"scule": "^0.2.1",
"std-env": "^2.3.0",
"ufo": "^0.7.5",
"unctx": "^0.0.3",
"unctx": "^1.0.1",
"untyped": "^0.2.5",
"upath": "^2.0.1"
}

View File

@ -17,8 +17,7 @@ export function defineNuxtModule<OptionsT extends ModuleOptions> (input: NuxtMod
// Resolve function
if (typeof input === 'function') {
const fn = input
mod = nuxtCtx.call(nuxt, () => fn(nuxt))
mod = input(nuxt)
} else {
mod = input
}
@ -38,8 +37,19 @@ export function defineNuxtModule<OptionsT extends ModuleOptions> (input: NuxtMod
const userOptions = defu(inlineOptions, nuxt.options[configKey]) as OptionsT
const resolvedOptions = applyDefaults(mod.defaults as any, userOptions) as OptionsT
// Ensure nuxt instance exists (nuxt2 compatibility)
if (!nuxtCtx.use()) {
nuxtCtx.set(nuxt)
// @ts-ignore
if (!nuxt.__nuxtkit_close__) {
nuxt.hook('close', () => nuxtCtx.unset())
// @ts-ignore
nuxt.__nuxtkit_close__ = true
}
}
// Call setup
return nuxtCtx.call(nuxt, () => mod.setup.call(null, resolvedOptions, nuxt))
return mod.setup.call(null, resolvedOptions, nuxt)
}
wrappedModule.meta = mod

View File

@ -1,6 +1,5 @@
import { resolveModule, requireModule } from '../utils/cjs'
import { resolveAlias } from '../utils/resolve'
import { nuxtCtx } from '../nuxt'
import type { LegacyNuxtModule, NuxtModule, ModuleMeta, ModuleInstallOptions, ModuleOptions, ModuleSrc } from '../types/module'
import type { Nuxt } from '../types/nuxt'
import { defineNuxtModule } from './define'
@ -59,5 +58,5 @@ export async function installModule (nuxt: Nuxt, installOpts: ModuleInstallOptio
// Execute in legacy container
const container = new ModuleContainer(nuxt)
await nuxtCtx.call(nuxt, () => handler.call(container, options))
await handler.call(container, options)
}

View File

@ -1,5 +1,5 @@
import Hookable from 'hookable'
import { loadNuxtConfig, LoadNuxtOptions, Nuxt, NuxtOptions, installModule, ModuleContainer } from '@nuxt/kit'
import { loadNuxtConfig, LoadNuxtOptions, Nuxt, NuxtOptions, nuxtCtx, installModule, ModuleContainer } from '@nuxt/kit'
import { initNitro } from './nitro'
export function createNuxt (options: NuxtOptions): Nuxt {
@ -21,6 +21,10 @@ async function initNuxt (nuxt: Nuxt) {
// Register user hooks
nuxt.hooks.addHooks(nuxt.options.hooks)
// Set nuxt instance for useNuxt
nuxtCtx.set(nuxt)
nuxt.hook('close', () => nuxtCtx.unset())
// Init nitro
await initNitro(nuxt)

View File

@ -1702,7 +1702,7 @@ __metadata:
std-env: ^2.3.0
ufo: ^0.7.5
unbuild: ^0.3.1
unctx: ^0.0.3
unctx: ^1.0.1
untyped: ^0.2.5
upath: ^2.0.1
languageName: unknown
@ -13133,10 +13133,10 @@ __metadata:
languageName: node
linkType: hard
"unctx@npm:^0.0.3":
version: 0.0.3
resolution: "unctx@npm:0.0.3"
checksum: 0142b5f75a76b93626c35408def8e2aa3f3478ebc503e53770b096ed2fea17b329ef9e616969cb44ea0ec062135c57d2b1914813eedce82982a4c858cdc55096
"unctx@npm:^1.0.1":
version: 1.0.1
resolution: "unctx@npm:1.0.1"
checksum: 614eafb604c42498b7a08e58e18f2802823befc1a297171a60768f8cd0bb56a95ba85bc9ddb575b5bc5262e55f1a42327bf97ff1ffebe1132453154fba1f19fb
languageName: node
linkType: hard