Skip to content

Commit ea540b5

Browse files
committed
Fix HMR when non-entry file is edited
1 parent 5ef3845 commit ea540b5

File tree

5 files changed

+45
-39
lines changed

5 files changed

+45
-39
lines changed

packages/app/src/sandbox/compile.js

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -453,48 +453,14 @@ async function compile({
453453
await manager.verifyTreeTranspiled();
454454
await manager.transpileModules(managerModuleToTranspile);
455455

456-
const managerTranspiledModuleToTranspile = manager.getTranspiledModule(
457-
managerModuleToTranspile
458-
);
459-
460456
debug(`Transpilation time ${Date.now() - t}ms`);
461457

462458
dispatch({ type: 'status', status: 'evaluating' });
463459

464460
if (!skipEval) {
465461
resetScreen();
466462

467-
if (
468-
!manager.webpackHMR &&
469-
(!managerTranspiledModuleToTranspile.compilation || isModuleView)
470-
) {
471-
try {
472-
const children = document.body.children;
473-
// Do unmounting for react
474-
if (
475-
manifest &&
476-
manifest.dependencies.find(n => n.name === 'react-dom')
477-
) {
478-
const reactDOMModule = manager.resolveModule('react-dom', '');
479-
const reactDOM = manager.evaluateModule(reactDOMModule);
480-
481-
reactDOM.unmountComponentAtNode(document.body);
482-
483-
for (let i = 0; i < children.length; i += 1) {
484-
if (children[i].tagName === 'DIV') {
485-
reactDOM.unmountComponentAtNode(children[i]);
486-
}
487-
}
488-
}
489-
} catch (e) {
490-
/* don't do anything with this error */
491-
492-
if (process.env.NODE_ENV === 'development') {
493-
console.error('Problem while cleaning up');
494-
console.error(e);
495-
}
496-
}
497-
}
463+
manager.preset.preEvaluate(manager);
498464

499465
if (!manager.webpackHMR && !manager.preset.htmlDisabled) {
500466
const htmlModulePath = templateDefinition

packages/app/src/sandbox/console/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { dispatch } from 'codesandbox-api';
2-
import { Hook } from 'console-feed';
2+
import Hook from 'console-feed';
33

44
export default function setupConsole() {
55
Hook(window.console, log => {

packages/app/src/sandbox/eval/presets/create-react-app/index.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,39 @@ export default function initialize() {
1010
'create-react-app',
1111
['web.js', 'js', 'json', 'web.jsx', 'jsx'],
1212
{ 'react-native': 'react-native-web' },
13-
{ hasDotEnv: true }
13+
{
14+
hasDotEnv: true,
15+
preEvaluate: manager => {
16+
if (!manager.webpackHMR) {
17+
try {
18+
const children = document.body.children;
19+
// Do unmounting for react
20+
if (
21+
manager.manifest &&
22+
manager.manifest.dependencies.find(n => n.name === 'react-dom')
23+
) {
24+
const reactDOMModule = manager.resolveModule('react-dom', '');
25+
const reactDOM = manager.evaluateModule(reactDOMModule);
26+
27+
reactDOM.unmountComponentAtNode(document.body);
28+
29+
for (let i = 0; i < children.length; i += 1) {
30+
if (children[i].tagName === 'DIV') {
31+
reactDOM.unmountComponentAtNode(children[i]);
32+
}
33+
}
34+
}
35+
} catch (e) {
36+
/* don't do anything with this error */
37+
38+
if (process.env.NODE_ENV === 'development') {
39+
console.error('Problem while cleaning up');
40+
console.error(e);
41+
}
42+
}
43+
}
44+
},
45+
}
1446
);
1547

1648
preset.registerTranspiler(module => /\.css$/.test(module.path), [

packages/app/src/sandbox/eval/presets/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,19 @@ export default class Preset {
4343
hasDotEnv: boolean;
4444

4545
/**
46-
* Code to run before evaluating entry
46+
* Code to run before evaluating and transpiling entry
4747
*/
4848
setup: LifeCycleFunction;
4949
/**
5050
* Code to run after done
5151
*/
5252
teardown: LifeCycleFunction;
5353

54+
/**
55+
* Code to run before evaluation
56+
*/
57+
preEvaluate: LifeCycleFunction;
58+
5459
constructor(
5560
name: string,
5661
ignoredExtensions: ?Array<string>,
@@ -60,9 +65,11 @@ export default class Preset {
6065
setup,
6166
teardown,
6267
htmlDisabled,
68+
preEvaluate,
6369
}: {
6470
hasDotEnv?: boolean,
6571
setup?: LifeCycleFunction,
72+
preEvaluate?: LifeCycleFunction,
6673
teardown?: LifeCycleFunction,
6774
htmlDisabled?: boolean,
6875
} = {}
@@ -80,6 +87,7 @@ export default class Preset {
8087
const noop = () => {};
8188
this.setup = setup || noop;
8289
this.teardown = teardown || noop;
90+
this.preEvaluate = preEvaluate || noop;
8391
this.htmlDisabled = htmlDisabled || false;
8492
}
8593

packages/app/src/sandbox/eval/transpiled-module.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ export default class TranspiledModule {
713713
) {
714714
return this.compilation.exports;
715715
}
716-
} else if (this.compilation && this.compilation.exports) {
716+
} else if (this.compilation && this.compilation.exports && !this.isEntry) {
717717
return this.compilation.exports;
718718
}
719719

0 commit comments

Comments
 (0)