@@ -2,10 +2,13 @@ import { createUnplugin } from 'unplugin'
22import { createFilter } from '@rollup/pluginutils'
33import chokidar from 'chokidar'
44import type { ResolvedConfig , ViteDevServer } from 'vite'
5+ import type { Watching } from 'webpack'
56import type { Options , PublicPluginAPI } from '../types'
67import { Context } from './context'
78import { shouldTransform , stringifyComponentImport } from './utils'
89
10+ const PLUGIN_NAME = 'unplugin:webpack'
11+
912export default createUnplugin < Options > ( ( options = { } ) => {
1013 const filter = createFilter (
1114 options . include || [ / \. v u e $ / , / \. v u e \? v u e / ] ,
@@ -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 webpack@5.0.0-rc.1)
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