Skip to content

Commit

Permalink
refactor(dev): early return for non-hmr-boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
pcattori committed Jul 3, 2023
1 parent 7e2512e commit 3401b6b
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions packages/remix-dev/compiler/js/plugins/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ $id$
${lastModified ? `import.meta.hot.lastModified = "${lastModified}";` : ""}
}`.replace(/\$id\$/g, hmrId);
let sourceCodeWithHMR = hmrPrefix + sourceCode;
let resultCode = sourceCodeWithHMR;

// run babel to add react-refresh
let transformResult = babel.transformSync(sourceCodeWithHMR, {
Expand All @@ -201,28 +200,26 @@ ${lastModified ? `import.meta.hot.lastModified = "${lastModified}";` : ""}

// auto opt-in to accepting fast refresh updates if the module
// has react components
if (IS_FAST_REFRESH_ENABLED.test(jsWithReactRefresh)) {
resultCode =
`
if (!window.$RefreshReg$ || !window.$RefreshSig$ || !window.$RefreshRuntime$) {
console.warn('remix:hmr: React Fast Refresh only works when the Remix compiler is running in development mode.');
} else {
var prevRefreshReg = window.$RefreshReg$;
var prevRefreshSig = window.$RefreshSig$;
window.$RefreshReg$ = (type, id) => {
window.$RefreshRuntime$.register(type, ${JSON.stringify(
hmrId
)} + id);
}
window.$RefreshSig$ = window.$RefreshRuntime$.createSignatureFunctionForTransform;
}
` +
jsWithReactRefresh +
`
window.$RefreshReg$ = prevRefreshReg;
window.$RefreshSig$ = prevRefreshSig;
`;
if (!IS_FAST_REFRESH_ENABLED.test(jsWithReactRefresh)) {
return sourceCodeWithHMR;
}

return resultCode;
return (
`
if (!window.$RefreshReg$ || !window.$RefreshSig$ || !window.$RefreshRuntime$) {
console.warn('remix:hmr: React Fast Refresh only works when the Remix compiler is running in development mode.');
} else {
var prevRefreshReg = window.$RefreshReg$;
var prevRefreshSig = window.$RefreshSig$;
window.$RefreshReg$ = (type, id) => {
window.$RefreshRuntime$.register(type, ${JSON.stringify(hmrId)} + id);
}
window.$RefreshSig$ = window.$RefreshRuntime$.createSignatureFunctionForTransform;
}
` +
jsWithReactRefresh +
`
window.$RefreshReg$ = prevRefreshReg;
window.$RefreshSig$ = prevRefreshSig;
`
);
}

0 comments on commit 3401b6b

Please sign in to comment.