-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (76 loc) · 3.41 KB
/
index.js
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
module.exports = (bundle) => `{
const __bundle = {
builtinRequire: typeof require === 'function' ? require : null,
cache: Object.create(null),
load: (url) => {
if (__bundle.cache[url]) return __bundle.cache[url]
const filename = url
const dirname = url.slice(0, url.lastIndexOf('/')) || '/'
const module = __bundle.cache[url] = {
filename,
dirname,
exports: {}
}
if (url.startsWith('builtin:')) {
module.exports = __bundle.builtinRequire(url.replace(/^builtin:/, ''))
return module
}
const file = __bundle.files[url] || null
if (file === null) throw new Error(\`Cannot find module '\${url}'\`)
function require (specifier) {
return __bundle.load(__bundle.resolve(specifier, url)).exports
}
require.main = ${JSON.stringify(bundle.main)}
require.cache = __bundle.cache
require.resolve = function resolve (specifier, parentURL = url) {
return __bundle.resolve(specifier, parentURL)
}
require.addon = function addon (specifier = '.', parentURL = url) {
return __bundle.builtinRequire.addon(__bundle.addon(specifier, parentURL))
}
require.addon.host = __bundle.builtinRequire.addon?.host
require.addon.resolve = function resolve (specifier = '.', parentURL = url) {
return __bundle.addon(specifier, parentURL)
}
require.asset = function asset (specifier, parentURL = url) {
return __bundle.asset(specifier, parentURL)
}
file.evaluate(require, module, module.exports, module.filename, module.dirname)
return module
},
resolve: (specifier, parentURL) => {
const file = __bundle.files[parentURL] || null
if (file === null) throw new Error(\`Cannot find module '\${parentURL}'\`)
const resolved = file.imports[specifier]
if (!resolved || (typeof resolved === 'object' && !resolved.default)) {
throw new Error(\`Cannot find module '\${specifier}' imported from '\${parentURL}'\`)
}
return typeof resolved === 'object' ? resolved.default : resolved
},
addon: (specifier = '.', parentURL) => {
const file = __bundle.files[parentURL] || null
if (file === null) throw new Error(\`Cannot find module '\${parentURL}'\`)
const resolved = file.imports[specifier]
if (!resolved || (typeof resolved === 'object' && !resolved.addon)) {
throw new Error(\`Cannot find addon '\${specifier}' imported from '\${parentURL}'\`)
}
return typeof resolved === 'object' ? resolved.addon : resolved
},
asset: (specifier, parentURL) => {
const file = __bundle.files[parentURL] || null
if (file === null) throw new Error(\`Cannot find module '\${parentURL}'\`)
const resolved = file.imports[specifier]
if (!resolved || (typeof resolved === 'object' && !resolved.asset)) {
throw new Error(\`Cannot find asset '\${specifier}' imported from '\${parentURL}'\`)
}
return typeof resolved === 'object' ? resolved.asset : resolved
},
files: {${[...bundle].map(([key, source]) => `
${JSON.stringify(key)}: {
imports: ${JSON.stringify(bundle.resolutions[key] || {})},
evaluate: (require, module, exports, __filename, __dirname) => {${key.endsWith('.json') ? 'module.exports = ' + source : source}}
}`).join(',')}
}
}
__bundle.load(${JSON.stringify(bundle.main)})
}`