Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: compiler.watcher undefined #434

Merged
merged 2 commits into from Jun 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/core/unplugin.ts
Expand Up @@ -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>((options = {}) => {
const filter = createFilter(
options.include || [/\.vue$/, /\.vue\?vue/],
Expand Down Expand Up @@ -67,16 +70,22 @@ export default createUnplugin<Options>((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(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') => {
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')
Expand Down