fix: make addWatchFile() work (fix #7024) (#9723)

Co-authored-by: Matt Jones <mattjones701@gmail.com>
This commit is contained in:
Em Zhan 2022-11-16 07:36:09 -06:00 committed by GitHub
parent 18c71dcd25
commit 34db08bb8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 56 additions and 4 deletions

View File

@ -231,7 +231,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
throwOutdatedRequest(importer)
}
if (!imports.length) {
if (!imports.length && !(this as any)._addedImports) {
importerModule.isSelfAccepting = false
isDebug &&
debug(
@ -263,7 +263,8 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
const normalizeUrl = async (
url: string,
pos: number
pos: number,
forceSkipImportAnalysis: boolean = false
): Promise<[string, string]> => {
url = stripBase(url, base)
@ -364,7 +365,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
const depModule = await moduleGraph.ensureEntryFromUrl(
unwrapId(url),
ssr,
canSkipImportAnalysis(url)
canSkipImportAnalysis(url) || forceSkipImportAnalysis
)
if (depModule.lastHMRTimestamp > 0) {
url = injectQuery(url, `t=${depModule.lastHMRTimestamp}`)
@ -667,7 +668,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
if (pluginImports) {
;(
await Promise.all(
[...pluginImports].map((id) => normalizeUrl(id, 0))
[...pluginImports].map((id) => normalizeUrl(id, 0, true))
)
).forEach(([url]) => importedUrls.add(url))
}

View File

@ -0,0 +1,9 @@
import { expect, test } from 'vitest'
import { editFile, page, untilUpdated } from '~utils'
test('should re-run transform when plugin-dep file is edited', async () => {
expect(await page.textContent('#transform-count')).toBe('1')
await editFile('plugin-dep.js', (str) => str)
await untilUpdated(() => page.textContent('#transform-count'), '2')
})

View File

@ -0,0 +1,3 @@
<div id="transform-count"></div>
<script type="module" src="./index.js"></script>

View File

@ -0,0 +1,2 @@
// 'TRANSFORM_COUNT' is injected by the transform plugin
document.getElementById('transform-count').innerHTML = TRANSFORM_COUNT

View File

@ -0,0 +1,11 @@
{
"name": "test-transform-plugin",
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../vite/bin/vite",
"serve": "vite preview"
}
}

View File

@ -0,0 +1 @@
// Empty file for detecting changes in tests

View File

@ -0,0 +1,25 @@
const { resolve } = require('node:path')
const { normalizePath } = require('vite')
let transformCount = 1
const transformPlugin = {
name: 'transform',
transform(code, id) {
if (id === normalizePath(resolve(__dirname, 'index.js'))) {
// Ensure `index.js` is reevaluated if 'plugin-dep.js' is changed
this.addWatchFile('./plugin-dep.js')
return `
// Inject TRANSFORM_COUNT
let TRANSFORM_COUNT = ${transformCount++};
${code}
`
}
}
}
module.exports = {
plugins: [transformPlugin]
}