bare-module 2.5.1 → 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 +181 -134
- 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
|
@@ -17,21 +17,13 @@ const Module = module.exports = exports = class Module {
|
|
|
17
17
|
this._exports = null
|
|
18
18
|
this._imports = null
|
|
19
19
|
this._builtins = null
|
|
20
|
-
this.
|
|
20
|
+
this._conditions = null
|
|
21
21
|
this._protocol = null
|
|
22
22
|
this._handle = null
|
|
23
23
|
|
|
24
24
|
Module._modules.add(this)
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
get type () {
|
|
28
|
-
return this._type
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
get defaultType () {
|
|
32
|
-
return this._defaultType
|
|
33
|
-
}
|
|
34
|
-
|
|
35
27
|
get filename () {
|
|
36
28
|
return this._filename
|
|
37
29
|
}
|
|
@@ -40,6 +32,14 @@ const Module = module.exports = exports = class Module {
|
|
|
40
32
|
return path.dirname(this._filename)
|
|
41
33
|
}
|
|
42
34
|
|
|
35
|
+
get type () {
|
|
36
|
+
return this._type
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
get defaultType () {
|
|
40
|
+
return this._defaultType
|
|
41
|
+
}
|
|
42
|
+
|
|
43
43
|
get main () {
|
|
44
44
|
return this._main
|
|
45
45
|
}
|
|
@@ -52,6 +52,18 @@ const Module = module.exports = exports = class Module {
|
|
|
52
52
|
this._exports = value
|
|
53
53
|
}
|
|
54
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
|
+
|
|
55
67
|
get protocol () {
|
|
56
68
|
return this._protocol
|
|
57
69
|
}
|
|
@@ -81,12 +93,15 @@ const Module = module.exports = exports = class Module {
|
|
|
81
93
|
return {
|
|
82
94
|
__proto__: { constructor: Module },
|
|
83
95
|
|
|
84
|
-
type: this.type,
|
|
85
|
-
defaultType: this.defaultType,
|
|
86
96
|
filename: this.filename,
|
|
87
97
|
dirname: this.dirname,
|
|
98
|
+
type: this.type,
|
|
99
|
+
defaultType: this.defaultType,
|
|
88
100
|
main: this.main,
|
|
89
|
-
exports: this.exports
|
|
101
|
+
exports: this.exports,
|
|
102
|
+
imports: this.imports,
|
|
103
|
+
builtins: this.builtins,
|
|
104
|
+
conditions: this.conditions
|
|
90
105
|
}
|
|
91
106
|
}
|
|
92
107
|
|
|
@@ -95,24 +110,19 @@ const Module = module.exports = exports = class Module {
|
|
|
95
110
|
static _cache = Object.create(null)
|
|
96
111
|
static _bundles = Object.create(null)
|
|
97
112
|
static _modules = new Set()
|
|
113
|
+
static _conditions = ['import', 'require', 'bare', 'node']
|
|
98
114
|
|
|
99
115
|
static _handle = binding.init(this, this._onimport, this._onevaluate, this._onmeta)
|
|
100
116
|
|
|
101
117
|
static _onimport (specifier, assertions, referrerFilename, dynamic) {
|
|
102
118
|
const referrer = this._cache[referrerFilename]
|
|
103
119
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
if (referrer) {
|
|
107
|
-
protocol = this._protocolFor(specifier, referrer._protocol)
|
|
120
|
+
const protocol = this._protocolFor(specifier, referrer._protocol)
|
|
108
121
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
} else {
|
|
114
|
-
specifier = this.resolve(specifier)
|
|
115
|
-
}
|
|
122
|
+
specifier = this.resolve(specifier, path.dirname(referrer._filename), {
|
|
123
|
+
protocol,
|
|
124
|
+
referrer
|
|
125
|
+
})
|
|
116
126
|
|
|
117
127
|
let type
|
|
118
128
|
|
|
@@ -146,29 +156,31 @@ const Module = module.exports = exports = class Module {
|
|
|
146
156
|
}
|
|
147
157
|
|
|
148
158
|
static _onmeta (specifier, meta) {
|
|
159
|
+
const self = Module
|
|
160
|
+
|
|
149
161
|
const module = this._cache[specifier]
|
|
150
162
|
|
|
151
163
|
const referrer = module
|
|
152
164
|
const dirname = path.dirname(module._filename)
|
|
153
165
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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),
|
|
157
174
|
imports: module._imports,
|
|
158
175
|
referrer
|
|
159
176
|
})
|
|
160
177
|
}
|
|
161
178
|
|
|
162
|
-
|
|
179
|
+
function addon (specifier = '.') {
|
|
163
180
|
return Bare.Addon.load(Bare.Addon.resolve(specifier, dirname, {
|
|
164
181
|
referrer
|
|
165
182
|
}))
|
|
166
183
|
}
|
|
167
|
-
|
|
168
|
-
meta.url = module._filename
|
|
169
|
-
meta.main = module._main === module
|
|
170
|
-
meta.resolve = resolve
|
|
171
|
-
meta.addon = addon
|
|
172
184
|
}
|
|
173
185
|
|
|
174
186
|
static Protocol = Protocol
|
|
@@ -179,57 +191,77 @@ const Module = module.exports = exports = class Module {
|
|
|
179
191
|
return this._cache
|
|
180
192
|
}
|
|
181
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
|
+
|
|
182
204
|
static createRequire (filename, opts = {}) {
|
|
183
|
-
const
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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
|
|
189
216
|
} = opts
|
|
190
217
|
|
|
191
218
|
const module = new Module(filename)
|
|
192
219
|
|
|
220
|
+
module._main = main || module
|
|
193
221
|
module._type = type
|
|
194
222
|
module._defaultType = defaultType
|
|
223
|
+
module._protocol = protocol
|
|
195
224
|
module._imports = imports
|
|
196
225
|
module._builtins = builtins
|
|
197
|
-
module.
|
|
226
|
+
module._conditions = conditions
|
|
227
|
+
|
|
228
|
+
referrer = module
|
|
198
229
|
|
|
199
|
-
const referrer = module
|
|
200
230
|
const dirname = path.dirname(module._filename)
|
|
201
231
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
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),
|
|
205
242
|
referrer
|
|
206
243
|
})
|
|
244
|
+
|
|
245
|
+
return module._exports
|
|
207
246
|
}
|
|
208
247
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
protocol:
|
|
248
|
+
function resolve (specifier) {
|
|
249
|
+
return self.resolve(specifier, dirname, {
|
|
250
|
+
protocol: self._protocolFor(specifier, protocol),
|
|
212
251
|
referrer
|
|
213
252
|
})
|
|
214
|
-
|
|
215
|
-
return module._exports
|
|
216
253
|
}
|
|
217
254
|
|
|
218
|
-
|
|
255
|
+
function addon (specifier = '.') {
|
|
219
256
|
return Bare.Addon.load(Bare.Addon.resolve(specifier, dirname, {
|
|
220
257
|
referrer
|
|
221
258
|
}))
|
|
222
259
|
}
|
|
223
|
-
|
|
224
|
-
require.main = module._main
|
|
225
|
-
require.cache = this._cache
|
|
226
|
-
require.resolve = resolve
|
|
227
|
-
require.addon = addon
|
|
228
|
-
|
|
229
|
-
return require
|
|
230
260
|
}
|
|
231
261
|
|
|
232
262
|
static load (specifier, source = null, opts = {}) {
|
|
263
|
+
const self = Module
|
|
264
|
+
|
|
233
265
|
if (!ArrayBuffer.isView(source) && typeof source !== 'string' && source !== null) {
|
|
234
266
|
opts = source
|
|
235
267
|
source = null
|
|
@@ -238,23 +270,24 @@ const Module = module.exports = exports = class Module {
|
|
|
238
270
|
let {
|
|
239
271
|
dynamic = false,
|
|
240
272
|
referrer = null,
|
|
241
|
-
protocol =
|
|
273
|
+
protocol = self._protocolFor(specifier, referrer ? referrer._protocol : self._protocols['file:']),
|
|
242
274
|
imports = referrer ? referrer._imports : null,
|
|
243
275
|
builtins = referrer ? referrer._builtins : null,
|
|
276
|
+
conditions = referrer ? referrer._conditions : self._conditions,
|
|
244
277
|
main = referrer ? referrer._main : null,
|
|
245
278
|
defaultType = referrer ? referrer._defaultType : 0,
|
|
246
279
|
type = 0
|
|
247
280
|
} = opts
|
|
248
281
|
|
|
249
|
-
if (
|
|
282
|
+
if (self._cache[specifier]) return self._transform(self._cache[specifier], referrer, dynamic)
|
|
250
283
|
|
|
251
|
-
const bundle =
|
|
284
|
+
const bundle = self._bundleFor(path.dirname(specifier), protocol)
|
|
252
285
|
|
|
253
286
|
if (bundle) {
|
|
254
287
|
protocol = new Protocol({
|
|
255
288
|
imports: bundle.imports,
|
|
256
289
|
|
|
257
|
-
preresolve:
|
|
290
|
+
preresolve: self._protocols['file:'].preresolve,
|
|
258
291
|
|
|
259
292
|
exists (filename) {
|
|
260
293
|
return bundle.exists(filename)
|
|
@@ -266,7 +299,7 @@ const Module = module.exports = exports = class Module {
|
|
|
266
299
|
})
|
|
267
300
|
}
|
|
268
301
|
|
|
269
|
-
const module =
|
|
302
|
+
const module = self._cache[specifier] = new Module(specifier)
|
|
270
303
|
|
|
271
304
|
if (builtins && specifier in builtins) {
|
|
272
305
|
module._exports = builtins[specifier]
|
|
@@ -276,12 +309,12 @@ const Module = module.exports = exports = class Module {
|
|
|
276
309
|
module._protocol = protocol
|
|
277
310
|
module._imports = imports
|
|
278
311
|
module._builtins = builtins
|
|
279
|
-
module.
|
|
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 = referrer ? referrer._protocol :
|
|
341
|
+
protocol = referrer ? referrer._protocol : self._protocols['file:'],
|
|
330
342
|
imports = referrer ? referrer._imports : null,
|
|
331
343
|
builtins = referrer ? referrer._builtins : null,
|
|
332
|
-
conditions =
|
|
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,36 +667,44 @@ 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
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
|
-
(
|
|
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)
|
|
702
|
+
return self._extensions[isESM ? '.mjs' : '.cjs'](module, source, referrer)
|
|
662
703
|
}
|
|
663
704
|
|
|
664
705
|
Module._extensions['.cjs'] = function (module, source, referrer) {
|
|
706
|
+
const self = Module
|
|
707
|
+
|
|
665
708
|
const protocol = module._protocol
|
|
666
709
|
|
|
667
710
|
module._type = constants.types.SCRIPT
|
|
@@ -677,30 +720,8 @@ Module._extensions['.cjs'] = function (module, source, referrer) {
|
|
|
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
|
-
referrer
|
|
684
|
-
})
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
const require = (specifier) => {
|
|
688
|
-
const module = this.load(resolve(specifier), {
|
|
689
|
-
protocol: this._protocolFor(specifier, protocol),
|
|
690
|
-
referrer
|
|
691
|
-
})
|
|
692
|
-
|
|
693
|
-
return module._exports
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
const addon = (specifier = '.') => {
|
|
697
|
-
return Bare.Addon.load(Bare.Addon.resolve(specifier, dirname, {
|
|
698
|
-
referrer
|
|
699
|
-
}))
|
|
700
|
-
}
|
|
701
|
-
|
|
702
723
|
require.main = module._main
|
|
703
|
-
require.cache =
|
|
724
|
+
require.cache = self._cache
|
|
704
725
|
require.resolve = resolve
|
|
705
726
|
require.addon = addon
|
|
706
727
|
|
|
@@ -713,10 +734,34 @@ Module._extensions['.cjs'] = function (module, source, referrer) {
|
|
|
713
734
|
module._filename,
|
|
714
735
|
path.dirname(module._filename)
|
|
715
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
|
+
}
|
|
716
759
|
}
|
|
717
760
|
}
|
|
718
761
|
|
|
719
762
|
Module._extensions['.mjs'] = function (module, source, referrer) {
|
|
763
|
+
const self = Module
|
|
764
|
+
|
|
720
765
|
const protocol = module._protocol
|
|
721
766
|
|
|
722
767
|
module._type = constants.types.MODULE
|
|
@@ -728,7 +773,7 @@ Module._extensions['.mjs'] = function (module, source, referrer) {
|
|
|
728
773
|
|
|
729
774
|
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
730
775
|
|
|
731
|
-
module._handle = binding.createModule(module._filename, source, 0,
|
|
776
|
+
module._handle = binding.createModule(module._filename, source, 0, self._handle)
|
|
732
777
|
}
|
|
733
778
|
}
|
|
734
779
|
|
|
@@ -761,6 +806,8 @@ Module._extensions['.node'] = function (module, source, referrer) {
|
|
|
761
806
|
}
|
|
762
807
|
|
|
763
808
|
Module._extensions['.bundle'] = function (module, source, referrer) {
|
|
809
|
+
const self = Module
|
|
810
|
+
|
|
764
811
|
const protocol = module._protocol
|
|
765
812
|
|
|
766
813
|
module._type = constants.types.BUNDLE
|
|
@@ -769,9 +816,9 @@ Module._extensions['.bundle'] = function (module, source, referrer) {
|
|
|
769
816
|
|
|
770
817
|
if (typeof source === 'string') source = Buffer.from(source)
|
|
771
818
|
|
|
772
|
-
const bundle =
|
|
819
|
+
const bundle = self._bundleFor(module._filename, protocol, source)
|
|
773
820
|
|
|
774
|
-
module._exports =
|
|
821
|
+
module._exports = self.load(bundle.main, bundle.read(bundle.main), { protocol, referrer })._exports
|
|
775
822
|
}
|
|
776
823
|
|
|
777
824
|
Module._protocols['file:'] = new Protocol({
|