forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate.js
More file actions
225 lines (193 loc) · 5.58 KB
/
evaluate.js
File metadata and controls
225 lines (193 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import resolve from 'browser-resolve';
import hashsum from 'hash-sum';
import * as events from 'events';
import * as crypto from 'crypto';
import * as util from 'util';
import { dirname, basename } from 'path';
import type FSType from 'fs';
import detectOldBrowser from '@codesandbox/common/lib/detect-old-browser';
import { packageFilter } from '../../../utils/resolve-utils';
import evaluateCode from '../../../loaders/eval';
let cache = {};
let cachedPaths = {};
let transpileBeforeExec = detectOldBrowser();
export const resetCache = () => {
cache = {};
cachedPaths = {};
transpileBeforeExec = detectOldBrowser();
};
export default function evaluate(
fs: FSType,
BFSRequire: Function,
code: string,
path = '/',
availablePlugins,
availablePresets
) {
const require = (requirePath: string) => {
if (requirePath === 'events') {
return events;
}
if (requirePath === 'util') {
return util;
}
if (requirePath === 'assert') {
return () => {};
}
if (requirePath === 'crypto') {
return crypto;
}
if (requirePath === 'stream') {
return {};
}
if (requirePath === 'constants') {
return {};
}
if (requirePath === 'babel-register') {
transpileBeforeExec = true;
return () => {};
}
if (requirePath === 'require-from-string') {
return (newCode: string, sourcePath: string) =>
evaluate(
fs,
BFSRequire,
newCode,
sourcePath,
availablePlugins,
availablePresets
);
}
const requiredNativeModule = BFSRequire(requirePath);
if (requiredNativeModule) {
return requiredNativeModule;
}
const plugin =
availablePlugins[requirePath] ||
availablePlugins[requirePath.replace('babel-plugin-', '')] ||
availablePlugins[requirePath.replace('@babel/plugin-', '')];
if (plugin && requirePath !== 'react') {
// Babel has exports as esmodule, but the plugins are not registered that way sadly
if (requirePath.startsWith('@babel/plugin')) {
return { __esModule: true, default: plugin };
}
return plugin;
}
const preset =
availablePresets[requirePath] ||
availablePresets[requirePath.replace('babel-preset-', '')] ||
availablePresets[requirePath.replace('@babel/preset-', '')];
if (preset && requirePath !== 'react') {
// Babel has exports as esmodule, but the plugins are not registered that way sadly. However, we register
// @babel/preset-env ourselves, so in that case we need to ignore that. We register @babel/preset-env to
// also export helper functions of the preset, to support vue.
if (
requirePath.startsWith('@babel/preset') &&
requirePath !== '@babel/preset-env'
) {
return { __esModule: true, default: preset };
}
return preset;
}
const dirName = dirname(path);
cachedPaths[dirName] = cachedPaths[dirName] || {};
const resolvedPath =
cachedPaths[dirName][requirePath] ||
resolve.sync(requirePath, {
filename: path,
extensions: ['.js', '.json'],
moduleDirectory: ['node_modules'],
packageFilter,
});
cachedPaths[dirName][requirePath] = resolvedPath;
const resolvedCode = fs.readFileSync(resolvedPath).toString();
const id = hashsum(resolvedCode + resolvedPath);
if (cache[id]) {
return cache[id].exports;
}
cache[id] = {};
return evaluate(
fs,
BFSRequire,
resolvedCode,
resolvedPath,
availablePlugins,
availablePresets
);
};
// require.resolve is often used in .babelrc configs to resolve the correct plugin path,
// we want to return a function for that, because our babelrc configs don't really understand
// strings as plugins.
require.resolve = requirePath => requirePath;
const id = hashsum(code + path);
cache[id] = {
id: path,
exports: {},
};
let finalCode = code;
if (path.endsWith('.json')) {
finalCode = `module.exports = JSON.parse(${JSON.stringify(code)})`;
}
finalCode += `\n//# sourceURL=${location.origin}${path}`;
if (transpileBeforeExec) {
const { code: transpiledCode } = self.Babel.transform(finalCode, {
presets: ['es2015', 'react', 'stage-0'],
plugins: [
'transform-async-to-generator',
'transform-object-rest-spread',
'transform-decorators-legacy',
'transform-class-properties',
// Polyfills the runtime needed for async/await and generators
[
'transform-runtime',
{
helpers: false,
polyfill: false,
regenerator: true,
},
],
[
'transform-regenerator',
{
// Async functions are converted to generators by babel-preset-env
async: false,
},
],
],
});
finalCode = transpiledCode;
}
const exports = evaluateCode(
finalCode,
require,
cache[id],
{
VUE_CLI_BABEL_TRANSPILE_MODULES: true,
},
{ __dirname: dirname(path), __filename: basename(path) }
);
return exports;
}
export function evaluateFromPath(
fs: FSType,
BFSRequire: Function,
path: string,
currentPath: string,
availablePlugins: Object,
availablePresets: Object
) {
const resolvedPath = resolve.sync(path, {
filename: currentPath,
extensions: ['.js', '.json'],
moduleDirectory: ['node_modules'],
});
const code = fs.readFileSync(resolvedPath).toString();
return evaluate(
fs,
BFSRequire,
code,
resolvedPath,
availablePlugins,
availablePresets
);
}