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: make addWatchFile() work (fix #7024) #9723

Merged
merged 6 commits into from Nov 16, 2022
Merged
Show file tree
Hide file tree
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
9 changes: 5 additions & 4 deletions packages/vite/src/node/plugins/importAnalysis.ts
Expand Up @@ -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(
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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}`)
Expand Down Expand Up @@ -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))
}
Expand Down
@@ -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')
})
3 changes: 3 additions & 0 deletions playground/transform-plugin/index.html
@@ -0,0 +1,3 @@
<div id="transform-count"></div>

<script type="module" src="./index.js"></script>
2 changes: 2 additions & 0 deletions playground/transform-plugin/index.js
@@ -0,0 +1,2 @@
// 'TRANSFORM_COUNT' is injected by the transform plugin
document.getElementById('transform-count').innerHTML = TRANSFORM_COUNT
11 changes: 11 additions & 0 deletions playground/transform-plugin/package.json
@@ -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"
}
}
1 change: 1 addition & 0 deletions playground/transform-plugin/plugin-dep.js
@@ -0,0 +1 @@
// Empty file for detecting changes in tests
25 changes: 25 additions & 0 deletions playground/transform-plugin/vite.config.js
@@ -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]
}