chore(nitro, kit, nuxt3): add nitro hook types and upgrade hookable@5 (#458)

This commit is contained in:
Daniel Roe 2021-08-27 14:51:40 +02:00 committed by GitHub
parent 16b769d6b9
commit 7a03460584
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 48 additions and 48 deletions

View File

@ -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",

View File

@ -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<string, HookCallback> {
// nuxt3
'app:resolve': (app: NuxtApp) => HookResult
'app:templates': (app: NuxtApp) => HookResult

View File

@ -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<Hook extends NuxtHookName>(hookName: Hook, callback: NuxtHooks[Hook]): void | Promise<void>
/** Run all Nuxt hooks that have been registered against the hook name. */
callHook<Hook extends NuxtHookName>(hookName: Hook, ...args: Parameters<NuxtHooks[Hook]>): ReturnType<NuxtHooks[Hook]>
/** Add all hooks in the object passed in. */
addHooks(hooks: Partial<NuxtHooks>): void
}
hooks: Hookable<NuxtHooks>
hook: Nuxt['hooks']['hook']
callHook: Nuxt['hooks']['callHook']
addHooks: Nuxt['hooks']['addHooks']
ready: () => Promise<void>
close: () => Promise<void>

View File

@ -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",

View File

@ -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 () => {

View File

@ -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<void>
'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<NitroHooks>
nuxtHooks: NestedHooks<NuxtHooks>
ignore: string[]
env: Preset
vfs: Record<string, string>
@ -59,7 +68,7 @@ export interface NitroContext {
}
_internal: {
runtimeDir: string
hooks: Hookable
hooks: Hookable<NitroHooks>
}
}
@ -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<NitroHooks>()
}
}

View File

@ -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)
}
}

View File

@ -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",

View File

@ -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<Hook extends NuxtAppHookName> (hookName: Hook, callback: RuntimeNuxtHooks[Hook]): HookResult
hookOnce<Hook extends NuxtAppHookName> (hookName: Hook, callback: RuntimeNuxtHooks[Hook]): HookResult
/** Run all Nuxt hooks that have been registered against the hook name. */
callHook<Hook extends NuxtAppHookName> (hookName: Hook, ...args: Parameters<RuntimeNuxtHooks[Hook]>): ReturnType<RuntimeNuxtHooks[Hook]>
/** Add all hooks in the object passed in. */
addHooks (hooks: Partial<RuntimeNuxtHooks>): void
}
hooks: Hookable<RuntimeNuxtHooks>
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<RuntimeNuxtHooks>()
nuxt.hook = nuxt.hooks.hook
nuxt.callHook = nuxt.hooks.callHook

View File

@ -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<NuxtHooks>()
const nuxt: Nuxt = {
options,
hooks,
callHook: hooks.callHook,
addHooks: hooks.addHooks,
hook: hooks.hook,
ready: () => initNuxt(nuxt),
close: () => Promise.resolve(hooks.callHook('close', nuxt)),

View File

@ -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