Skip to content

Commit

Permalink
feat: support import.meta.hot?.accept (#12053)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Feb 15, 2023
1 parent b2f2a26 commit 081c27f
Show file tree
Hide file tree
Showing 6 changed files with 26 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 @@ -406,19 +406,20 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
const prop = source.slice(end, end + 4)
if (prop === '.hot') {
hasHMR = true
if (source.slice(end + 4, end + 11) === '.accept') {
const endHot = end + 4 + (source[end + 4] === '?' ? 1 : 0)
if (source.slice(endHot, endHot + 7) === '.accept') {
// further analyze accepted modules
if (source.slice(end + 4, end + 18) === '.acceptExports') {
if (source.slice(endHot, endHot + 14) === '.acceptExports') {
lexAcceptedHmrExports(
source,
source.indexOf('(', end + 18) + 1,
source.indexOf('(', endHot + 14) + 1,
acceptedExports,
)
isPartiallySelfAccepting = true
} else if (
lexAcceptedHmrDeps(
source,
source.indexOf('(', end + 11) + 1,
source.indexOf('(', endHot + 7) + 1,
acceptedUrls,
)
) {
Expand Down
12 changes: 12 additions & 0 deletions playground/hmr/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,4 +774,16 @@ if (import.meta.hot) {
'parent:child',
)
})

test('import.meta.hot?.accept', async () => {
const el = await page.$('.optional-chaining')
await untilBrowserLogAfter(
() =>
editFile('optional-chaining/child.js', (code) =>
code.replace('const foo = 1', 'const foo = 2'),
),
'(optional-chaining) child update',
)
await untilUpdated(() => el.textContent(), '2')
})
}
1 change: 1 addition & 0 deletions playground/hmr/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { foo as depFoo, nestedFoo } from './hmrDep'
import './importing-updated'
import './invalidation/parent'
import './file-delete-restore'
import './optional-chaining/parent'

export const foo = 1
text('.app', foo)
Expand Down
1 change: 1 addition & 0 deletions playground/hmr/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@
<div class="import-image"></div>
<div class="importing-reloaded"></div>
<div class="file-delete-restore"></div>
<div class="optional-chaining"></div>
1 change: 1 addition & 0 deletions playground/hmr/optional-chaining/child.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 1
6 changes: 6 additions & 0 deletions playground/hmr/optional-chaining/parent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { foo } from './child'

import.meta.hot?.accept('./child', ({ foo }) => {
console.log('(optional-chaining) child update')
document.querySelector('.optional-chaining').textContent = foo
})

0 comments on commit 081c27f

Please sign in to comment.