Skip to content

Commit b34e74c

Browse files
authored
fix(hmr): use dispose global module and add checks for babel plugin (#5)
1 parent 58415bf commit b34e74c

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/react-native/preview-registry.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ComponentType } from "react";
2-
import { Metadata, MetroModule, Preview } from "../shared/types";
2+
import { Metadata, Module, Preview } from "../shared/types";
33
import { createSignalMap } from "../utils/signal-map";
44
import { client } from "./setup-plugin";
55

@@ -17,15 +17,18 @@ export function getPreviewComponents(): Preview[] {
1717
}
1818

1919
export function getComponentByName(name: string) {
20-
return flattenRegistryEntries().find((entry) => entry.name === name)?.component || null;
20+
return (
21+
flattenRegistryEntries().find((entry) => entry.name === name)?.component ||
22+
null
23+
);
2124
}
2225

2326
/**
2427
*
2528
* @internal
2629
*/
2730
const __registerPreviewInternal = (
28-
module: MetroModule,
31+
module: Module,
2932
name: string,
3033
component: ComponentType,
3134
metadata?: Metadata
@@ -36,26 +39,23 @@ const __registerPreviewInternal = (
3639
module.id === undefined
3740
) {
3841
console.warn(
39-
`Cannot register preview "${name}" in production or in non Metro environment.`
42+
`[Rozenite Preview Plugin] Cannot register preview "${name}" in production or in non Metro environment.`
4043
);
4144
return;
4245
}
4346

4447
if (metadata?.isInsideReactComponent) {
4548
console.error(
46-
'Do not call "registerPreview" inside a React lifecycle. Use it at the top level of your module.'
49+
'[Rozenite Preview Plugin] Do not call "registerPreview" inside a React lifecycle. Use it at the top level of your module.'
4750
);
4851
return;
4952
}
5053

5154
let moduleId = module.id;
5255

53-
const originalDisposeCallback = module.hot._disposeCallback;
54-
55-
module.hot._disposeCallback = () => {
56-
originalDisposeCallback?.();
56+
module.hot.dispose(() => {
5757
registry.delete(moduleId);
58-
};
58+
});
5959

6060
const current = registry.get(module.id) || [];
6161

@@ -73,8 +73,16 @@ const __registerPreviewInternal = (
7373
* @param name Preview name
7474
* @param component React component
7575
*/
76-
export function registerPreview(name: string, component: React.ComponentType) {
76+
export function registerPreview(name: string, component: React.ComponentType) {
77+
if (arguments.length !== 4) {
78+
console.warn("[Rozenite Preview Plugin] Babel plugin is not configured correctly. This will result in a broken preview.");
79+
return;
80+
}
81+
7782
__registerPreviewInternal(
78-
...(arguments as unknown as [MetroModule, string, ComponentType, Metadata])
83+
arguments[0] as Module,
84+
arguments[1] as string,
85+
arguments[2] as React.ComponentType,
86+
arguments[3] as Metadata
7987
);
8088
}

src/shared/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ type ModuleID = number;
3333

3434
type Exports = any;
3535

36-
// Ref: https://github.com/facebook/metro/blob/a81c99cf103be00181aa635fef94c6e3385a47bb/packages/metro-runtime/src/polyfills/require.js#L51
37-
export type MetroModule = {
36+
// Ref Metro: https://github.com/facebook/metro/blob/a81c99cf103be00181aa635fef94c6e3385a47bb/packages/metro-runtime/src/polyfills/require.js#L51
37+
// Ref RsPack: https://rspack.rs/api/runtime-api/hmr#dispose-or-adddisposehandler
38+
export type Module = {
3839
id?: ModuleID;
3940
exports: Exports;
4041
hot?: HotModuleReloadingData;

0 commit comments

Comments
 (0)