Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.

Commit 2bb898f

Browse files
authored
fix(webpack): promisify webpack dev/hot handlers using h3.promisifyHandler (#7275)
1 parent 5a69f48 commit 2bb898f

File tree

5 files changed

+12
-7
lines changed

5 files changed

+12
-7
lines changed

packages/schema/build.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export default defineBuildConfig({
3535
'terser-webpack-plugin',
3636
'css-minimizer-webpack-plugin',
3737
'webpack-dev-middleware',
38+
'h3',
3839
'webpack-hot-middleware',
3940
'postcss',
4041
'consola',

packages/schema/src/types/hooks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { Compiler, Configuration, Stats } from 'webpack'
44
import type { TSConfig } from 'pkg-types'
55
import type { InlineConfig as ViteInlineConfig, ViteDevServer } from 'vite'
66
import type { Manifest } from 'vue-bundle-renderer'
7+
import type { Middleware } from 'h3'
78
import type { ModuleContainer } from './module'
89
import type { NuxtTemplate, Nuxt, NuxtApp } from './nuxt'
910
import type { Preset as ImportPreset, Import } from 'unimport'
@@ -159,7 +160,7 @@ export interface NuxtHooks {
159160
'build:compile': (options: { name: string, compiler: Compiler }) => HookResult
160161
'build:compiled': (options: { name: string, compiler: Compiler, stats: Stats }) => HookResult
161162
'build:resources': (mfs?: Compiler['outputFileSystem']) => HookResult
162-
'server:devMiddleware': (middleware: (req: IncomingMessage, res: ServerResponse, next: (err?: any) => any) => any) => HookResult
163+
'server:devMiddleware': (middleware: Middleware) => HookResult
163164
'bundler:change': (shortPath: string) => void
164165
'bundler:error': () => void
165166
'bundler:done': () => void

packages/webpack/build.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export default defineBuildConfig({
1919
'vue'
2020
],
2121
externals: [
22-
'@nuxt/schema'
22+
'@nuxt/schema',
23+
'h3'
2324
]
2425
})

packages/webpack/src/configs/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ function clientHMR (ctx: WebpackConfigContext) {
6464
// Add HMR support
6565
const app = (config.entry as any).app as any
6666
app.unshift(
67-
// https://github.com/glenjamin/webpack-hot-middleware#config
68-
`webpack-hot-middleware/client?${hotMiddlewareClientOptionsStr}`
67+
// https://github.com/glenjamin/webpack-hot-middleware#config
68+
`webpack-hot-middleware/client?${hotMiddlewareClientOptionsStr}`
6969
)
7070

7171
config.plugins = config.plugins || []

packages/webpack/src/webpack.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { IncomingMessage, ServerResponse } from 'node:http'
22
import pify from 'pify'
33
import webpack from 'webpack'
4+
import { promisifyHandler } from 'h3'
45
import webpackDevMiddleware, { API, OutputFileSystem } from 'webpack-dev-middleware'
56
import webpackHotMiddleware from 'webpack-hot-middleware'
67
import type { Compiler, Watching } from 'webpack'
@@ -97,9 +98,10 @@ async function createDevMiddleware (compiler: Compiler) {
9798
await nuxt.callHook('webpack:hotMiddleware', hotMiddleware)
9899

99100
// Register devMiddleware on server
100-
await nuxt.callHook('server:devMiddleware', async (req: IncomingMessage, res: ServerResponse, next: (error?: any) => void) => {
101-
for (const mw of [devMiddleware, hotMiddleware]) {
102-
await mw?.(req, res, next)
101+
const handlers = [promisifyHandler(devMiddleware), promisifyHandler(hotMiddleware)]
102+
await nuxt.callHook('server:devMiddleware', async (req, res, next) => {
103+
for (const mw of handlers) {
104+
await mw?.(req, res)
103105
}
104106
next()
105107
})

0 commit comments

Comments
 (0)