Skip to content

Commit 0472bb8

Browse files
✨ Add minimal watch example. API to be simplified in the future.
Concept taken from comment by <hi-ogawa, Hiroshi Ogawa> - vitest-dev/vitest#6457 (comment) and their example at https://stackblitz.com/edit/vitest-dev-vitest-qq31nt?file=vite.config.ts
1 parent 19279a4 commit 0472bb8

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/plugin.ts

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
import { Plugin } from "rollup"
22

3-
export function guardLikeWatch(): Plugin {
3+
export type Watch = {
4+
pattern: RegExp | string
5+
action: (match: RegExpMatchArray, id: string) => string[]
6+
}
7+
8+
export function guardLikeWatch(watches: Watch[]): Plugin {
49
return {
510
name: "vite-plugin-guard-like-watch",
611
transform(_code, id) {
7-
console.log(`id:`, id)
8-
}
12+
for (const watch of watches) {
13+
const regexp = new RegExp(watch.pattern)
14+
const match = id.match(regexp)
15+
if (match === null || match === undefined) return
16+
17+
const filesToWatch = watch.action(match, id)
18+
for (const file of filesToWatch) {
19+
this.addWatchFile(file)
20+
}
21+
}
22+
},
923
}
1024
}
1125

vite.config.mts

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ export default defineConfig({
1212
root: ".",
1313
projects: ["./tsconfig.json", "./tests/tsconfig.json"],
1414
}),
15-
guardLikeWatch(),
15+
guardLikeWatch([
16+
{
17+
pattern: /(.*\/example)\.ts/,
18+
action(match) {
19+
return [`${match[1]}.html`, `${match[1]}.txt`]
20+
},
21+
},
22+
]),
1623
],
1724
test: {
1825
globals: true,

0 commit comments

Comments
 (0)