From cbbf593521e791c4fa5c3c7a1b411d6b6e78414b Mon Sep 17 00:00:00 2001 From: cyly <786156072@qq.com> Date: Sun, 26 Jun 2022 03:25:48 +0800 Subject: [PATCH 1/2] fix: compiler.watcher undefined --- src/core/unplugin.ts | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/core/unplugin.ts b/src/core/unplugin.ts index b9f78319..cbe910ae 100644 --- a/src/core/unplugin.ts +++ b/src/core/unplugin.ts @@ -2,10 +2,13 @@ import { createUnplugin } from 'unplugin' import { createFilter } from '@rollup/pluginutils' import chokidar from 'chokidar' import type { ResolvedConfig, ViteDevServer } from 'vite' +import type { Watching } from 'webpack' import type { Options, PublicPluginAPI } from '../types' import { Context } from './context' import { shouldTransform, stringifyComponentImport } from './utils' +const PLUGIN_NAME = 'unplugin:webpack' + export default createUnplugin((options = {}) => { const filter = createFilter( options.include || [/\.vue$/, /\.vue\?vue/], @@ -67,16 +70,22 @@ export default createUnplugin((options = {}) => { }, webpack(compiler) { - if (compiler.options.mode !== 'development') - return + let watcher: Watching let fileDepQueue: { path: string; type: 'unlink' | 'add' }[] = [] - ctx.setupWatcherWebpack(chokidar.watch(ctx.options.globs), (path: string, type: 'unlink' | 'add') => { - fileDepQueue.push({ path, type }) - process.nextTick(() => { - compiler.watching.invalidate() - }) + compiler.hooks.watchRun.tap(PLUGIN_NAME, () => { + // ensure watcher is ready + if (!watcher && compiler.watching) { + watcher = compiler.watching + ctx.setupWatcherWebpack(chokidar.watch(ctx.options.globs), (path: string, type: 'unlink' | 'add') => { + fileDepQueue.push({ path, type }) + // process.nextTick is for aggregated file change event + process.nextTick(() => { + watcher.invalidate() + }) + }) + } }) - compiler.hooks.compilation.tap('unplugin-vue-components', (compilation) => { + compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { if (fileDepQueue.length) { fileDepQueue.forEach(({ path, type }) => { if (type === 'unlink') From a5996fc649b7e0635285a9a6ca80744eca3f0c55 Mon Sep 17 00:00:00 2001 From: cyly <786156072@qq.com> Date: Sun, 26 Jun 2022 19:54:10 +0800 Subject: [PATCH 2/2] docs: add comment --- src/core/unplugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/unplugin.ts b/src/core/unplugin.ts index cbe910ae..922e9449 100644 --- a/src/core/unplugin.ts +++ b/src/core/unplugin.ts @@ -73,7 +73,7 @@ export default createUnplugin((options = {}) => { let watcher: Watching let fileDepQueue: { path: string; type: 'unlink' | 'add' }[] = [] compiler.hooks.watchRun.tap(PLUGIN_NAME, () => { - // ensure watcher is ready + // ensure watcher is ready(supported since webpack@5.0.0-rc.1) if (!watcher && compiler.watching) { watcher = compiler.watching ctx.setupWatcherWebpack(chokidar.watch(ctx.options.globs), (path: string, type: 'unlink' | 'add') => {