Skip to content

Commit 30e4f6c

Browse files
authored
Fix babel preval incompatabilities in setting cwd (codesandbox#2898)
Fixes: https://codesandbox.io/s/react-hooks-22c9h
1 parent e0648d8 commit 30e4f6c

File tree

1 file changed

+22
-6
lines changed
  • packages/app/src/sandbox/eval/transpilers/babel/worker

1 file changed

+22
-6
lines changed

packages/app/src/sandbox/eval/transpilers/babel/worker/evaluate.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import resolve from 'browser-resolve';
22
import hashsum from 'hash-sum';
3-
import { dirname } from 'path';
3+
import * as events from 'events';
4+
import * as util from 'util';
5+
import { dirname, basename } from 'path';
46
import type FSType from 'fs';
57
import detectOldBrowser from '@codesandbox/common/lib/detect-old-browser';
68
import evaluateCode from '../../../loaders/eval';
@@ -24,6 +26,14 @@ export default function evaluate(
2426
availablePresets
2527
) {
2628
const require = (requirePath: string) => {
29+
if (requirePath === 'events') {
30+
return events;
31+
}
32+
33+
if (requirePath === 'util') {
34+
return util;
35+
}
36+
2737
if (requirePath === 'assert') {
2838
return () => {};
2939
}
@@ -42,12 +52,12 @@ export default function evaluate(
4252
}
4353

4454
if (requirePath === 'require-from-string') {
45-
return (newCode: string) =>
55+
return (newCode: string, sourcePath: string) =>
4656
evaluate(
4757
fs,
4858
BFSRequire,
4959
newCode,
50-
'/',
60+
sourcePath,
5161
availablePlugins,
5262
availablePresets
5363
);
@@ -154,9 +164,15 @@ export default function evaluate(
154164
finalCode = transpiledCode;
155165
}
156166

157-
const exports = evaluateCode(finalCode, require, cache[id], {
158-
VUE_CLI_BABEL_TRANSPILE_MODULES: true,
159-
});
167+
const exports = evaluateCode(
168+
finalCode,
169+
require,
170+
cache[id],
171+
{
172+
VUE_CLI_BABEL_TRANSPILE_MODULES: true,
173+
},
174+
{ __dirname: dirname(path), __filename: basename(path) }
175+
);
160176

161177
return exports;
162178
}

0 commit comments

Comments
 (0)