Skip to content

Commit dc2a689

Browse files
authored
Fix module.exports (codesandbox#326)
* Fix module.exports * Add MobX sandbox as test sandbox * Fix screenshot
1 parent b6904d1 commit dc2a689

File tree

5 files changed

+17
-24
lines changed

5 files changed

+17
-24
lines changed

generate-test-screenshots.sh

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/bash
2-
docker run -v $(pwd):/app/ --name test-container -t codesandbox/test yarn start:test && \
2+
docker run -v $(pwd):/home/circleci/codesandbox-client -w /home/circleci/codesandbox-client --name test-container -t codesandbox/node-puppeteer yarn start:test && \
33
id=$(docker inspect --format="{{.Id}}" test-container) && \
44
docker exec $(id) yarn test:integrations
31.5 KB
Loading

packages/app/integration-tests/tests/sandboxes.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const SANDBOXES = [
1717
'github/reactjs/redux/tree/master/examples/real-world',
1818
{ id: 'github/CompuIves/codesandbox-presentation', timeout: 60000 },
1919
'lp5rjr0z4z',
20+
'nOymMxyY',
2021
];
2122

2223
SANDBOXES.forEach(sandbox => {

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import buildProcess from './utils/process';
55
export default function(
66
code: string,
77
require: Function,
8-
exports: Object,
8+
module: Object,
99
env: Object = {}
1010
) {
11-
const module = { exports: {} };
11+
const exports = module.exports;
12+
1213
const global = window;
1314
const process = buildProcess(env);
1415
window.global = global;
@@ -19,12 +20,7 @@ export default function(
1920
}\n})`;
2021
(0, eval)(newCode)(require, module, exports, process, global); // eslint-disable-line no-eval
2122

22-
// Choose either the export of __esModule or node
23-
return Object.keys(module.exports || {}).length > 0 ||
24-
(module.exports || {}).constructor !== Object ||
25-
(module.exports && !exports)
26-
? module.exports
27-
: exports;
23+
return module.exports;
2824
} catch (e) {
2925
e.isEvalError = true;
3026

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,9 @@ export type LoaderContext = {
6969
_module: TranspiledModule, // eslint-disable-line no-use-before-define
7070
};
7171

72-
class Compilation {
73-
exports: any;
74-
75-
constructor() {
76-
this.exports = {};
77-
}
78-
}
72+
type Compilation = {
73+
exports: any,
74+
};
7975

8076
export default class TranspiledModule {
8177
module: Module;
@@ -437,9 +433,11 @@ export default class TranspiledModule {
437433
throw new Error(`${this.module.path} hasn't been transpiled yet.`);
438434
}
439435

440-
const module = this.module;
436+
const localModule = this.module;
441437

442-
this.compilation = new Compilation();
438+
this.compilation = {
439+
exports: {},
440+
};
443441
const transpiledModule = this;
444442

445443
try {
@@ -461,11 +459,11 @@ export default class TranspiledModule {
461459

462460
const requiredTranspiledModule = manager.resolveTranspiledModule(
463461
aliasedPath,
464-
module.path
462+
localModule.path
465463
);
466464

467-
if (module === requiredTranspiledModule.module) {
468-
throw new Error(`${module.path} is importing itself`);
465+
if (localModule === requiredTranspiledModule.module) {
466+
throw new Error(`${localModule.path} is importing itself`);
469467
}
470468

471469
// Check if this module has been evaluated before, if so return the exports
@@ -483,12 +481,10 @@ export default class TranspiledModule {
483481
const exports = evaluate(
484482
this.source.compiledCode,
485483
require,
486-
this.compilation.exports,
484+
this.compilation,
487485
manager.envVariables
488486
);
489487

490-
this.compilation.exports = exports;
491-
492488
return exports;
493489
} catch (e) {
494490
e.tModule = e.tModule || transpiledModule;

0 commit comments

Comments
 (0)