Skip to content

Commit

Permalink
fix: add watch in fallback file load
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Nov 10, 2023
1 parent 374e6fd commit b70073f
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/vite/src/node/server/pluginContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,9 @@ export async function createPluginContainer(
return result
}
}

watchFiles.add(id)
if (watcher) ensureWatchedFile(watcher, id, root)
return null
},

Expand Down
10 changes: 10 additions & 0 deletions playground/hmr-root/__tests__/hmr-root.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { expect, test } from 'vitest'

import { editFile, isServe, page, untilUpdated } from '~utils'

test.runIf(isServe)('should watch files outside root', async () => {
expect(await page.textContent('#foo')).toBe('foo')
editFile('foo.js', (code) => code.replace("'foo'", "'foobar'"))
await page.waitForEvent('load')
await untilUpdated(async () => await page.textContent('#foo'), 'foobar')
})
1 change: 1 addition & 0 deletions playground/hmr-root/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'foo'
7 changes: 7 additions & 0 deletions playground/hmr-root/root/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div id="foo"></div>

<script type="module">
import { foo } from '../foo.js'

document.querySelector('#foo').textContent = foo
</script>
9 changes: 9 additions & 0 deletions playground/hmr-root/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import path from 'node:path'
import url from 'node:url'
import { defineConfig } from 'vite'

const __dirname = path.dirname(url.fileURLToPath(import.meta.url))

export default defineConfig({
root: path.join(__dirname, './root'),
})
14 changes: 14 additions & 0 deletions playground/hmr/__tests__/root/root-hmr.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { expect, test } from 'vitest'

import { editFile, isServe, page, untilUpdated } from '~utils'

test.runIf(isServe)('should render', async () => {
expect(await page.textContent('#foo')).toBe('foo')
})

test.runIf(isServe)('should watch files outside root', async () => {
expect(await page.textContent('#foo')).toBe('bar')
editFile('foo.js', (code) => code.replace("'foo'", "'foobar'"))
await page.waitForEvent('load')
await untilUpdated(async () => await page.textContent('#foo'), 'foobar')
})

0 comments on commit b70073f

Please sign in to comment.