bare-module 1.11.4 → 1.12.1
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 +3 -0
- package/index.js +38 -13
- package/lib/constants.js +7 -1
- package/package.json +1 -1
package/binding.c
CHANGED
|
@@ -198,6 +198,9 @@ bare_module_destroy (js_env_t *env, js_callback_info_t *info) {
|
|
|
198
198
|
err = js_delete_reference(env, context->on_evaluate);
|
|
199
199
|
assert(err == 0);
|
|
200
200
|
|
|
201
|
+
err = js_delete_reference(env, context->on_meta);
|
|
202
|
+
assert(err == 0);
|
|
203
|
+
|
|
201
204
|
err = js_delete_reference(env, context->ctx);
|
|
202
205
|
assert(err == 0);
|
|
203
206
|
|
package/index.js
CHANGED
|
@@ -116,6 +116,31 @@ const Module = module.exports = class Module {
|
|
|
116
116
|
return name in this._builtins
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
+
static createRequire (filename) {
|
|
120
|
+
const module = new Module(filename)
|
|
121
|
+
const referrer = module
|
|
122
|
+
|
|
123
|
+
const resolve = (specifier) => {
|
|
124
|
+
return this.resolve(specifier, module.dirname, {
|
|
125
|
+
referrer
|
|
126
|
+
})
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const require = (specifier) => {
|
|
130
|
+
const module = this.load(resolve(specifier), {
|
|
131
|
+
referrer
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
return module.exports
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
require.main = module.main
|
|
138
|
+
require.cache = this.cache
|
|
139
|
+
require.resolve = resolve
|
|
140
|
+
|
|
141
|
+
return require
|
|
142
|
+
}
|
|
143
|
+
|
|
119
144
|
static load (specifier, source = null, opts = {}) {
|
|
120
145
|
if (!ArrayBuffer.isView(source) && typeof source !== 'string' && source !== null) {
|
|
121
146
|
opts = source
|
|
@@ -365,20 +390,20 @@ const Module = module.exports = class Module {
|
|
|
365
390
|
|
|
366
391
|
static _transform (module, referrer = null, dynamic = false) {
|
|
367
392
|
if (dynamic) {
|
|
368
|
-
if (module._type !==
|
|
393
|
+
if (module._type !== constants.TYPE_ESM && module._handle === null) {
|
|
369
394
|
this._synthesize(module)
|
|
370
395
|
}
|
|
371
396
|
|
|
372
397
|
this._evaluate(module)
|
|
373
398
|
} else if (referrer) {
|
|
374
|
-
if (referrer._type ===
|
|
375
|
-
if (module._type !==
|
|
399
|
+
if (referrer._type === constants.TYPE_ESM) {
|
|
400
|
+
if (module._type !== constants.TYPE_ESM && module._handle === null) {
|
|
376
401
|
this._synthesize(module)
|
|
377
402
|
}
|
|
378
|
-
} else if (module._type ===
|
|
403
|
+
} else if (module._type === constants.TYPE_ESM) {
|
|
379
404
|
this._evaluate(module)
|
|
380
405
|
}
|
|
381
|
-
} else if (module._type ===
|
|
406
|
+
} else if (module._type === constants.TYPE_ESM) {
|
|
382
407
|
this._evaluate(module)
|
|
383
408
|
}
|
|
384
409
|
|
|
@@ -390,7 +415,7 @@ const Module = module.exports = class Module {
|
|
|
390
415
|
|
|
391
416
|
binding.runModule(module._handle, this._context)
|
|
392
417
|
|
|
393
|
-
if (module._type ===
|
|
418
|
+
if (module._type === constants.TYPE_ESM) {
|
|
394
419
|
module.exports = binding.getNamespace(module._handle)
|
|
395
420
|
}
|
|
396
421
|
|
|
@@ -416,7 +441,7 @@ Module._extensions['.js'] = function (module, source, referrer, protocol, import
|
|
|
416
441
|
(module._info && module._info.type === 'module') ||
|
|
417
442
|
|
|
418
443
|
// The source is a data: URI and the referrer is itself an ES module.
|
|
419
|
-
(protocol === this._protocols['data:'] && referrer && referrer._type ===
|
|
444
|
+
(protocol === this._protocols['data:'] && referrer && referrer._type === constants.TYPE_ESM)
|
|
420
445
|
)
|
|
421
446
|
|
|
422
447
|
const loader = this._extensions[isESM ? '.mjs' : '.cjs']
|
|
@@ -449,7 +474,7 @@ Module._extensions['.cjs'] = function (module, source, referrer, protocol, impor
|
|
|
449
474
|
return module.exports
|
|
450
475
|
}
|
|
451
476
|
|
|
452
|
-
module._type =
|
|
477
|
+
module._type = constants.TYPE_CJS
|
|
453
478
|
module._protocol = protocol
|
|
454
479
|
module._imports = imports
|
|
455
480
|
|
|
@@ -473,7 +498,7 @@ Module._extensions['.mjs'] = function (module, source, referrer, protocol, impor
|
|
|
473
498
|
|
|
474
499
|
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
475
500
|
|
|
476
|
-
module._type =
|
|
501
|
+
module._type = constants.TYPE_ESM
|
|
477
502
|
module._protocol = protocol
|
|
478
503
|
module._imports = imports
|
|
479
504
|
|
|
@@ -485,7 +510,7 @@ Module._extensions['.json'] = function (module, source, referrer, protocol, impo
|
|
|
485
510
|
|
|
486
511
|
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
487
512
|
|
|
488
|
-
module._type =
|
|
513
|
+
module._type = constants.TYPE_JSON
|
|
489
514
|
module._protocol = protocol
|
|
490
515
|
module._imports = imports
|
|
491
516
|
|
|
@@ -493,7 +518,7 @@ Module._extensions['.json'] = function (module, source, referrer, protocol, impo
|
|
|
493
518
|
}
|
|
494
519
|
|
|
495
520
|
Module._extensions['.bare'] = function (module, source, referrer, protocol, imports) {
|
|
496
|
-
module._type =
|
|
521
|
+
module._type = constants.TYPE_ADDON
|
|
497
522
|
module._protocol = protocol
|
|
498
523
|
module._imports = imports
|
|
499
524
|
|
|
@@ -501,7 +526,7 @@ Module._extensions['.bare'] = function (module, source, referrer, protocol, impo
|
|
|
501
526
|
}
|
|
502
527
|
|
|
503
528
|
Module._extensions['.node'] = function (module, source, referrer, protocol, imports) {
|
|
504
|
-
module._type =
|
|
529
|
+
module._type = constants.TYPE_ADDON
|
|
505
530
|
module._protocol = protocol
|
|
506
531
|
module._imports = imports
|
|
507
532
|
|
|
@@ -513,7 +538,7 @@ Module._extensions['.bundle'] = function (module, source, referrer, protocol, im
|
|
|
513
538
|
|
|
514
539
|
const bundle = this._bundleFor(module.filename, protocol, source)
|
|
515
540
|
|
|
516
|
-
module._type =
|
|
541
|
+
module._type = constants.TYPE_BUNDLE
|
|
517
542
|
module._protocol = protocol
|
|
518
543
|
module._imports = imports
|
|
519
544
|
|
package/lib/constants.js
CHANGED