Skip to content

Commit 00fcab2

Browse files
fix: compiler.watcher undefined (#434)
* fix: compiler.watcher undefined * docs: add comment
1 parent 777babb commit 00fcab2

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

src/core/unplugin.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ import { createUnplugin } from 'unplugin'
22
import { createFilter } from '@rollup/pluginutils'
33
import chokidar from 'chokidar'
44
import type { ResolvedConfig, ViteDevServer } from 'vite'
5+
import type { Watching } from 'webpack'
56
import type { Options, PublicPluginAPI } from '../types'
67
import { Context } from './context'
78
import { shouldTransform, stringifyComponentImport } from './utils'
89

10+
const PLUGIN_NAME = 'unplugin:webpack'
11+
912
export default createUnplugin<Options>((options = {}) => {
1013
const filter = createFilter(
1114
options.include || [/\.vue$/, /\.vue\?vue/],
@@ -67,16 +70,22 @@ export default createUnplugin<Options>((options = {}) => {
6770
},
6871

6972
webpack(compiler) {
70-
if (compiler.options.mode !== 'development')
71-
return
73+
let watcher: Watching
7274
let fileDepQueue: { path: string; type: 'unlink' | 'add' }[] = []
73-
ctx.setupWatcherWebpack(chokidar.watch(ctx.options.globs), (path: string, type: 'unlink' | 'add') => {
74-
fileDepQueue.push({ path, type })
75-
process.nextTick(() => {
76-
compiler.watching.invalidate()
77-
})
75+
compiler.hooks.watchRun.tap(PLUGIN_NAME, () => {
76+
// ensure watcher is ready(supported since [email protected])
77+
if (!watcher && compiler.watching) {
78+
watcher = compiler.watching
79+
ctx.setupWatcherWebpack(chokidar.watch(ctx.options.globs), (path: string, type: 'unlink' | 'add') => {
80+
fileDepQueue.push({ path, type })
81+
// process.nextTick is for aggregated file change event
82+
process.nextTick(() => {
83+
watcher.invalidate()
84+
})
85+
})
86+
}
7887
})
79-
compiler.hooks.compilation.tap('unplugin-vue-components', (compilation) => {
88+
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
8089
if (fileDepQueue.length) {
8190
fileDepQueue.forEach(({ path, type }) => {
8291
if (type === 'unlink')

0 commit comments

Comments
 (0)