Skip to content

Commit 34db08b

Browse files
fix: make addWatchFile() work (fix #7024) (#9723)
Co-authored-by: Matt Jones <mattjones701@gmail.com>
1 parent 18c71dc commit 34db08b

File tree

7 files changed

+56
-4
lines changed

7 files changed

+56
-4
lines changed

packages/vite/src/node/plugins/importAnalysis.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
231231
throwOutdatedRequest(importer)
232232
}
233233

234-
if (!imports.length) {
234+
if (!imports.length && !(this as any)._addedImports) {
235235
importerModule.isSelfAccepting = false
236236
isDebug &&
237237
debug(
@@ -263,7 +263,8 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
263263

264264
const normalizeUrl = async (
265265
url: string,
266-
pos: number
266+
pos: number,
267+
forceSkipImportAnalysis: boolean = false
267268
): Promise<[string, string]> => {
268269
url = stripBase(url, base)
269270

@@ -364,7 +365,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
364365
const depModule = await moduleGraph.ensureEntryFromUrl(
365366
unwrapId(url),
366367
ssr,
367-
canSkipImportAnalysis(url)
368+
canSkipImportAnalysis(url) || forceSkipImportAnalysis
368369
)
369370
if (depModule.lastHMRTimestamp > 0) {
370371
url = injectQuery(url, `t=${depModule.lastHMRTimestamp}`)
@@ -667,7 +668,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
667668
if (pluginImports) {
668669
;(
669670
await Promise.all(
670-
[...pluginImports].map((id) => normalizeUrl(id, 0))
671+
[...pluginImports].map((id) => normalizeUrl(id, 0, true))
671672
)
672673
).forEach(([url]) => importedUrls.add(url))
673674
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { expect, test } from 'vitest'
2+
import { editFile, page, untilUpdated } from '~utils'
3+
4+
test('should re-run transform when plugin-dep file is edited', async () => {
5+
expect(await page.textContent('#transform-count')).toBe('1')
6+
7+
await editFile('plugin-dep.js', (str) => str)
8+
await untilUpdated(() => page.textContent('#transform-count'), '2')
9+
})
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div id="transform-count"></div>
2+
3+
<script type="module" src="./index.js"></script>

playground/transform-plugin/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// 'TRANSFORM_COUNT' is injected by the transform plugin
2+
document.getElementById('transform-count').innerHTML = TRANSFORM_COUNT
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "test-transform-plugin",
3+
"private": true,
4+
"version": "0.0.0",
5+
"scripts": {
6+
"dev": "vite",
7+
"build": "vite build",
8+
"debug": "node --inspect-brk ../../vite/bin/vite",
9+
"serve": "vite preview"
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Empty file for detecting changes in tests
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const { resolve } = require('node:path')
2+
const { normalizePath } = require('vite')
3+
4+
let transformCount = 1
5+
6+
const transformPlugin = {
7+
name: 'transform',
8+
transform(code, id) {
9+
if (id === normalizePath(resolve(__dirname, 'index.js'))) {
10+
// Ensure `index.js` is reevaluated if 'plugin-dep.js' is changed
11+
this.addWatchFile('./plugin-dep.js')
12+
13+
return `
14+
// Inject TRANSFORM_COUNT
15+
let TRANSFORM_COUNT = ${transformCount++};
16+
17+
${code}
18+
`
19+
}
20+
}
21+
}
22+
23+
module.exports = {
24+
plugins: [transformPlugin]
25+
}

0 commit comments

Comments
 (0)