fix(webpack): types in webpack and await compiler close (#176)

This commit is contained in:
Xin Du (Clark) 2021-03-05 18:17:05 +00:00 committed by GitHub
parent 5248c61ed0
commit 2c9854dfe3

View File

@ -5,6 +5,11 @@ import Glob from 'glob'
import webpackDevMiddleware from 'webpack-dev-middleware' import webpackDevMiddleware from 'webpack-dev-middleware'
import webpackHotMiddleware from 'webpack-hot-middleware' import webpackHotMiddleware from 'webpack-hot-middleware'
import consola from 'consola' import consola from 'consola'
import type { Compiler, Watching } from 'webpack'
import type { Context as WebpackDevMiddlewareContext, Options as WebpackDevMiddlewareOptions } from 'webpack-dev-middleware'
import type { MiddlewareOptions as WebpackHotMiddlewareOptions } from 'webpack-hot-middleware'
import { Nuxt } from 'src/core' import { Nuxt } from 'src/core'
import { TARGETS, parallel, sequence, wrapArray } from 'src/utils' import { TARGETS, parallel, sequence, wrapArray } from 'src/utils'
import { createMFS } from './utils/mfs' import { createMFS } from './utils/mfs'
@ -12,10 +17,16 @@ import { client, server } from './configs'
import { createWebpackConfigContext, applyPresets, getWebpackConfig } from './utils/config' import { createWebpackConfigContext, applyPresets, getWebpackConfig } from './utils/config'
const glob = pify(Glob) const glob = pify(Glob)
class WebpackBundler { class WebpackBundler {
nuxt: Nuxt nuxt: Nuxt
plugins: Array<string> plugins: Array<string>
compilers: Array<Compiler>
compilersWatching: Array<Watching & { closeAsync?: () => void }>
// TODO: change this when pify has better types https://github.com/sindresorhus/pify/pull/76
devMiddleware: Record<string, Function & { close?: () => void, context?: WebpackDevMiddlewareContext }>
hotMiddleware: Record<string, Function>
mfs: Compiler['outputFileSystem']
__closed: boolean
constructor (nuxt) { constructor (nuxt) {
this.nuxt = nuxt this.nuxt = nuxt
@ -33,7 +44,7 @@ class WebpackBundler {
// Initialize shared MFS for dev // Initialize shared MFS for dev
if (this.nuxt.options.dev) { if (this.nuxt.options.dev) {
this.mfs = createMFS() this.mfs = createMFS() as Compiler['outputFileSystem']
} }
} }
@ -143,7 +154,7 @@ class WebpackBundler {
resolve() resolve()
}) })
watching.close = pify(watching.close) watching.closeAsync = pify(watching.close)
this.compilersWatching.push(watching) this.compilersWatching.push(watching)
}) })
} }
@ -175,12 +186,14 @@ class WebpackBundler {
// Create webpack dev middleware // Create webpack dev middleware
this.devMiddleware[name] = pify( this.devMiddleware[name] = pify(
webpackDevMiddleware( webpackDevMiddleware(
compiler, { compiler,
{
publicPath: buildOptions.publicPath, publicPath: buildOptions.publicPath,
outputFileSystem: this.mfs, outputFileSystem: this.mfs,
stats: 'none', stats: 'none',
...buildOptions.devMiddleware ...buildOptions.devMiddleware
}) } as WebpackDevMiddlewareOptions
)
) )
this.devMiddleware[name].close = pify(this.devMiddleware[name].close) this.devMiddleware[name].close = pify(this.devMiddleware[name].close)
@ -189,12 +202,14 @@ class WebpackBundler {
this.hotMiddleware[name] = pify( this.hotMiddleware[name] = pify(
webpackHotMiddleware( webpackHotMiddleware(
compiler, { compiler,
{
log: false, log: false,
heartbeat: 10000, heartbeat: 10000,
path: `/__webpack_hmr/${name}`, path: `/__webpack_hmr/${name}`,
...hotMiddlewareOptions ...hotMiddlewareOptions
}) } as WebpackHotMiddlewareOptions
)
) )
// Register devMiddleware on server // Register devMiddleware on server
@ -214,7 +229,7 @@ class WebpackBundler {
} }
async unwatch () { async unwatch () {
await Promise.all(this.compilersWatching.map(watching => watching.close())) await Promise.all(this.compilersWatching.map(watching => watching.closeAsync()))
} }
async close () { async close () {
@ -232,12 +247,11 @@ class WebpackBundler {
} }
for (const compiler of this.compilers) { for (const compiler of this.compilers) {
compiler.close() await new Promise(resolve => compiler.close(resolve))
} }
// Cleanup MFS // Cleanup MFS
if (this.mfs) { if (this.mfs) {
delete this.mfs.data
delete this.mfs delete this.mfs
} }