Skip to content

Commit dc1d54d

Browse files
authored
Add sandbox improvements (codesandbox#3337)
* Add sandbox improvements * Rename entities * Lazy load react devtools * Lazy load jest-lite * Fix react devtools and HMR * Simplify yarn.lock
1 parent ee78057 commit dc1d54d

File tree

25 files changed

+125
-97
lines changed

25 files changed

+125
-97
lines changed

packages/app/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@
177177
"react-color": "^2.17.3",
178178
"react-content-loader": "^4.2.2",
179179
"react-day-picker": "^7.2.4",
180-
"react-devtools-inline": "^4.0.0",
180+
"react-devtools-inline": "^4.4.0",
181181
"react-dnd": "^9.4.0",
182182
"react-dnd-html5-backend": "^9.4.0",
183183
"react-dom": "^16.9.0",

packages/app/src/app/components/Preview/DevTools/React-Devtools/index.tsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useEffect, useState, useContext, useCallback } from 'react';
22
import delay from '@codesandbox/common/lib/utils/delay';
3-
import { initialize } from 'react-devtools-inline/frontend';
4-
import { dispatch, actions } from 'codesandbox-api';
3+
import * as reactDevtools from 'react-devtools-inline/frontend';
4+
import { dispatch, actions, listen } from 'codesandbox-api';
55
import { ThemeContext } from 'styled-components';
66

77
import { Container } from './elements';
@@ -28,15 +28,11 @@ const DevTools = (props: DevToolProps) => {
2828
if (iframe) {
2929
const { contentWindow } = iframe;
3030

31-
setDevTools(initialize(contentWindow));
32-
iframe.onload = () => {
33-
contentWindow.postMessage(
34-
{
35-
type: 'activate',
36-
},
37-
'*'
38-
);
39-
};
31+
listen((message: { type: string | 'activate-react-devtools' }) => {
32+
if (message.type === 'activate-react-devtools') {
33+
setDevTools(reactDevtools.initialize(contentWindow));
34+
}
35+
});
4036
}
4137
}, []);
4238

File renamed without changes.

packages/app/src/sandbox/compile.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import defaultBoilerplates from './boilerplates/default-boilerplates';
2121
import createCodeSandboxOverlay from './codesandbox-overlay';
2222
import getPreset from './eval';
2323
import { consumeCache, deleteAPICache, saveCache } from './eval/cache';
24-
import { Module } from './eval/entities/module';
24+
import { Module } from './eval/types/module';
2525
import Manager from './eval/manager';
2626
import TranspiledModule from './eval/transpiled-module';
2727
import handleExternalResources from './external-resources';
@@ -683,8 +683,9 @@ async function compile({
683683
firstLoad
684684
);
685685

686-
setTimeout(() => {
686+
setTimeout(async () => {
687687
try {
688+
await manager.initializeTestRunner();
688689
sendTestCount(manager, modules);
689690
} catch (e) {
690691
if (process.env.NODE_ENV === 'development') {
@@ -739,6 +740,8 @@ async function compile({
739740
type: 'state',
740741
state: managerState,
741742
});
743+
744+
manager.isFirstLoad = false;
742745
}
743746

744747
if (firstLoad) {

packages/app/src/sandbox/eval/errors/module-error.js renamed to packages/app/src/sandbox/eval/errors/module-error.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const ErrorClass = Error;
66
Author Tobias Koppers @sokra
77
*/
88
export default class ModuleError extends ErrorClass {
9+
path: string;
10+
error: Error;
911
constructor(module, err) {
1012
super();
1113

packages/app/src/sandbox/eval/errors/module-warning.js renamed to packages/app/src/sandbox/eval/errors/module-warning.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ export default class ModuleWarning extends Error {
3030
path: string;
3131
message: string;
3232
warning: string;
33-
message: string;
3433
severity: 'notice' | 'warning';
35-
source: ?string;
36-
lineNumber: ?number;
37-
columnNumber: ?number;
34+
source?: string;
35+
lineNumber?: number;
36+
columnNumber?: number;
3837
}

packages/app/src/sandbox/eval/loaders/dependency-resolver.js renamed to packages/app/src/sandbox/eval/loaders/dependency-resolver.ts

File renamed without changes.

packages/app/src/sandbox/eval/loaders/eval.js renamed to packages/app/src/sandbox/eval/loaders/eval.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const g = typeof window === 'undefined' ? self : window;
55
const requestFrame = (() => {
66
const raf =
77
g.requestAnimationFrame ||
8+
// @ts-ignore
89
g.mozRequestAnimationFrame ||
910
g.webkitRequestAnimationFrame ||
1011
function(fn) {
@@ -21,15 +22,16 @@ const hasGlobalDeclaration = /^const global/m;
2122
export default function(
2223
code: string,
2324
require: Function,
24-
module: Object,
25+
module: { exports: any },
2526
env: Object = {},
2627
globals: Object = {},
27-
{ asUMD = false }: { asUMD: boolean } = {}
28+
{ asUMD = false }: { asUMD?: boolean } = {}
2829
) {
2930
const { exports } = module;
3031

3132
const global = g;
3233
const process = buildProcess(env);
34+
// @ts-ignore
3335
g.global = global;
3436

3537
const allGlobals = {

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

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { ParsedConfigurationFiles } from '@codesandbox/common/lib/templates/temp
99
import DependencyNotFoundError from 'sandbox-hooks/errors/dependency-not-found-error';
1010
import ModuleNotFoundError from 'sandbox-hooks/errors/module-not-found-error';
1111

12-
import { Module } from './entities/module';
12+
import { Module } from './types/module';
1313
import TranspiledModule, {
1414
ChildModule,
1515
SerializedTranspiledModule,
@@ -22,7 +22,6 @@ import fetchModule, {
2222
} from './npm/fetch-npm-module';
2323
import coreLibraries from './npm/get-core-libraries';
2424
import getDependencyName from './utils/get-dependency-name';
25-
import TestRunner from './tests/jest-lite';
2625
import dependenciesToQuery from '../npm/dependencies-to-query';
2726
import { packageFilter } from './utils/resolve-utils';
2827

@@ -117,7 +116,7 @@ export default class Manager {
117116
hardReload: boolean;
118117
hmrStatus: HMRStatus = 'idle';
119118
hmrStatusChangeListeners: Set<(status: HMRStatus) => void>;
120-
testRunner: TestRunner;
119+
testRunner: import('./tests/jest-lite').default;
121120
isFirstLoad: boolean;
122121

123122
fileResolver: IFileResolver | undefined;
@@ -157,7 +156,6 @@ export default class Manager {
157156

158157
this.modules = modules;
159158
Object.keys(modules).forEach(k => this.addModule(modules[k]));
160-
this.testRunner = new TestRunner(this);
161159

162160
getGlobal().manager = this;
163161
if (process.env.NODE_ENV === 'development') {
@@ -184,6 +182,18 @@ export default class Manager {
184182
}
185183
}
186184

185+
async initializeTestRunner() {
186+
if (this.testRunner) {
187+
return this.testRunner;
188+
}
189+
190+
this.testRunner = await import(
191+
/* webpackChunkName: 'jest-lite' */ './tests/jest-lite'
192+
).then(({ default: TestRunner }) => new TestRunner(this));
193+
194+
return this.testRunner;
195+
}
196+
187197
setupFileResolver() {
188198
const protocol = new Protocol('file-resolver', () => true, window.parent);
189199
this.fileResolver = {
@@ -308,10 +318,7 @@ export default class Manager {
308318

309319
evaluateModule(
310320
module: Module,
311-
{
312-
force = false,
313-
testGlobals = false,
314-
}: { force?: boolean; testGlobals?: boolean } = {}
321+
{ force = false, globals = {} }: { force?: boolean; globals?: object } = {}
315322
) {
316323
if (this.hardReload && !this.isFirstLoad) {
317324
// Do a hard reload
@@ -334,7 +341,7 @@ export default class Manager {
334341
const exports = this.evaluateTranspiledModule(
335342
transpiledModule,
336343
undefined,
337-
{ force, testGlobals }
344+
{ force, globals }
338345
);
339346

340347
this.setHmrStatus('idle');
@@ -349,16 +356,13 @@ export default class Manager {
349356
evaluateTranspiledModule(
350357
transpiledModule: TranspiledModule,
351358
initiator?: TranspiledModule,
352-
{
353-
force = false,
354-
testGlobals = false,
355-
}: { force?: boolean; testGlobals?: boolean } = {}
359+
{ force = false, globals = {} }: { force?: boolean; globals?: object } = {}
356360
) {
357361
if (force && transpiledModule.compilation) {
358362
transpiledModule.compilation = null;
359363
}
360364

361-
return transpiledModule.evaluate(this, { force, testGlobals }, initiator);
365+
return transpiledModule.evaluate(this, { force, globals }, initiator);
362366
}
363367

364368
addModule(module: Module) {
@@ -933,7 +937,6 @@ export default class Manager {
933937
}): Promise<Array<TranspiledModule>> {
934938
this.transpileJobs = {};
935939
this.hardReload = false;
936-
this.isFirstLoad = false;
937940

938941
this.modules = modules;
939942

@@ -984,15 +987,15 @@ export default class Manager {
984987
this.hardReload = this.configurations.sandbox.parsed.hardReloadOnChange;
985988
}
986989

987-
const modulesWithWErrors = this.getTranspiledModules().filter(t => {
990+
const modulesWithErrors = this.getTranspiledModules().filter(t => {
988991
if (t.hasMissingDependencies) {
989992
t.resetTranspilation();
990993
}
991994
return t.errors.length > 0 || t.hasMissingDependencies;
992995
});
993996
const flattenedTModulesToUpdate = (flattenDeep([
994997
tModulesToUpdate,
995-
modulesWithWErrors,
998+
modulesWithErrors,
996999
]) as unknown) as TranspiledModule[];
9971000

9981001
const allModulesToUpdate = uniq(flattenedTModulesToUpdate);

packages/app/src/sandbox/eval/npm/fetch-npm-module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import resolve from 'browser-resolve';
44
import DependencyNotFoundError from 'sandbox-hooks/errors/dependency-not-found-error';
55

66
import delay from 'sandbox/utils/delay';
7-
import { Module } from '../entities/module';
7+
import { Module } from '../types/module';
88
import Manager from '../manager';
99

1010
import getDependencyName from '../utils/get-dependency-name';

0 commit comments

Comments
 (0)