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", "scule": "^0.2.1",
"std-env": "^2.3.0", "std-env": "^2.3.0",
"ufo": "^0.7.5", "ufo": "^0.7.5",
"unctx": "^0.0.3", "unctx": "^1.0.1",
"untyped": "^0.2.5", "untyped": "^0.2.5",
"upath": "^2.0.1" "upath": "^2.0.1"
} }

View File

@ -17,8 +17,7 @@ export function defineNuxtModule<OptionsT extends ModuleOptions> (input: NuxtMod
// Resolve function // Resolve function
if (typeof input === 'function') { if (typeof input === 'function') {
const fn = input mod = input(nuxt)
mod = nuxtCtx.call(nuxt, () => fn(nuxt))
} else { } else {
mod = input mod = input
} }
@ -38,8 +37,19 @@ export function defineNuxtModule<OptionsT extends ModuleOptions> (input: NuxtMod
const userOptions = defu(inlineOptions, nuxt.options[configKey]) as OptionsT const userOptions = defu(inlineOptions, nuxt.options[configKey]) as OptionsT
const resolvedOptions = applyDefaults(mod.defaults as any, userOptions) 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 // Call setup
return nuxtCtx.call(nuxt, () => mod.setup.call(null, resolvedOptions, nuxt)) return mod.setup.call(null, resolvedOptions, nuxt)
} }
wrappedModule.meta = mod wrappedModule.meta = mod

View File

@ -1,6 +1,5 @@
import { resolveModule, requireModule } from '../utils/cjs' import { resolveModule, requireModule } from '../utils/cjs'
import { resolveAlias } from '../utils/resolve' import { resolveAlias } from '../utils/resolve'
import { nuxtCtx } from '../nuxt'
import type { LegacyNuxtModule, NuxtModule, ModuleMeta, ModuleInstallOptions, ModuleOptions, ModuleSrc } from '../types/module' import type { LegacyNuxtModule, NuxtModule, ModuleMeta, ModuleInstallOptions, ModuleOptions, ModuleSrc } from '../types/module'
import type { Nuxt } from '../types/nuxt' import type { Nuxt } from '../types/nuxt'
import { defineNuxtModule } from './define' import { defineNuxtModule } from './define'
@ -59,5 +58,5 @@ export async function installModule (nuxt: Nuxt, installOpts: ModuleInstallOptio
// Execute in legacy container // Execute in legacy container
const container = new ModuleContainer(nuxt) 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 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' import { initNitro } from './nitro'
export function createNuxt (options: NuxtOptions): Nuxt { export function createNuxt (options: NuxtOptions): Nuxt {
@ -21,6 +21,10 @@ async function initNuxt (nuxt: Nuxt) {
// Register user hooks // Register user hooks
nuxt.hooks.addHooks(nuxt.options.hooks) nuxt.hooks.addHooks(nuxt.options.hooks)
// Set nuxt instance for useNuxt
nuxtCtx.set(nuxt)
nuxt.hook('close', () => nuxtCtx.unset())
// Init nitro // Init nitro
await initNitro(nuxt) await initNitro(nuxt)

View File

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