-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: HRM API has been re-designed. - All HMR APIs are now exposed on `import.meta.hot` and HMR API calls should be nested inside `if (import.meta.hot) {}` conditional checks. - `import.meta.hot.accept()` is now only used for self-accepting updates. - `import.meta.hot.acceptDeps()` is now used for accepting dependency updates without re-instantiating acceptor. - `import.meta.hot.data` is an object that is persisted across hot updates of a module. - `import.meta.hot.dispose()` callback now receives the persisted data object. - `import.meta.hot.decline()` can be used to decline updates and if the module is affected in an HMR update chain and force full page reload. - `import.meta.hot.invalidate()` can be used inside an acceptance callback to conditionally reject the update and force full page reload. See `hmr.d.ts` for full API definitions.
- Loading branch information
Showing
10 changed files
with
366 additions
and
312 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,17 @@ | ||
export declare const hot: { | ||
// single dep | ||
accept(dep: string, cb?: (newModule: any) => void): void | ||
// multiple deps | ||
accept(deps: string[], cb?: (newModules: any[]) => void): void | ||
// self-accepting | ||
accept(cb: (newModule: any) => void): void | ||
// dispose | ||
dispose(cb: () => void): void | ||
// custom events | ||
on(event: string, cb: (data: any) => void): void | ||
declare interface ImportMeta { | ||
hot: { | ||
data: any | ||
|
||
accept(): void | ||
accept(cb: (mod: any) => void): void | ||
|
||
acceptDeps(dep: string, cb: (mod: any) => void): void | ||
acceptDeps(deps: string[], cb: (mods: any[]) => void): void | ||
|
||
dispose(cb: (data: any) => void): void | ||
decline(): void | ||
invalidate(): void | ||
|
||
on(event: string, cb: (...args: any[]) => void): void | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,25 @@ | ||
import { hot } from 'vite/hmr' | ||
import './testHmrManualDep' | ||
|
||
export const foo = 1 | ||
|
||
if (__DEV__) { | ||
hot.accept(({ foo }) => { | ||
if (import.meta.hot) { | ||
import.meta.hot.accept(({ foo }) => { | ||
console.log('(self-accepting)1.foo is now:', foo) | ||
}) | ||
|
||
hot.accept(({ foo }) => { | ||
import.meta.hot.accept(({ foo }) => { | ||
console.log('(self-accepting)2.foo is now:', foo) | ||
}) | ||
|
||
hot.dispose(() => { | ||
import.meta.hot.dispose(() => { | ||
console.log(`foo was: ${foo}`) | ||
}) | ||
|
||
hot.accept('./testHmrManualDep.js', ({ foo }) => { | ||
import.meta.hot.acceptDeps('./testHmrManualDep.js', ({ foo }) => { | ||
console.log('(single dep) foo is now:', foo) | ||
}) | ||
|
||
hot.accept(['./testHmrManualDep.js'], (modules) => { | ||
import.meta.hot.acceptDeps(['./testHmrManualDep.js'], (modules) => { | ||
console.log('(multiple deps) foo is now:', modules[0].foo) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { hot } from 'vite/hmr' | ||
|
||
export const foo = 1 | ||
|
||
if (__DEV__) { | ||
hot.dispose(() => { | ||
if (import.meta.hot) { | ||
const data = import.meta.hot.data | ||
console.log(`(dep) foo from dispose: ${data.fromDispose}`) | ||
|
||
import.meta.hot.dispose((data) => { | ||
console.log(`(dep) foo was: ${foo}`) | ||
data.fromDispose = foo * 10 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.