Skip to content

Commit 081c27f

Browse files
authoredFeb 15, 2023
feat: support import.meta.hot?.accept (#12053)
1 parent b2f2a26 commit 081c27f

File tree

6 files changed

+26
-4
lines changed

6 files changed

+26
-4
lines changed
 

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -406,19 +406,20 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
406406
const prop = source.slice(end, end + 4)
407407
if (prop === '.hot') {
408408
hasHMR = true
409-
if (source.slice(end + 4, end + 11) === '.accept') {
409+
const endHot = end + 4 + (source[end + 4] === '?' ? 1 : 0)
410+
if (source.slice(endHot, endHot + 7) === '.accept') {
410411
// further analyze accepted modules
411-
if (source.slice(end + 4, end + 18) === '.acceptExports') {
412+
if (source.slice(endHot, endHot + 14) === '.acceptExports') {
412413
lexAcceptedHmrExports(
413414
source,
414-
source.indexOf('(', end + 18) + 1,
415+
source.indexOf('(', endHot + 14) + 1,
415416
acceptedExports,
416417
)
417418
isPartiallySelfAccepting = true
418419
} else if (
419420
lexAcceptedHmrDeps(
420421
source,
421-
source.indexOf('(', end + 11) + 1,
422+
source.indexOf('(', endHot + 7) + 1,
422423
acceptedUrls,
423424
)
424425
) {

‎playground/hmr/__tests__/hmr.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -774,4 +774,16 @@ if (import.meta.hot) {
774774
'parent:child',
775775
)
776776
})
777+
778+
test('import.meta.hot?.accept', async () => {
779+
const el = await page.$('.optional-chaining')
780+
await untilBrowserLogAfter(
781+
() =>
782+
editFile('optional-chaining/child.js', (code) =>
783+
code.replace('const foo = 1', 'const foo = 2'),
784+
),
785+
'(optional-chaining) child update',
786+
)
787+
await untilUpdated(() => el.textContent(), '2')
788+
})
777789
}

‎playground/hmr/hmr.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { foo as depFoo, nestedFoo } from './hmrDep'
33
import './importing-updated'
44
import './invalidation/parent'
55
import './file-delete-restore'
6+
import './optional-chaining/parent'
67

78
export const foo = 1
89
text('.app', foo)

‎playground/hmr/index.html

+1
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@
3131
<div class="import-image"></div>
3232
<div class="importing-reloaded"></div>
3333
<div class="file-delete-restore"></div>
34+
<div class="optional-chaining"></div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const foo = 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { foo } from './child'
2+
3+
import.meta.hot?.accept('./child', ({ foo }) => {
4+
console.log('(optional-chaining) child update')
5+
document.querySelector('.optional-chaining').textContent = foo
6+
})

0 commit comments

Comments
 (0)
Please sign in to comment.