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

Commit

Permalink
fix(webpack): promisify webpack dev/hot handlers using `h3.promisifyH…
Browse files Browse the repository at this point in the history
…andler` (#7275)
  • Loading branch information
danielroe committed Sep 7, 2022
1 parent 5a69f48 commit 2bb898f
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/schema/build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default defineBuildConfig({
'terser-webpack-plugin',
'css-minimizer-webpack-plugin',
'webpack-dev-middleware',
'h3',
'webpack-hot-middleware',
'postcss',
'consola',
Expand Down
3 changes: 2 additions & 1 deletion packages/schema/src/types/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Compiler, Configuration, Stats } from 'webpack'
import type { TSConfig } from 'pkg-types'
import type { InlineConfig as ViteInlineConfig, ViteDevServer } from 'vite'
import type { Manifest } from 'vue-bundle-renderer'
import type { Middleware } from 'h3'
import type { ModuleContainer } from './module'
import type { NuxtTemplate, Nuxt, NuxtApp } from './nuxt'
import type { Preset as ImportPreset, Import } from 'unimport'
Expand Down Expand Up @@ -159,7 +160,7 @@ export interface NuxtHooks {
'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
'server:devMiddleware': (middleware: Middleware) => HookResult
'bundler:change': (shortPath: string) => void
'bundler:error': () => void
'bundler:done': () => void
Expand Down
3 changes: 2 additions & 1 deletion packages/webpack/build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default defineBuildConfig({
'vue'
],
externals: [
'@nuxt/schema'
'@nuxt/schema',
'h3'
]
})
4 changes: 2 additions & 2 deletions packages/webpack/src/configs/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ function clientHMR (ctx: WebpackConfigContext) {
// Add HMR support
const app = (config.entry as any).app as any
app.unshift(
// https://github.com/glenjamin/webpack-hot-middleware#config
`webpack-hot-middleware/client?${hotMiddlewareClientOptionsStr}`
// https://github.com/glenjamin/webpack-hot-middleware#config
`webpack-hot-middleware/client?${hotMiddlewareClientOptionsStr}`
)

config.plugins = config.plugins || []
Expand Down
8 changes: 5 additions & 3 deletions packages/webpack/src/webpack.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { IncomingMessage, ServerResponse } from 'node:http'
import pify from 'pify'
import webpack from 'webpack'
import { promisifyHandler } from 'h3'
import webpackDevMiddleware, { API, OutputFileSystem } from 'webpack-dev-middleware'
import webpackHotMiddleware from 'webpack-hot-middleware'
import type { Compiler, Watching } from 'webpack'
Expand Down Expand Up @@ -97,9 +98,10 @@ async function createDevMiddleware (compiler: Compiler) {
await nuxt.callHook('webpack:hotMiddleware', hotMiddleware)

// Register devMiddleware on server
await nuxt.callHook('server:devMiddleware', async (req: IncomingMessage, res: ServerResponse, next: (error?: any) => void) => {
for (const mw of [devMiddleware, hotMiddleware]) {
await mw?.(req, res, next)
const handlers = [promisifyHandler(devMiddleware), promisifyHandler(hotMiddleware)]
await nuxt.callHook('server:devMiddleware', async (req, res, next) => {
for (const mw of handlers) {
await mw?.(req, res)
}
next()
})
Expand Down

0 comments on commit 2bb898f

Please sign in to comment.