Skip to content

Commit

Permalink
fix: make addWatchFile() work (fix vitejs#7024) (vitejs#9723)
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Jones <mattjones701@gmail.com>
  • Loading branch information
2 people authored and Clay Smith committed Nov 23, 2022
1 parent 771a668 commit d9028db
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 4 deletions.
9 changes: 5 additions & 4 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Empty file for detecting changes in tests
25 changes: 25 additions & 0 deletions playground/transform-plugin/vite.config.js
Original file line number Diff line number Diff line change
@@ -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]
}

0 comments on commit d9028db

Please sign in to comment.