bare-module 2.5.0 → 2.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/binding.c +20 -59
- package/index.js +209 -168
- package/package.json +1 -1
package/binding.c
CHANGED
|
@@ -596,66 +596,27 @@ err:
|
|
|
596
596
|
|
|
597
597
|
static js_value_t *
|
|
598
598
|
init (js_env_t *env, js_value_t *exports) {
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
{
|
|
605
|
-
js_value_t *fn;
|
|
606
|
-
js_create_function(env, "destroy", -1, bare_module_destroy, NULL, &fn);
|
|
607
|
-
js_set_named_property(env, exports, "destroy", fn);
|
|
608
|
-
}
|
|
609
|
-
{
|
|
610
|
-
js_value_t *fn;
|
|
611
|
-
js_create_function(env, "createFunction", -1, bare_module_create_function, NULL, &fn);
|
|
612
|
-
js_set_named_property(env, exports, "createFunction", fn);
|
|
613
|
-
}
|
|
614
|
-
{
|
|
615
|
-
js_value_t *fn;
|
|
616
|
-
js_create_function(env, "createModule", -1, bare_module_create_module, NULL, &fn);
|
|
617
|
-
js_set_named_property(env, exports, "createModule", fn);
|
|
618
|
-
}
|
|
619
|
-
{
|
|
620
|
-
js_value_t *fn;
|
|
621
|
-
js_create_function(env, "createSyntheticModule", -1, bare_module_create_synthetic_module, NULL, &fn);
|
|
622
|
-
js_set_named_property(env, exports, "createSyntheticModule", fn);
|
|
623
|
-
}
|
|
624
|
-
{
|
|
625
|
-
js_value_t *fn;
|
|
626
|
-
js_create_function(env, "deleteModule", -1, bare_module_delete_module, NULL, &fn);
|
|
627
|
-
js_set_named_property(env, exports, "deleteModule", fn);
|
|
628
|
-
}
|
|
629
|
-
{
|
|
630
|
-
js_value_t *fn;
|
|
631
|
-
js_create_function(env, "setExport", -1, bare_module_set_export, NULL, &fn);
|
|
632
|
-
js_set_named_property(env, exports, "setExport", fn);
|
|
633
|
-
}
|
|
634
|
-
{
|
|
635
|
-
js_value_t *fn;
|
|
636
|
-
js_create_function(env, "runModule", -1, bare_module_run_module, NULL, &fn);
|
|
637
|
-
js_set_named_property(env, exports, "runModule", fn);
|
|
638
|
-
}
|
|
639
|
-
{
|
|
640
|
-
js_value_t *fn;
|
|
641
|
-
js_create_function(env, "getNamespace", -1, bare_module_get_namespace, NULL, &fn);
|
|
642
|
-
js_set_named_property(env, exports, "getNamespace", fn);
|
|
643
|
-
}
|
|
644
|
-
{
|
|
645
|
-
js_value_t *fn;
|
|
646
|
-
js_create_function(env, "exists", -1, bare_module_exists, NULL, &fn);
|
|
647
|
-
js_set_named_property(env, exports, "exists", fn);
|
|
648
|
-
}
|
|
649
|
-
{
|
|
650
|
-
js_value_t *fn;
|
|
651
|
-
js_create_function(env, "realpath", -1, bare_module_realpath, NULL, &fn);
|
|
652
|
-
js_set_named_property(env, exports, "realpath", fn);
|
|
653
|
-
}
|
|
654
|
-
{
|
|
655
|
-
js_value_t *fn;
|
|
656
|
-
js_create_function(env, "read", -1, bare_module_read, NULL, &fn);
|
|
657
|
-
js_set_named_property(env, exports, "read", fn);
|
|
599
|
+
#define V(name, fn) \
|
|
600
|
+
{ \
|
|
601
|
+
js_value_t *val; \
|
|
602
|
+
js_create_function(env, name, -1, fn, NULL, &val); \
|
|
603
|
+
js_set_named_property(env, exports, name, val); \
|
|
658
604
|
}
|
|
605
|
+
V("init", bare_module_init)
|
|
606
|
+
V("destroy", bare_module_destroy)
|
|
607
|
+
|
|
608
|
+
V("createFunction", bare_module_create_function)
|
|
609
|
+
V("createModule", bare_module_create_module)
|
|
610
|
+
V("createSyntheticModule", bare_module_create_synthetic_module)
|
|
611
|
+
V("deleteModule", bare_module_delete_module)
|
|
612
|
+
V("setExport", bare_module_set_export)
|
|
613
|
+
V("runModule", bare_module_run_module)
|
|
614
|
+
V("getNamespace", bare_module_get_namespace)
|
|
615
|
+
|
|
616
|
+
V("exists", bare_module_exists)
|
|
617
|
+
V("realpath", bare_module_realpath)
|
|
618
|
+
V("read", bare_module_read)
|
|
619
|
+
#undef V
|
|
659
620
|
|
|
660
621
|
return exports;
|
|
661
622
|
}
|
package/index.js
CHANGED
|
@@ -16,21 +16,14 @@ const Module = module.exports = exports = class Module {
|
|
|
16
16
|
this._main = null
|
|
17
17
|
this._exports = null
|
|
18
18
|
this._imports = null
|
|
19
|
-
this.
|
|
19
|
+
this._builtins = null
|
|
20
|
+
this._conditions = null
|
|
20
21
|
this._protocol = null
|
|
21
22
|
this._handle = null
|
|
22
23
|
|
|
23
24
|
Module._modules.add(this)
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
get type () {
|
|
27
|
-
return this._type
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
get defaultType () {
|
|
31
|
-
return this._defaultType
|
|
32
|
-
}
|
|
33
|
-
|
|
34
27
|
get filename () {
|
|
35
28
|
return this._filename
|
|
36
29
|
}
|
|
@@ -39,6 +32,14 @@ const Module = module.exports = exports = class Module {
|
|
|
39
32
|
return path.dirname(this._filename)
|
|
40
33
|
}
|
|
41
34
|
|
|
35
|
+
get type () {
|
|
36
|
+
return this._type
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
get defaultType () {
|
|
40
|
+
return this._defaultType
|
|
41
|
+
}
|
|
42
|
+
|
|
42
43
|
get main () {
|
|
43
44
|
return this._main
|
|
44
45
|
}
|
|
@@ -51,6 +52,18 @@ const Module = module.exports = exports = class Module {
|
|
|
51
52
|
this._exports = value
|
|
52
53
|
}
|
|
53
54
|
|
|
55
|
+
get imports () {
|
|
56
|
+
return this._imports
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
get builtins () {
|
|
60
|
+
return this._builtins
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
get conditions () {
|
|
64
|
+
return Array.from(this._conditions)
|
|
65
|
+
}
|
|
66
|
+
|
|
54
67
|
get protocol () {
|
|
55
68
|
return this._protocol
|
|
56
69
|
}
|
|
@@ -80,12 +93,15 @@ const Module = module.exports = exports = class Module {
|
|
|
80
93
|
return {
|
|
81
94
|
__proto__: { constructor: Module },
|
|
82
95
|
|
|
83
|
-
type: this.type,
|
|
84
|
-
defaultType: this.defaultType,
|
|
85
96
|
filename: this.filename,
|
|
86
97
|
dirname: this.dirname,
|
|
98
|
+
type: this.type,
|
|
99
|
+
defaultType: this.defaultType,
|
|
87
100
|
main: this.main,
|
|
88
|
-
exports: this.exports
|
|
101
|
+
exports: this.exports,
|
|
102
|
+
imports: this.imports,
|
|
103
|
+
builtins: this.builtins,
|
|
104
|
+
conditions: this.conditions
|
|
89
105
|
}
|
|
90
106
|
}
|
|
91
107
|
|
|
@@ -94,27 +110,19 @@ const Module = module.exports = exports = class Module {
|
|
|
94
110
|
static _cache = Object.create(null)
|
|
95
111
|
static _bundles = Object.create(null)
|
|
96
112
|
static _modules = new Set()
|
|
113
|
+
static _conditions = ['import', 'require', 'bare', 'node']
|
|
97
114
|
|
|
98
115
|
static _handle = binding.init(this, this._onimport, this._onevaluate, this._onmeta)
|
|
99
116
|
|
|
100
117
|
static _onimport (specifier, assertions, referrerFilename, dynamic) {
|
|
101
118
|
const referrer = this._cache[referrerFilename]
|
|
102
119
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
if (referrer) {
|
|
106
|
-
protocol = this._protocolFor(specifier, referrer._protocol)
|
|
107
|
-
|
|
108
|
-
imports = referrer._imports
|
|
120
|
+
const protocol = this._protocolFor(specifier, referrer._protocol)
|
|
109
121
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
})
|
|
115
|
-
} else {
|
|
116
|
-
specifier = this.resolve(specifier)
|
|
117
|
-
}
|
|
122
|
+
specifier = this.resolve(specifier, path.dirname(referrer._filename), {
|
|
123
|
+
protocol,
|
|
124
|
+
referrer
|
|
125
|
+
})
|
|
118
126
|
|
|
119
127
|
let type
|
|
120
128
|
|
|
@@ -129,7 +137,6 @@ const Module = module.exports = exports = class Module {
|
|
|
129
137
|
|
|
130
138
|
const module = this.load(specifier, {
|
|
131
139
|
protocol: this._protocolFor(specifier, protocol),
|
|
132
|
-
imports,
|
|
133
140
|
referrer,
|
|
134
141
|
dynamic,
|
|
135
142
|
type
|
|
@@ -149,29 +156,31 @@ const Module = module.exports = exports = class Module {
|
|
|
149
156
|
}
|
|
150
157
|
|
|
151
158
|
static _onmeta (specifier, meta) {
|
|
159
|
+
const self = Module
|
|
160
|
+
|
|
152
161
|
const module = this._cache[specifier]
|
|
153
162
|
|
|
154
163
|
const referrer = module
|
|
155
164
|
const dirname = path.dirname(module._filename)
|
|
156
165
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
166
|
+
meta.url = module._filename
|
|
167
|
+
meta.main = module._main === module
|
|
168
|
+
meta.resolve = resolve
|
|
169
|
+
meta.addon = addon
|
|
170
|
+
|
|
171
|
+
function resolve (specifier) {
|
|
172
|
+
return self.resolve(specifier, dirname, {
|
|
173
|
+
protocol: self._protocolFor(specifier, module._protocol),
|
|
160
174
|
imports: module._imports,
|
|
161
175
|
referrer
|
|
162
176
|
})
|
|
163
177
|
}
|
|
164
178
|
|
|
165
|
-
|
|
179
|
+
function addon (specifier = '.') {
|
|
166
180
|
return Bare.Addon.load(Bare.Addon.resolve(specifier, dirname, {
|
|
167
181
|
referrer
|
|
168
182
|
}))
|
|
169
183
|
}
|
|
170
|
-
|
|
171
|
-
meta.url = module._filename
|
|
172
|
-
meta.main = module._main === module
|
|
173
|
-
meta.resolve = resolve
|
|
174
|
-
meta.addon = addon
|
|
175
184
|
}
|
|
176
185
|
|
|
177
186
|
static Protocol = Protocol
|
|
@@ -182,82 +191,103 @@ const Module = module.exports = exports = class Module {
|
|
|
182
191
|
return this._cache
|
|
183
192
|
}
|
|
184
193
|
|
|
194
|
+
// For Node.js compatibility
|
|
195
|
+
static get builtinModules () {
|
|
196
|
+
return []
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// For Node.js compatibility
|
|
200
|
+
static isBuiltin () {
|
|
201
|
+
return false
|
|
202
|
+
}
|
|
203
|
+
|
|
185
204
|
static createRequire (filename, opts = {}) {
|
|
186
|
-
const
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
205
|
+
const self = Module
|
|
206
|
+
|
|
207
|
+
let {
|
|
208
|
+
referrer = null,
|
|
209
|
+
protocol = self._protocolFor(filename, referrer ? referrer._protocol : self._protocols['file:']),
|
|
210
|
+
imports = referrer ? referrer._imports : null,
|
|
211
|
+
builtins = referrer ? referrer._builtins : null,
|
|
212
|
+
conditions = referrer ? referrer._conditions : self._conditions,
|
|
213
|
+
main = referrer ? referrer._main : null,
|
|
214
|
+
defaultType = referrer ? referrer._defaultType : constants.types.SCRIPT,
|
|
215
|
+
type = constants.types.SCRIPT
|
|
191
216
|
} = opts
|
|
192
217
|
|
|
193
218
|
const module = new Module(filename)
|
|
194
219
|
|
|
220
|
+
module._main = main || module
|
|
195
221
|
module._type = type
|
|
196
222
|
module._defaultType = defaultType
|
|
197
|
-
module._imports = imports
|
|
198
223
|
module._protocol = protocol
|
|
224
|
+
module._imports = imports
|
|
225
|
+
module._builtins = builtins
|
|
226
|
+
module._conditions = conditions
|
|
227
|
+
|
|
228
|
+
referrer = module
|
|
199
229
|
|
|
200
|
-
const referrer = module
|
|
201
230
|
const dirname = path.dirname(module._filename)
|
|
202
231
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
232
|
+
require.main = module._main
|
|
233
|
+
require.cache = self._cache
|
|
234
|
+
require.resolve = resolve
|
|
235
|
+
require.addon = addon
|
|
236
|
+
|
|
237
|
+
return require
|
|
238
|
+
|
|
239
|
+
function require (specifier) {
|
|
240
|
+
const module = self.load(resolve(specifier), {
|
|
241
|
+
protocol: self._protocolFor(specifier, protocol),
|
|
207
242
|
referrer
|
|
208
243
|
})
|
|
244
|
+
|
|
245
|
+
return module._exports
|
|
209
246
|
}
|
|
210
247
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
protocol:
|
|
214
|
-
imports,
|
|
248
|
+
function resolve (specifier) {
|
|
249
|
+
return self.resolve(specifier, dirname, {
|
|
250
|
+
protocol: self._protocolFor(specifier, protocol),
|
|
215
251
|
referrer
|
|
216
252
|
})
|
|
217
|
-
|
|
218
|
-
return module._exports
|
|
219
253
|
}
|
|
220
254
|
|
|
221
|
-
|
|
255
|
+
function addon (specifier = '.') {
|
|
222
256
|
return Bare.Addon.load(Bare.Addon.resolve(specifier, dirname, {
|
|
223
257
|
referrer
|
|
224
258
|
}))
|
|
225
259
|
}
|
|
226
|
-
|
|
227
|
-
require.main = module._main
|
|
228
|
-
require.cache = this._cache
|
|
229
|
-
require.resolve = resolve
|
|
230
|
-
require.addon = addon
|
|
231
|
-
|
|
232
|
-
return require
|
|
233
260
|
}
|
|
234
261
|
|
|
235
262
|
static load (specifier, source = null, opts = {}) {
|
|
263
|
+
const self = Module
|
|
264
|
+
|
|
236
265
|
if (!ArrayBuffer.isView(source) && typeof source !== 'string' && source !== null) {
|
|
237
266
|
opts = source
|
|
238
267
|
source = null
|
|
239
268
|
}
|
|
240
269
|
|
|
241
270
|
let {
|
|
242
|
-
referrer = null,
|
|
243
|
-
protocol = this._protocolFor(specifier, this._protocols['file:']),
|
|
244
|
-
imports = null,
|
|
245
|
-
builtins = null,
|
|
246
271
|
dynamic = false,
|
|
272
|
+
referrer = null,
|
|
273
|
+
protocol = self._protocolFor(specifier, referrer ? referrer._protocol : self._protocols['file:']),
|
|
274
|
+
imports = referrer ? referrer._imports : null,
|
|
275
|
+
builtins = referrer ? referrer._builtins : null,
|
|
276
|
+
conditions = referrer ? referrer._conditions : self._conditions,
|
|
247
277
|
main = referrer ? referrer._main : null,
|
|
248
|
-
|
|
249
|
-
|
|
278
|
+
defaultType = referrer ? referrer._defaultType : 0,
|
|
279
|
+
type = 0
|
|
250
280
|
} = opts
|
|
251
281
|
|
|
252
|
-
if (
|
|
282
|
+
if (self._cache[specifier]) return self._transform(self._cache[specifier], referrer, dynamic)
|
|
253
283
|
|
|
254
|
-
const bundle =
|
|
284
|
+
const bundle = self._bundleFor(path.dirname(specifier), protocol)
|
|
255
285
|
|
|
256
286
|
if (bundle) {
|
|
257
287
|
protocol = new Protocol({
|
|
258
288
|
imports: bundle.imports,
|
|
259
289
|
|
|
260
|
-
preresolve:
|
|
290
|
+
preresolve: self._protocols['file:'].preresolve,
|
|
261
291
|
|
|
262
292
|
exists (filename) {
|
|
263
293
|
return bundle.exists(filename)
|
|
@@ -269,19 +299,22 @@ const Module = module.exports = exports = class Module {
|
|
|
269
299
|
})
|
|
270
300
|
}
|
|
271
301
|
|
|
272
|
-
const module =
|
|
302
|
+
const module = self._cache[specifier] = new Module(specifier)
|
|
273
303
|
|
|
274
304
|
if (builtins && specifier in builtins) {
|
|
275
305
|
module._exports = builtins[specifier]
|
|
276
306
|
} else {
|
|
277
|
-
module._defaultType = defaultType
|
|
278
307
|
module._main = main || module
|
|
279
|
-
module.
|
|
308
|
+
module._defaultType = defaultType
|
|
309
|
+
module._protocol = protocol
|
|
310
|
+
module._imports = imports
|
|
311
|
+
module._builtins = builtins
|
|
312
|
+
module._conditions = conditions
|
|
280
313
|
|
|
281
|
-
let extension =
|
|
314
|
+
let extension = self._extensionFor(type) || path.extname(specifier)
|
|
282
315
|
|
|
283
|
-
if (extension in
|
|
284
|
-
if (defaultType) extension =
|
|
316
|
+
if (extension in self._extensions === false) {
|
|
317
|
+
if (defaultType) extension = self._extensionFor(defaultType) || '.js'
|
|
285
318
|
else extension = '.js'
|
|
286
319
|
}
|
|
287
320
|
|
|
@@ -289,36 +322,15 @@ const Module = module.exports = exports = class Module {
|
|
|
289
322
|
throw errors.INVALID_BUNDLE_EXTENSION(`Invalid extension for bundle '${specifier}'`)
|
|
290
323
|
}
|
|
291
324
|
|
|
292
|
-
|
|
325
|
+
self._extensions[extension](module, source, referrer)
|
|
293
326
|
}
|
|
294
327
|
|
|
295
|
-
return
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
static _loadPackageManifest (dirname, protocol, opts = {}) {
|
|
299
|
-
const {
|
|
300
|
-
traverse = true
|
|
301
|
-
} = opts
|
|
302
|
-
|
|
303
|
-
do {
|
|
304
|
-
const specifier = path.join(dirname, 'package.json')
|
|
305
|
-
|
|
306
|
-
if (this._cache[specifier]) return this._cache[specifier]._exports
|
|
307
|
-
|
|
308
|
-
if (protocol.exists(specifier)) {
|
|
309
|
-
try {
|
|
310
|
-
return this.load(specifier, { protocol })._exports
|
|
311
|
-
} catch {}
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
if (traverse) dirname = path.dirname(dirname)
|
|
315
|
-
else break
|
|
316
|
-
} while (dirname !== path.sep && dirname !== '.')
|
|
317
|
-
|
|
318
|
-
return {}
|
|
328
|
+
return self._transform(module, referrer, dynamic)
|
|
319
329
|
}
|
|
320
330
|
|
|
321
331
|
static resolve (specifier, dirname = os.cwd(), opts = {}) {
|
|
332
|
+
const self = Module
|
|
333
|
+
|
|
322
334
|
if (typeof dirname !== 'string') {
|
|
323
335
|
opts = dirname
|
|
324
336
|
dirname = os.cwd()
|
|
@@ -326,19 +338,19 @@ const Module = module.exports = exports = class Module {
|
|
|
326
338
|
|
|
327
339
|
let {
|
|
328
340
|
referrer = null,
|
|
329
|
-
protocol =
|
|
330
|
-
imports = null,
|
|
331
|
-
builtins = null,
|
|
332
|
-
conditions =
|
|
341
|
+
protocol = referrer ? referrer._protocol : self._protocols['file:'],
|
|
342
|
+
imports = referrer ? referrer._imports : null,
|
|
343
|
+
builtins = referrer ? referrer._builtins : null,
|
|
344
|
+
conditions = referrer ? referrer._conditions : self._conditions
|
|
333
345
|
} = opts
|
|
334
346
|
|
|
335
|
-
const bundle =
|
|
347
|
+
const bundle = self._bundleFor(path.dirname(specifier), protocol)
|
|
336
348
|
|
|
337
349
|
if (bundle) {
|
|
338
350
|
protocol = new Protocol({
|
|
339
351
|
imports: bundle.imports,
|
|
340
352
|
|
|
341
|
-
preresolve:
|
|
353
|
+
preresolve: self._protocols['file:'].preresolve,
|
|
342
354
|
|
|
343
355
|
exists (filename) {
|
|
344
356
|
return bundle.exists(filename)
|
|
@@ -350,7 +362,7 @@ const Module = module.exports = exports = class Module {
|
|
|
350
362
|
})
|
|
351
363
|
}
|
|
352
364
|
|
|
353
|
-
const [resolved = null] =
|
|
365
|
+
const [resolved = null] = self._resolve(specifier, dirname, protocol, imports, builtins, conditions)
|
|
354
366
|
|
|
355
367
|
if (resolved === null) {
|
|
356
368
|
let msg = `Cannot find module '${specifier}'`
|
|
@@ -363,6 +375,29 @@ const Module = module.exports = exports = class Module {
|
|
|
363
375
|
return protocol.postresolve(resolved, dirname)
|
|
364
376
|
}
|
|
365
377
|
|
|
378
|
+
static _loadPackageManifest (dirname, protocol, opts = {}) {
|
|
379
|
+
const {
|
|
380
|
+
traverse = true
|
|
381
|
+
} = opts
|
|
382
|
+
|
|
383
|
+
do {
|
|
384
|
+
const specifier = path.join(dirname, 'package.json')
|
|
385
|
+
|
|
386
|
+
if (this._cache[specifier]) return this._cache[specifier]._exports
|
|
387
|
+
|
|
388
|
+
if (protocol.exists(specifier)) {
|
|
389
|
+
try {
|
|
390
|
+
return this.load(specifier, { protocol })._exports
|
|
391
|
+
} catch {}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if (traverse) dirname = path.dirname(dirname)
|
|
395
|
+
else break
|
|
396
|
+
} while (dirname !== path.sep && dirname !== '.')
|
|
397
|
+
|
|
398
|
+
return {}
|
|
399
|
+
}
|
|
400
|
+
|
|
366
401
|
static * _resolve (specifier, dirname, protocol, imports, builtins, conditions) {
|
|
367
402
|
const info = this._loadPackageManifest(dirname, protocol)
|
|
368
403
|
|
|
@@ -602,11 +637,11 @@ const Module = module.exports = exports = class Module {
|
|
|
602
637
|
|
|
603
638
|
static _transform (module, referrer = null, dynamic = false) {
|
|
604
639
|
if (dynamic) {
|
|
605
|
-
|
|
640
|
+
this._synthesize(module)
|
|
606
641
|
this._evaluate(module)
|
|
607
642
|
} else if (referrer) {
|
|
608
643
|
if (referrer._type === constants.types.MODULE) {
|
|
609
|
-
|
|
644
|
+
this._synthesize(module)
|
|
610
645
|
} else if (module._type === constants.types.MODULE) {
|
|
611
646
|
this._evaluate(module)
|
|
612
647
|
}
|
|
@@ -632,39 +667,47 @@ const Module = module.exports = exports = class Module {
|
|
|
632
667
|
static _synthesize (module) {
|
|
633
668
|
if ((module._state & constants.states.SYNTHESIZED) !== 0) return
|
|
634
669
|
|
|
635
|
-
|
|
670
|
+
if (module._type !== constants.types.MODULE) {
|
|
671
|
+
const names = ['default']
|
|
636
672
|
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
673
|
+
for (const key of Object.keys(module._exports)) {
|
|
674
|
+
if (key !== 'default') names.push(key)
|
|
675
|
+
}
|
|
640
676
|
|
|
641
|
-
|
|
677
|
+
module._handle = binding.createSyntheticModule(module._filename, names, this._handle)
|
|
678
|
+
}
|
|
642
679
|
|
|
643
680
|
module._state |= constants.states.SYNTHESIZED
|
|
644
681
|
}
|
|
645
682
|
}
|
|
646
683
|
|
|
647
|
-
Module._extensions['.js'] = function (module, source, referrer
|
|
684
|
+
Module._extensions['.js'] = function (module, source, referrer) {
|
|
685
|
+
const self = Module
|
|
686
|
+
|
|
687
|
+
const protocol = module._protocol
|
|
688
|
+
|
|
689
|
+
const info = self._loadPackageManifest(path.dirname(module._filename), protocol)
|
|
690
|
+
|
|
648
691
|
const isESM = (
|
|
649
692
|
// The default type is ES modules.
|
|
650
|
-
(
|
|
693
|
+
(constants.types.MODULE === module._defaultType) ||
|
|
651
694
|
|
|
652
695
|
// The package is explicitly declared as an ES module.
|
|
653
|
-
(
|
|
696
|
+
(info && info.type === 'module') ||
|
|
654
697
|
|
|
655
698
|
// The source is a data: URI and the referrer is itself an ES module.
|
|
656
|
-
(protocol ===
|
|
699
|
+
(protocol === self._protocols['data:'] && referrer && referrer._type === constants.types.MODULE)
|
|
657
700
|
)
|
|
658
701
|
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
return loader.call(this, module, source, referrer, protocol, imports)
|
|
702
|
+
return self._extensions[isESM ? '.mjs' : '.cjs'](module, source, referrer)
|
|
662
703
|
}
|
|
663
704
|
|
|
664
|
-
Module._extensions['.cjs'] = function (module, source, referrer
|
|
705
|
+
Module._extensions['.cjs'] = function (module, source, referrer) {
|
|
706
|
+
const self = Module
|
|
707
|
+
|
|
708
|
+
const protocol = module._protocol
|
|
709
|
+
|
|
665
710
|
module._type = constants.types.SCRIPT
|
|
666
|
-
module._protocol = protocol
|
|
667
|
-
module._imports = imports
|
|
668
711
|
|
|
669
712
|
if (protocol.load) {
|
|
670
713
|
module._exports = protocol.load(module._filename)
|
|
@@ -677,32 +720,8 @@ Module._extensions['.cjs'] = function (module, source, referrer, protocol, impor
|
|
|
677
720
|
|
|
678
721
|
const dirname = path.dirname(module._filename)
|
|
679
722
|
|
|
680
|
-
const resolve = (specifier) => {
|
|
681
|
-
return this.resolve(specifier, dirname, {
|
|
682
|
-
protocol: this._protocolFor(specifier, protocol),
|
|
683
|
-
imports,
|
|
684
|
-
referrer
|
|
685
|
-
})
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
const require = (specifier) => {
|
|
689
|
-
const module = this.load(resolve(specifier), {
|
|
690
|
-
protocol: this._protocolFor(specifier, protocol),
|
|
691
|
-
imports,
|
|
692
|
-
referrer
|
|
693
|
-
})
|
|
694
|
-
|
|
695
|
-
return module._exports
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
const addon = (specifier = '.') => {
|
|
699
|
-
return Bare.Addon.load(Bare.Addon.resolve(specifier, dirname, {
|
|
700
|
-
referrer
|
|
701
|
-
}))
|
|
702
|
-
}
|
|
703
|
-
|
|
704
723
|
require.main = module._main
|
|
705
|
-
require.cache =
|
|
724
|
+
require.cache = self._cache
|
|
706
725
|
require.resolve = resolve
|
|
707
726
|
require.addon = addon
|
|
708
727
|
|
|
@@ -715,13 +734,37 @@ Module._extensions['.cjs'] = function (module, source, referrer, protocol, impor
|
|
|
715
734
|
module._filename,
|
|
716
735
|
path.dirname(module._filename)
|
|
717
736
|
)
|
|
737
|
+
|
|
738
|
+
function require (specifier) {
|
|
739
|
+
const module = self.load(resolve(specifier), {
|
|
740
|
+
protocol: self._protocolFor(specifier, protocol),
|
|
741
|
+
referrer
|
|
742
|
+
})
|
|
743
|
+
|
|
744
|
+
return module._exports
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
function resolve (specifier) {
|
|
748
|
+
return self.resolve(specifier, dirname, {
|
|
749
|
+
protocol: self._protocolFor(specifier, protocol),
|
|
750
|
+
referrer
|
|
751
|
+
})
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
function addon (specifier = '.') {
|
|
755
|
+
return Bare.Addon.load(Bare.Addon.resolve(specifier, dirname, {
|
|
756
|
+
referrer
|
|
757
|
+
}))
|
|
758
|
+
}
|
|
718
759
|
}
|
|
719
760
|
}
|
|
720
761
|
|
|
721
|
-
Module._extensions['.mjs'] = function (module, source, referrer
|
|
762
|
+
Module._extensions['.mjs'] = function (module, source, referrer) {
|
|
763
|
+
const self = Module
|
|
764
|
+
|
|
765
|
+
const protocol = module._protocol
|
|
766
|
+
|
|
722
767
|
module._type = constants.types.MODULE
|
|
723
|
-
module._protocol = protocol
|
|
724
|
-
module._imports = imports
|
|
725
768
|
|
|
726
769
|
if (protocol.load) {
|
|
727
770
|
module._exports = protocol.load(module._filename)
|
|
@@ -730,14 +773,14 @@ Module._extensions['.mjs'] = function (module, source, referrer, protocol, impor
|
|
|
730
773
|
|
|
731
774
|
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
732
775
|
|
|
733
|
-
module._handle = binding.createModule(module._filename, source, 0,
|
|
776
|
+
module._handle = binding.createModule(module._filename, source, 0, self._handle)
|
|
734
777
|
}
|
|
735
778
|
}
|
|
736
779
|
|
|
737
|
-
Module._extensions['.json'] = function (module, source, referrer
|
|
780
|
+
Module._extensions['.json'] = function (module, source, referrer) {
|
|
781
|
+
const protocol = module._protocol
|
|
782
|
+
|
|
738
783
|
module._type = constants.types.JSON
|
|
739
|
-
module._protocol = protocol
|
|
740
|
-
module._imports = imports
|
|
741
784
|
|
|
742
785
|
if (protocol.load) {
|
|
743
786
|
module._exports = protocol.load(module._filename)
|
|
@@ -750,34 +793,32 @@ Module._extensions['.json'] = function (module, source, referrer, protocol, impo
|
|
|
750
793
|
}
|
|
751
794
|
}
|
|
752
795
|
|
|
753
|
-
Module._extensions['.bare'] = function (module, source, referrer
|
|
796
|
+
Module._extensions['.bare'] = function (module, source, referrer) {
|
|
754
797
|
module._type = constants.types.ADDON
|
|
755
|
-
module._protocol = protocol
|
|
756
|
-
module._imports = imports
|
|
757
798
|
|
|
758
799
|
module._exports = Bare.Addon.load(module._filename)
|
|
759
800
|
}
|
|
760
801
|
|
|
761
|
-
Module._extensions['.node'] = function (module, source, referrer
|
|
802
|
+
Module._extensions['.node'] = function (module, source, referrer) {
|
|
762
803
|
module._type = constants.types.ADDON
|
|
763
|
-
module._protocol = protocol
|
|
764
|
-
module._imports = imports
|
|
765
804
|
|
|
766
805
|
module._exports = Bare.Addon.load(module._filename)
|
|
767
806
|
}
|
|
768
807
|
|
|
769
|
-
Module._extensions['.bundle'] = function (module, source, referrer
|
|
808
|
+
Module._extensions['.bundle'] = function (module, source, referrer) {
|
|
809
|
+
const self = Module
|
|
810
|
+
|
|
811
|
+
const protocol = module._protocol
|
|
812
|
+
|
|
770
813
|
module._type = constants.types.BUNDLE
|
|
771
|
-
module._protocol = protocol
|
|
772
|
-
module._imports = imports
|
|
773
814
|
|
|
774
815
|
if (source === null) source = protocol.read(module._filename)
|
|
775
816
|
|
|
776
817
|
if (typeof source === 'string') source = Buffer.from(source)
|
|
777
818
|
|
|
778
|
-
const bundle =
|
|
819
|
+
const bundle = self._bundleFor(module._filename, protocol, source)
|
|
779
820
|
|
|
780
|
-
module._exports =
|
|
821
|
+
module._exports = self.load(bundle.main, bundle.read(bundle.main), { protocol, referrer })._exports
|
|
781
822
|
}
|
|
782
823
|
|
|
783
824
|
Module._protocols['file:'] = new Protocol({
|