From debe5a98b0429bf9c13f99e7c568de5d1cbcd123 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Thu, 24 Jun 2021 16:06:16 +0200 Subject: [PATCH] feat(kit, nuxt3): use single nuxt instance (#271) --- packages/kit/package.json | 2 +- packages/kit/src/module/define.ts | 16 +++++++++++++--- packages/kit/src/module/install.ts | 3 +-- packages/nuxt3/src/nuxt.ts | 6 +++++- yarn.lock | 10 +++++----- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/packages/kit/package.json b/packages/kit/package.json index 1676de2ace..88d8bbfc52 100644 --- a/packages/kit/package.json +++ b/packages/kit/package.json @@ -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" } diff --git a/packages/kit/src/module/define.ts b/packages/kit/src/module/define.ts index 5d06d26444..885cd1fc80 100644 --- a/packages/kit/src/module/define.ts +++ b/packages/kit/src/module/define.ts @@ -17,8 +17,7 @@ export function defineNuxtModule (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 (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 diff --git a/packages/kit/src/module/install.ts b/packages/kit/src/module/install.ts index 7bb18b2fd4..2e6fec5a31 100644 --- a/packages/kit/src/module/install.ts +++ b/packages/kit/src/module/install.ts @@ -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) } diff --git a/packages/nuxt3/src/nuxt.ts b/packages/nuxt3/src/nuxt.ts index 617947c4be..ccba627844 100644 --- a/packages/nuxt3/src/nuxt.ts +++ b/packages/nuxt3/src/nuxt.ts @@ -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) diff --git a/yarn.lock b/yarn.lock index d53ae711cf..6545572fc3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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