-
Notifications
You must be signed in to change notification settings - Fork 0
/
vbox.ts
52 lines (44 loc) · 1.38 KB
/
vbox.ts
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
import path = require('path');
import vm = require('vm');
import PatchedModule from '../patched-module'
export const builtinModules = require('@fibjs/builtin-modules')
export const recommendedVBoxModules = {
fs: require('fs'),
path: require('path'),
events: require('events'),
util: {
deprecate: require('util-deprecate/browser'),
...require('util'),
},
crypto: require('crypto'),
buffer: require('buffer'),
module: PatchedModule,
'builtin-modules': builtinModules,
}
export function recommendedVBoxModuleFallback (name: string) {
if (builtinModules.includes(name)) {
return require(name)
}
}
export function getCustomizedVBox (myModules: any = {}, myFallback: Function = recommendedVBoxModuleFallback) {
return new vm.SandBox(
{
...recommendedVBoxModules,
...myModules
},
myFallback
)
}
export function getVueSsrVBox (myModules: any = {}, myFallback: Function = recommendedVBoxModuleFallback, opts: any = {}) {
const vbox = new vm.SandBox(
{
...recommendedVBoxModules,
...myModules
},
myFallback
)
const { env = {} } = opts || {}
vbox.addScript('__VUE_NODE_ENV__', `process.env.NODE_ENV="${env.NODE_ENV || 'development'}";` as any)
vbox.run(path.resolve(__dirname, '../snippets/vue-ssr.js'))
return vbox
}