@@ -2,10 +2,13 @@ import { createUnplugin } from 'unplugin'
2
2
import { createFilter } from '@rollup/pluginutils'
3
3
import chokidar from 'chokidar'
4
4
import type { ResolvedConfig , ViteDevServer } from 'vite'
5
+ import type { Watching } from 'webpack'
5
6
import type { Options , PublicPluginAPI } from '../types'
6
7
import { Context } from './context'
7
8
import { shouldTransform , stringifyComponentImport } from './utils'
8
9
10
+ const PLUGIN_NAME = 'unplugin:webpack'
11
+
9
12
export default createUnplugin < Options > ( ( options = { } ) => {
10
13
const filter = createFilter (
11
14
options . include || [ / \. v u e $ / , / \. v u e \? v u e / ] ,
@@ -67,16 +70,22 @@ export default createUnplugin<Options>((options = {}) => {
67
70
} ,
68
71
69
72
webpack ( compiler ) {
70
- if ( compiler . options . mode !== 'development' )
71
- return
73
+ let watcher : Watching
72
74
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
+ }
78
87
} )
79
- compiler . hooks . compilation . tap ( 'unplugin-vue-components' , ( compilation ) => {
88
+ compiler . hooks . compilation . tap ( PLUGIN_NAME , ( compilation ) => {
80
89
if ( fileDepQueue . length ) {
81
90
fileDepQueue . forEach ( ( { path, type } ) => {
82
91
if ( type === 'unlink' )
0 commit comments