Skip to content

Commit 606ff76

Browse files
authored
Fix recompilation for cached modules (codesandbox#607)
* Fix recompilation for cached modules * Fix test recompilation
1 parent 19203f3 commit 606ff76

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

packages/app/src/sandbox/compile.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ async function updateManager(
220220
}
221221

222222
manager.updateConfigurations(configurations);
223-
return await manager.updateData(managerModules);
223+
return manager.updateData(managerModules);
224224
}
225225

226226
function initializeResizeListener() {
@@ -352,7 +352,14 @@ async function compile({
352352

353353
resetScreen();
354354

355-
if (!manager.webpackHMR) {
355+
const managerTranspiledModuleToTranspile = manager.getTranspiledModule(
356+
managerModuleToTranspile
357+
);
358+
359+
if (
360+
!manager.webpackHMR &&
361+
!managerTranspiledModuleToTranspile.compilation
362+
) {
356363
try {
357364
const children = document.body.children;
358365
// Do unmounting for react
@@ -372,20 +379,23 @@ async function compile({
372379
/* don't do anything with this error */
373380
}
374381
}
382+
375383
if ((!manager.webpackHMR || firstLoad) && !manager.preset.htmlDisabled) {
376-
const htmlModule =
377-
modules[
378-
templateDefinition
379-
.getHTMLEntries(configurations)
380-
.find(p => modules[p])
381-
];
382-
383-
const html = htmlModule
384-
? htmlModule.code
385-
: template === 'vue-cli'
386-
? '<div id="app"></div>'
387-
: '<div id="root"></div>';
388-
document.body.innerHTML = html;
384+
if (!managerTranspiledModuleToTranspile.compilation) {
385+
const htmlModule =
386+
modules[
387+
templateDefinition
388+
.getHTMLEntries(configurations)
389+
.find(p => modules[p])
390+
];
391+
392+
const html = htmlModule
393+
? htmlModule.code
394+
: template === 'vue-cli'
395+
? '<div id="app"></div>'
396+
: '<div id="root"></div>';
397+
document.body.innerHTML = html;
398+
}
389399
}
390400

391401
const tt = Date.now();
@@ -448,10 +458,6 @@ async function compile({
448458

449459
debug(`Total time: ${Date.now() - startTime}ms`);
450460

451-
dispatch({
452-
type: 'success',
453-
});
454-
455461
manager.save();
456462
} catch (e) {
457463
console.log('Error in sandbox:');

packages/app/src/sandbox/eval/manager.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ export default class Manager {
194194
debug(`Loaded manifest.`);
195195
}
196196

197-
evaluateModule(module: Module) {
197+
evaluateModule(module: Module, force: boolean = false) {
198198
if (this.hardReload) {
199199
// Do a hard reload
200200
document.location.reload();
@@ -210,6 +210,10 @@ export default class Manager {
210210

211211
const transpiledModule = this.getTranspiledModule(module);
212212

213+
if (force && transpiledModule.compilation) {
214+
transpiledModule.compilation = null;
215+
}
216+
213217
try {
214218
const exports = this.evaluateTranspiledModule(transpiledModule);
215219

packages/app/src/sandbox/eval/tests/jest-lite.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export default class TestRunner {
208208

209209
if (testModule) {
210210
await this.manager.transpileModules(testModule, true);
211-
this.manager.evaluateModule(testModule);
211+
this.manager.evaluateModule(testModule, true);
212212
}
213213

214214
if (this.manager.modules) {
@@ -223,7 +223,7 @@ export default class TestRunner {
223223
await Promise.all(
224224
tests.map(async t => {
225225
try {
226-
this.manager.evaluateModule(t);
226+
this.manager.evaluateModule(t, true);
227227
this.ranTests.add(t.path);
228228
} catch (e) {
229229
this.ranTests.delete(t.path);

0 commit comments

Comments
 (0)