From 7a034605844c974b001489aab476b53b508a1a7d Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Fri, 27 Aug 2021 14:51:40 +0200 Subject: [PATCH] chore(nitro, kit, nuxt3): add nitro hook types and upgrade `hookable@5` (#458) --- packages/kit/package.json | 1 + packages/kit/src/types/hooks.ts | 6 ++---- packages/kit/src/types/nuxt.ts | 15 +++++---------- packages/nitro/package.json | 2 +- packages/nitro/src/compat.ts | 10 +++++++--- packages/nitro/src/context.ts | 21 +++++++++++++++------ packages/nitro/src/utils/index.ts | 4 ++-- packages/nuxt3/package.json | 2 +- packages/nuxt3/src/app/nuxt.ts | 15 +++------------ packages/nuxt3/src/core/nuxt.ts | 7 ++++--- yarn.lock | 13 +++++++------ 11 files changed, 48 insertions(+), 48 deletions(-) diff --git a/packages/kit/package.json b/packages/kit/package.json index e6f7f882e5..3ed966a385 100644 --- a/packages/kit/package.json +++ b/packages/kit/package.json @@ -22,6 +22,7 @@ "dotenv": "^10.0.0", "globby": "^11.0.4", "hash-sum": "^2.0.0", + "hookable": "^5.0.0-2", "jiti": "^1.11.0", "rc9": "^1.2.0", "scule": "^0.2.1", diff --git a/packages/kit/src/types/hooks.ts b/packages/kit/src/types/hooks.ts index 83adf39f3f..3fedabef6f 100644 --- a/packages/kit/src/types/hooks.ts +++ b/packages/kit/src/types/hooks.ts @@ -1,4 +1,5 @@ import type { IncomingMessage, ServerResponse } from 'http' +import type { HookCallback } from 'hookable' import type { Compiler, Configuration, Stats } from 'webpack' import type { NuxtConfig, NuxtOptions } from '..' import type { ModuleContainer } from '../module/container' @@ -35,10 +36,7 @@ type RenderResult = { // https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html export type TSReference = { types: string } | { path: string } -export interface NuxtHooks { - // Don't break usage of untyped hooks - [key: string]: (...args: any[]) => HookResult - +export interface NuxtHooks extends Record { // nuxt3 'app:resolve': (app: NuxtApp) => HookResult 'app:templates': (app: NuxtApp) => HookResult diff --git a/packages/kit/src/types/nuxt.ts b/packages/kit/src/types/nuxt.ts index 68b803df23..c139ed11d7 100644 --- a/packages/kit/src/types/nuxt.ts +++ b/packages/kit/src/types/nuxt.ts @@ -1,20 +1,15 @@ -import { NuxtHookName, NuxtHooks } from './hooks' -import { NuxtOptions } from './config' +import type { Hookable } from 'hookable' +import type { NuxtHooks } from './hooks' +import type { NuxtOptions } from './config' export interface Nuxt { /** The resolved Nuxt configuration. */ options: NuxtOptions - hooks: { - /** Register a function to be run when the named Nuxt hook is called. */ - hook(hookName: Hook, callback: NuxtHooks[Hook]): void | Promise - /** Run all Nuxt hooks that have been registered against the hook name. */ - callHook(hookName: Hook, ...args: Parameters): ReturnType - /** Add all hooks in the object passed in. */ - addHooks(hooks: Partial): void - } + hooks: Hookable hook: Nuxt['hooks']['hook'] callHook: Nuxt['hooks']['callHook'] + addHooks: Nuxt['hooks']['addHooks'] ready: () => Promise close: () => Promise diff --git a/packages/nitro/package.json b/packages/nitro/package.json index 597e478286..ef5c82fa3c 100644 --- a/packages/nitro/package.json +++ b/packages/nitro/package.json @@ -45,7 +45,7 @@ "gzip-size": "^6.0.0", "h3": "^0.3.0", "hasha": "^5.2.2", - "hookable": "^4.4.1", + "hookable": "^5.0.0-2", "http-proxy": "^1.18.1", "is-primitive": "^3.0.1", "jiti": "^1.11.0", diff --git a/packages/nitro/src/compat.ts b/packages/nitro/src/compat.ts index eddcc8825a..d53c8b141b 100644 --- a/packages/nitro/src/compat.ts +++ b/packages/nitro/src/compat.ts @@ -1,13 +1,14 @@ import fetch from 'node-fetch' import { resolve } from 'upath' import { move, readFile, writeFile } from 'fs-extra' +import type { ModuleContainer } from '@nuxt/kit' import { build, generate, prepare } from './build' import { getNitroContext, NitroContext } from './context' import { createDevServer } from './server/dev' import { wpfs } from './utils/wpfs' import { resolveMiddleware } from './server/middleware' -export default function nuxt2CompatModule () { +export default function nuxt2CompatModule (this: ModuleContainer) { const { nuxt } = this // Ensure we're not just building with 'static' target @@ -16,7 +17,9 @@ export default function nuxt2CompatModule () { } // Disable loading-screen + // @ts-ignore nuxt.options.build.loadingScreen = false + // @ts-ignore nuxt.options.build.indicator = false // Create contexts @@ -40,8 +43,6 @@ export default function nuxt2CompatModule () { nuxt.addHooks(nitroDevContext.nuxtHooks) nuxt.hook('close', () => nitroDevContext._internal.hooks.callHook('close')) nitroDevContext._internal.hooks.hook('nitro:document', template => nuxt.callHook('nitro:document', template)) - nitroDevContext._internal.hooks.hook('renderLoading', - (req, res) => nuxt.callHook('server:nuxt:renderLoading', req, res)) // Expose process.env.NITRO_PRESET nuxt.options.env.NITRO_PRESET = nitroContext.preset @@ -78,6 +79,8 @@ export default function nuxt2CompatModule () { // Fix module resolution nuxt.hook('webpack:config', (configs) => { for (const config of configs) { + // We use only object form of alias in base config + if (Array.isArray(config.resolve.alias)) { return } config.resolve.alias.ufo = 'ufo/dist/index.mjs' config.resolve.alias.ohmyfetch = 'ohmyfetch/dist/index.mjs' } @@ -111,6 +114,7 @@ export default function nuxt2CompatModule () { }) // nuxt build/dev + // @ts-ignore nuxt.options.build._minifyServer = false nuxt.options.build.standalone = false nuxt.hook('build:done', async () => { diff --git a/packages/nitro/src/context.ts b/packages/nitro/src/context.ts index f6c18bfe11..acf5ec6668 100644 --- a/packages/nitro/src/context.ts +++ b/packages/nitro/src/context.ts @@ -1,8 +1,10 @@ +/* eslint-disable no-use-before-define */ + import { resolve, dirname } from 'upath' import defu from 'defu' -import Hookable, { configHooksT } from 'hookable' +import { createHooks, Hookable, NestedHooks } from 'hookable' import type { Preset } from 'unenv' -import type { NuxtOptions } from '@nuxt/kit' +import type { NuxtHooks, NuxtOptions } from '@nuxt/kit' import { tryImport, resolvePath, detectTarget, extendPreset } from './utils' import * as PRESETS from './presets' import type { NodeExternalsOptions } from './rollup/plugins/externals' @@ -11,6 +13,13 @@ import type { AssetOptions } from './rollup/plugins/assets' import type { ServerMiddleware } from './server/middleware' import type { RollupConfig } from './rollup/config' +export interface NitroHooks { + 'nitro:document': (htmlTemplate: { src: string, contents: string, dst: string, compiled: string }) => void + 'nitro:rollup:before': (context: NitroContext) => void | Promise + 'nitro:compiled': (context: NitroContext) => void + 'close': () => void +} + export interface NitroContext { timing: boolean inlineDynamicImports: boolean @@ -27,8 +36,8 @@ export interface NitroContext { serveStatic: boolean middleware: ServerMiddleware[] scannedMiddleware: ServerMiddleware[] - hooks: configHooksT - nuxtHooks: configHooksT + hooks: NestedHooks + nuxtHooks: NestedHooks ignore: string[] env: Preset vfs: Record @@ -59,7 +68,7 @@ export interface NitroContext { } _internal: { runtimeDir: string - hooks: Hookable + hooks: Hookable } } @@ -124,7 +133,7 @@ export function getNitroContext (nuxtOptions: NuxtOptions, input: NitroInput): N }, _internal: { runtimeDir: resolve(dirname(require.resolve('@nuxt/nitro')), 'runtime'), - hooks: new Hookable() + hooks: createHooks() } } diff --git a/packages/nitro/src/utils/index.ts b/packages/nitro/src/utils/index.ts index a9a7aac0ce..9a8d8749aa 100644 --- a/packages/nitro/src/utils/index.ts +++ b/packages/nitro/src/utils/index.ts @@ -2,7 +2,7 @@ import { relative, dirname, resolve } from 'upath' import fse from 'fs-extra' import jiti from 'jiti' import defu from 'defu' -import Hookable from 'hookable' +import { mergeHooks } from 'hookable' import consola from 'consola' import chalk from 'chalk' import { get } from 'dot-prop' @@ -97,7 +97,7 @@ export function extendPreset (base: NitroPreset, preset: NitroPreset): NitroPres base = base(config) } return defu({ - hooks: Hookable.mergeHooks(base.hooks, preset.hooks) + hooks: mergeHooks(base.hooks, preset.hooks) }, preset, base) } } diff --git a/packages/nuxt3/package.json b/packages/nuxt3/package.json index d0df667379..bf5da41dd7 100644 --- a/packages/nuxt3/package.json +++ b/packages/nuxt3/package.json @@ -33,7 +33,7 @@ "fs-extra": "^10.0.0", "globby": "^11.0.4", "hash-sum": "^2.0.0", - "hookable": "^4.4.1", + "hookable": "^5.0.0-2", "ignore": "^5.1.8", "lodash": "^4.17.21", "nuxi": "^0.10.0", diff --git a/packages/nuxt3/src/app/nuxt.ts b/packages/nuxt3/src/app/nuxt.ts index 8349d8fea3..28f78ad5f2 100644 --- a/packages/nuxt3/src/app/nuxt.ts +++ b/packages/nuxt3/src/app/nuxt.ts @@ -1,6 +1,6 @@ import { getCurrentInstance } from 'vue' import type { App, VNode } from 'vue' -import Hookable from 'hookable' +import { createHooks, Hookable } from 'hookable' import { defineGetter } from './utils' import { legacyPlugin, LegacyContext } from './legacy' @@ -22,21 +22,12 @@ export interface RuntimeNuxtHooks { 'page:start': (Component?: VNode) => HookResult 'page:finish': (Component?: VNode) => HookResult } -type NuxtAppHookName = keyof RuntimeNuxtHooks export interface Nuxt { app: App globalName: string - hooks: { - /** Register a function to be run when the named Nuxt hook is called. */ - hook (hookName: Hook, callback: RuntimeNuxtHooks[Hook]): HookResult - hookOnce (hookName: Hook, callback: RuntimeNuxtHooks[Hook]): HookResult - /** Run all Nuxt hooks that have been registered against the hook name. */ - callHook (hookName: Hook, ...args: Parameters): ReturnType - /** Add all hooks in the object passed in. */ - addHooks (hooks: Partial): void - } + hooks: Hookable hook: Nuxt['hooks']['hook'] callHook: Nuxt['hooks']['callHook'] @@ -83,7 +74,7 @@ export function createNuxt (options: CreateOptions) { ...options } as any as Nuxt - nuxt.hooks = new Hookable() + nuxt.hooks = createHooks() nuxt.hook = nuxt.hooks.hook nuxt.callHook = nuxt.hooks.callHook diff --git a/packages/nuxt3/src/core/nuxt.ts b/packages/nuxt3/src/core/nuxt.ts index a075996e25..7344e7566f 100644 --- a/packages/nuxt3/src/core/nuxt.ts +++ b/packages/nuxt3/src/core/nuxt.ts @@ -1,6 +1,6 @@ import { resolve } from 'upath' -import Hookable from 'hookable' -import { loadNuxtConfig, LoadNuxtOptions, Nuxt, NuxtOptions, nuxtCtx, installModule, ModuleContainer } from '@nuxt/kit' +import { createHooks } from 'hookable' +import { loadNuxtConfig, LoadNuxtOptions, Nuxt, NuxtOptions, nuxtCtx, installModule, ModuleContainer, NuxtHooks } from '@nuxt/kit' import pagesModule from '../pages/module' import metaModule from '../meta/module' import componentsModule from '../components/module' @@ -9,12 +9,13 @@ import { distDir, pkgDir } from '../dirs' import { initNitro } from './nitro' export function createNuxt (options: NuxtOptions): Nuxt { - const hooks = new Hookable() as any as Nuxt['hooks'] + const hooks = createHooks() const nuxt: Nuxt = { options, hooks, callHook: hooks.callHook, + addHooks: hooks.addHooks, hook: hooks.hook, ready: () => initNuxt(nuxt), close: () => Promise.resolve(hooks.callHook('close', nuxt)), diff --git a/yarn.lock b/yarn.lock index 9adf921aa7..e837878fac 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1313,6 +1313,7 @@ __metadata: dotenv: ^10.0.0 globby: ^11.0.4 hash-sum: ^2.0.0 + hookable: ^5.0.0-2 jiti: ^1.11.0 rc9: ^1.2.0 scule: ^0.2.1 @@ -1366,7 +1367,7 @@ __metadata: gzip-size: ^6.0.0 h3: ^0.3.0 hasha: ^5.2.2 - hookable: ^4.4.1 + hookable: ^5.0.0-2 http-proxy: ^1.18.1 is-primitive: ^3.0.1 jiti: ^1.11.0 @@ -6248,10 +6249,10 @@ fsevents@~2.3.2: languageName: node linkType: hard -"hookable@npm:^4.4.1": - version: 4.4.1 - resolution: "hookable@npm:4.4.1" - checksum: 6f1cdd047c18a07f59d851186e993c7e15f9a59ae2223ddc8efa02dbe9cfbbf921cab026cf9910beb485512ad293a6693ef60e45eccaf1b0f5902baf521985c4 +"hookable@npm:^5.0.0-2": + version: 5.0.0-2 + resolution: "hookable@npm:5.0.0-2" + checksum: 2545fc3183a0adce4d38d025d534fa6b7b19b3e10684fa12b51a4f181c2c8e7be869b0ce69dfff6f9f10638a793dfe4000f7b7811cdb2ba3c93b631b455cf245 languageName: node linkType: hard @@ -8644,7 +8645,7 @@ fsevents@~2.3.2: fs-extra: ^10.0.0 globby: ^11.0.4 hash-sum: ^2.0.0 - hookable: ^4.4.1 + hookable: ^5.0.0-2 ignore: ^5.1.8 lodash: ^4.17.21 nuxi: ^0.10.0