Skip to content

Commit 47d98b8

Browse files
committed
Add conditional module.hot.accept()
1 parent 9b4a675 commit 47d98b8

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

packages/app/src/sandbox/compile.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,21 @@ async function compile({
543543

544544
debug(`Transpilation time ${Date.now() - t}ms`);
545545

546+
// Save the resulting cache from the transpilation. In evaluation we can sometimes refresh,
547+
// which would cause the cache to go wasted
548+
saveCache(
549+
sandboxId,
550+
managerModuleToTranspile,
551+
manager,
552+
changedModuleCount,
553+
firstLoad
554+
).catch(e => {
555+
console.error(
556+
'Something went wrong while saving the cache, this is not critical'
557+
);
558+
console.error(e);
559+
});
560+
546561
dispatch({ type: 'status', status: 'evaluating' });
547562
manager.setStage('evaluation');
548563

@@ -666,14 +681,6 @@ async function compile({
666681
type: 'success',
667682
});
668683

669-
saveCache(
670-
sandboxId,
671-
managerModuleToTranspile,
672-
manager,
673-
changedModuleCount,
674-
firstLoad
675-
);
676-
677684
setTimeout(() => {
678685
try {
679686
sendTestCount(manager, modules);

packages/app/src/sandbox/eval/transpilers/react/refresh-transpiler.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,18 @@ class RefreshTranspiler extends Transpiler {
107107
loaderContext.addDependency('react-refresh/runtime');
108108
loaderContext.emitModule(HELPER_PATH, HELPER_CODE, '/', false, false);
109109

110-
const newCode = getWrapperCode(code);
110+
const isRootModule = loaderContext._module.isEntry;
111+
let newCode = code;
112+
/**
113+
* We want to explicitly add module.hot.accept to the root module, because there is often a component there
114+
* that's not exported. We don't want the window to refresh every time the entry changes. This is the most
115+
* used file in CodeSandbox.
116+
*/
117+
if (isRootModule) {
118+
newCode += '\nmodule.hot.accept();';
119+
} else {
120+
newCode = getWrapperCode(code);
121+
}
111122

112123
return Promise.resolve({
113124
transpiledCode: newCode || '',

0 commit comments

Comments
 (0)