feat: add hook signatures and basic typings (#79)

This commit is contained in:
Daniel Roe 2021-04-12 23:12:30 +01:00 committed by GitHub
parent 9722c5e7a1
commit dacde63063
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 134 additions and 18 deletions

View File

@ -12,5 +12,6 @@ export default <BuildConfig>{
} }
}, },
'src/index' 'src/index'
] ],
externals: ['webpack']
} }

View File

@ -1,13 +1,118 @@
import type { IncomingMessage, ServerResponse } from 'http'
import type { Compiler, Configuration, Stats } from 'webpack'
import type { NuxtConfig, NuxtOptions } from '..'
import type { ModuleContainer } from '../module/container'
import { Nuxt } from './nuxt' import { Nuxt } from './nuxt'
export type NuxtHook<Arg1 = any> = (arg1?: Arg1, ...args: any) => Promise<void> | void type HookResult = Promise<void> | void
type Builder = any
type Generator = any
type Server = any
type TemplateFile = string | {
src?: string
dst?: string
custom?: boolean
options?: any
}
type WatchEvent = 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir'
export interface NuxtHooks { export interface NuxtHooks {
[key: string]: NuxtHook // Don't break usage of untyped hooks
[key: string]: (...args: any[]) => HookResult
'modules:before': NuxtHook<Nuxt> // @nuxt/builder
'modules:done': NuxtHook<Nuxt> 'build:before':
'ready': NuxtHook<Nuxt> (builder: Builder, buildOptions: NuxtOptions['build']) => HookResult
'builder:prepared': (builder: Builder, buildOptions: NuxtOptions['build']) => HookResult
'builder:extendPlugins': (plugins: NuxtOptions['plugins']) => HookResult
'build:templates': (templates: {
templateVars: Record<string, any>,
templatesFiles: TemplateFile[],
resolve: (...args: string[]) => string
}) => HookResult
'build:extendRoutes': (routes: any[], resolve: (...args: string[]) => string) => HookResult
'build:done': (builder: Builder) => HookResult
'watch:restart': (event: { event: string, path: string }) => HookResult
// 'watch:fileChanged': (builder: Builder, fileName: string) => HookResult
'builder:watch': (event: WatchEvent, path: string) => HookResult
// @nuxt/cli
'cli:buildError': (error: unknown) => HookResult
'generate:cache:ignore': (ignore: string[]) => HookResult
'config': (options: NuxtConfig) => HookResult
'run:before': (options: { argv: string[], cmd: { name: string, usage: string, description: string, options: Record<string, any> }, rootDir: string }) => HookResult
// @nuxt/core
'ready': (nuxt: Nuxt) => HookResult
'close': (nuxt: Nuxt) => HookResult
'modules:before': (moduleContainer: ModuleContainer, modules?: any[]) => HookResult
'modules:done': (moduleContainer: ModuleContainer) => HookResult
// 'webpack:done': () => HookResult
// @nuxt/server
'render:before': (server: Server, renderOptions: NuxtOptions['render']) => HookResult
'render:setupMiddleware': (app: any) => HookResult
'render:errorMiddleware': (app: any) => HookResult
'render:done': (server: Server) => HookResult
'listen': (listenerServer: any, listener: any) => HookResult
'server:nuxt:renderLoading': (req: IncomingMessage, res: ServerResponse) => HookResult
'render:route': (url: string, result: string, context: any) => HookResult
'render:routeDone': (url: string, result: string, context: any) => HookResult
'render:beforeResponse': (url: string, result: string, context: any) => HookResult
// @nuxt/vue-renderer
'render:resourcesLoaded': (resources: any) => HookResult
'vue-renderer:context': (renderContext: any) => HookResult
'vue-renderer:spa:prepareContext': (renderContext: any) => HookResult
'vue-renderer:spa:templateParams': (templateParams: Record<string, any>) => HookResult
'vue-renderer:ssr:prepareContext': (renderContext: any) => HookResult
'vue-renderer:ssr:context': (renderContext: any) => HookResult
// '_render:context': (nuxt: Nuxt) => HookResult
// 'render:routeContext': (renderContext: any) => HookResult
'vue-renderer:ssr:csp': (cspScriptSrcHashes: string[]) => HookResult
'vue-renderer:ssr:templateParams': (templateParams: Record<string, any>, renderContext: any) => HookResult
// @nuxt/webpack
'webpack:config': (webpackConfigs: Configuration[]) => HookResult
'build:compile': (options: { name: string, compiler: Compiler }) => HookResult
'build:compiled': (options: { name: string, compiler: Compiler, stats: Stats }) => HookResult
'build:resources': (mfs?: Compiler['outputFileSystem']) => HookResult
'server:devMiddleware': (middleware: (req: IncomingMessage, res: ServerResponse, next: (err?: any) => any) => any) => HookResult
'bundler:change': (shortPath: string) => void
'bundler:error': () => void
'bundler:done': () => void
'bundler:progress': (statesArray: any[]) => void
// @nuxt/generator
'generate:before': (generator: Generator, generateOptions: NuxtOptions['generate']) => HookResult
'generate:distRemoved': (generator: Generator) => HookResult
'generate:distCopied': (generator: Generator) => HookResult
'generate:route': ({ route, setPayload }: { route: any, setPayload: any }) => HookResult
'generate:page': (page: {
route: any,
path: string,
html: string,
exclude: boolean,
errors: string[]
}) => HookResult
'generate:routeCreated': ({ route, path, errors }: { route: any, path: string, errors: any[] }) => HookResult
'generate:extendRoutes': (routes: any[]) => HookResult
'generate:routeFailed': ({ route, errors }: { route: any, errors: any[] }) => HookResult
'generate:manifest': (manifest: any, generator: Generator) => HookResult
'generate:done': (generator: Generator, errors: any[]) => HookResult
'export:before': (generator: Generator) => HookResult
'export:distRemoved': (generator: Generator) => HookResult
'export:distCopied': (generator: Generator) => HookResult
'export:route': ({ route, setPayload }: { route: any, setPayload: any }) => HookResult
'export:routeCreated': ({ route, path, errors }: { route: any, path: string, errors: any[] }) => HookResult
'export:extendRoutes': ({ routes }: { routes: any[] }) => HookResult
'export:routeFailed': ({ route, errors }: { route: any, errors: any[] }) => HookResult
'export:done': (generator: Generator, { errors }: { errors: any[] }) => HookResult
} }
export type NuxtHookName = keyof NuxtHooks export type NuxtHookName = keyof NuxtHooks

View File

@ -1,13 +1,13 @@
import { NuxtHookName, NuxtHook, NuxtHooks } from './hooks' import { NuxtHookName, NuxtHooks } from './hooks'
import { NuxtOptions } from './config' import { NuxtOptions } from './config'
export interface Nuxt { export interface Nuxt {
options: NuxtOptions options: NuxtOptions
hooks: { hooks: {
hook(hookName: NuxtHookName, callback: NuxtHook) hook<Hook extends NuxtHookName>(hookName: Hook, callback: NuxtHooks[Hook]): void | Promise<void>
callHook<T extends string>(hookbname: T, ...args: Parameters<NuxtHooks[T]>) callHook<Hook extends NuxtHookName>(hookName: Hook, ...args: Parameters<NuxtHooks[Hook]>): ReturnType<NuxtHooks[Hook]>
addHooks(hooks: Partial<NuxtHooks>) addHooks(hooks: Partial<NuxtHooks>): void
} }
hook: Nuxt['hooks']['hook'] hook: Nuxt['hooks']['hook']
callHook: Nuxt['hooks']['callHook'] callHook: Nuxt['hooks']['callHook']

View File

@ -3,7 +3,7 @@ import { loadNuxtConfig, LoadNuxtConfigOptions, Nuxt, NuxtOptions, installModule
import { initNitro } from './nitro' import { initNitro } from './nitro'
export function createNuxt (options: NuxtOptions): Nuxt { export function createNuxt (options: NuxtOptions): Nuxt {
const hooks = new Hookable() const hooks = new Hookable() as any as Nuxt['hooks']
return { return {
options, options,

View File

@ -46,6 +46,7 @@
"webpackbar": "^5.0.0-3" "webpackbar": "^5.0.0-3"
}, },
"devDependencies": { "devDependencies": {
"@types/pify": "^5.0.0",
"@types/terser-webpack-plugin": "^5.0.3", "@types/terser-webpack-plugin": "^5.0.3",
"@types/webpack-bundle-analyzer": "^3.9.2", "@types/webpack-bundle-analyzer": "^3.9.2",
"@types/webpack-dev-middleware": "^4.1.2", "@types/webpack-dev-middleware": "^4.1.2",

View File

@ -1,4 +1,5 @@
import path from 'path' import path from 'path'
import type { IncomingMessage, ServerResponse } from 'http'
import pify from 'pify' import pify from 'pify'
import webpack from 'webpack' import webpack from 'webpack'
import Glob from 'glob' import Glob from 'glob'
@ -22,12 +23,12 @@ class WebpackBundler {
compilers: Array<Compiler> compilers: Array<Compiler>
compilersWatching: Array<Watching & { closeAsync?: () => void }> compilersWatching: Array<Watching & { closeAsync?: () => void }>
// TODO: change this when pify has better types https://github.com/sindresorhus/pify/pull/76 // TODO: change this when pify has better types https://github.com/sindresorhus/pify/pull/76
devMiddleware: Record<string, Function & { close?: () => void, context?: WebpackDevMiddlewareContext }> devMiddleware: Record<string, Function & { close?: () => Promise<void>, context?: WebpackDevMiddlewareContext }>
hotMiddleware: Record<string, Function> hotMiddleware: Record<string, Function>
mfs: Compiler['outputFileSystem'] mfs?: Compiler['outputFileSystem']
__closed: boolean __closed?: boolean
constructor (nuxt) { constructor (nuxt: Nuxt) {
this.nuxt = nuxt this.nuxt = nuxt
// TODO: plugins // TODO: plugins
this.plugins = [] this.plugins = []
@ -98,7 +99,7 @@ class WebpackBundler {
// In dev, write files in memory FS // In dev, write files in memory FS
if (options.dev) { if (options.dev) {
compiler.outputFileSystem = this.mfs compiler.outputFileSystem = this.mfs!
} }
return compiler return compiler
@ -175,7 +176,7 @@ class WebpackBundler {
await this.nuxt.callHook('build:resources') await this.nuxt.callHook('build:resources')
} }
async webpackDev (compiler) { async webpackDev (compiler: Compiler) {
consola.debug('Creating webpack middleware...') consola.debug('Creating webpack middleware...')
const { name } = compiler.options const { name } = compiler.options
@ -216,7 +217,7 @@ class WebpackBundler {
await this.nuxt.callHook('server:devMiddleware', this.middleware) await this.nuxt.callHook('server:devMiddleware', this.middleware)
} }
async middleware (req, res, next) { async middleware (req: IncomingMessage, res: ServerResponse, next: () => any) {
if (this.devMiddleware && this.devMiddleware.client) { if (this.devMiddleware && this.devMiddleware.client) {
await this.devMiddleware.client(req, res) await this.devMiddleware.client(req, res)
} }

View File

@ -1751,6 +1751,7 @@ __metadata:
"@babel/core": ^7.13.15 "@babel/core": ^7.13.15
"@nuxt/friendly-errors-webpack-plugin": ^2.5.1 "@nuxt/friendly-errors-webpack-plugin": ^2.5.1
"@nuxt/kit": ^0.3.0 "@nuxt/kit": ^0.3.0
"@types/pify": ^5.0.0
"@types/terser-webpack-plugin": ^5.0.3 "@types/terser-webpack-plugin": ^5.0.3
"@types/webpack-bundle-analyzer": ^3.9.2 "@types/webpack-bundle-analyzer": ^3.9.2
"@types/webpack-dev-middleware": ^4.1.2 "@types/webpack-dev-middleware": ^4.1.2
@ -2370,6 +2371,13 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"@types/pify@npm:^5.0.0":
version: 5.0.0
resolution: "@types/pify@npm:5.0.0"
checksum: 3e2a1ba34accea3379dfa4750dc5a5eb37e5254aa77cce22ce48f96ef87ee248519bbec684c341afe9db17f4829d44e0cdaf05f87738230602241192c2264f1b
languageName: node
linkType: hard
"@types/prettier@npm:^2.0.0": "@types/prettier@npm:^2.0.0":
version: 2.2.3 version: 2.2.3
resolution: "@types/prettier@npm:2.2.3" resolution: "@types/prettier@npm:2.2.3"