Skip to content

Commit

Permalink
fix: #1478 support react class-component hot-update (#1489)
Browse files Browse the repository at this point in the history
* fix: #1478 support react class-component hot-update

* refactor: 🔥 revert example code

---------

Co-authored-by: pshu <[email protected]>
  • Loading branch information
jeasonnow and stormslowly authored Aug 29, 2024
1 parent ec7a39b commit 8775b18
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion crates/mako/src/visitors/react.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,21 @@ fn react_refresh_module_postfix(context: &Arc<Context>) -> Box<dyn VisitMut> {
code: r#"
if (prevRefreshReg) self.$RefreshReg$ = prevRefreshReg;
if (prevRefreshSig) self.$RefreshSig$ = prevRefreshSig;
function registerClassComponent(filename, moduleExports) {
for (const key in moduleExports) {
try {
if (key === "__esModule") continue;
const exportValue = moduleExports[key];
if (RefreshRuntime.isLikelyComponentType(exportValue) && exportValue.prototype && exportValue.prototype.isReactComponent) {
RefreshRuntime.register(exportValue, filename + " " + key);
}
} catch (e) {
// in case the moduleExports[key] is not accessible due depedence loop
}
}
}
function $RefreshIsReactComponentLike$(moduleExports) {
if (RefreshRuntime.isLikelyComponentType(moduleExports.default || moduleExports)) {
if (RefreshRuntime.isLikelyComponentType(moduleExports || moduleExports.default)) {
return true;
}
for (var key in moduleExports) {
Expand All @@ -175,6 +188,7 @@ function $RefreshIsReactComponentLike$(moduleExports) {
}
return false;
}
registerClassComponent(module.id, module.exports);
if ($RefreshIsReactComponentLike$(module.exports)) {
module.meta.hot.accept();
RefreshRuntime.performReactRefresh();
Expand Down

0 comments on commit 8775b18

Please sign in to comment.