bare-module 1.12.1 → 1.13.0

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.
Files changed (3) hide show
  1. package/index.js +122 -44
  2. package/lib/constants.js +10 -6
  3. package/package.json +1 -1
package/index.js CHANGED
@@ -5,25 +5,49 @@ const constants = require('./lib/constants')
5
5
  const errors = require('./lib/errors')
6
6
  const binding = require('./binding')
7
7
 
8
- const Module = module.exports = class Module {
8
+ module.exports = exports = class Module {
9
9
  constructor (filename, main) {
10
- this.filename = filename
11
- this.main = main || this
12
- this.exports = null
13
-
14
- this._type = null
15
- this._info = null
16
10
  this._state = 0
17
- this._protocol = null
11
+ this._type = 0
12
+ this._defaultType = this._type
13
+ this._filename = filename
14
+ this._main = main || this
15
+ this._exports = null
18
16
  this._imports = null
17
+ this._info = null
18
+ this._protocol = null
19
19
  this._handle = null
20
20
  }
21
21
 
22
+ get type () {
23
+ return this._type
24
+ }
25
+
26
+ get defaultType () {
27
+ return this._defaultType
28
+ }
29
+
30
+ get filename () {
31
+ return this._filename
32
+ }
33
+
22
34
  get dirname () {
23
- return path.dirname(this.filename)
35
+ return path.dirname(this._filename)
36
+ }
37
+
38
+ get main () {
39
+ return this._main
40
+ }
41
+
42
+ get exports () {
43
+ return this._exports
24
44
  }
25
45
 
26
- get importMap () {
46
+ set exports (value) {
47
+ this._exports = value
48
+ }
49
+
50
+ get imports () {
27
51
  return this._imports
28
52
  }
29
53
 
@@ -37,6 +61,20 @@ const Module = module.exports = class Module {
37
61
  return this.dirname
38
62
  }
39
63
 
64
+ [Symbol.for('bare.inspect')] () {
65
+ return {
66
+ __proto__: { constructor: Module },
67
+
68
+ type: this.type,
69
+ defaultType: this.defaultType,
70
+ filename: this.filename,
71
+ dirname: this.dirname,
72
+ main: this.main,
73
+ exports: this.exports,
74
+ imports: this.imports
75
+ }
76
+ }
77
+
40
78
  static _context = binding.init(this, this._onimport, this._onevaluate, this._onmeta)
41
79
 
42
80
  static _extensions = Object.create(null)
@@ -65,11 +103,23 @@ const Module = module.exports = class Module {
65
103
  specifier = this.resolve(specifier)
66
104
  }
67
105
 
106
+ let type
107
+
108
+ switch (assertions.type) {
109
+ case 'module':
110
+ type = constants.types.MODULE
111
+ break
112
+ case 'json':
113
+ type = constants.types.JSON
114
+ break
115
+ }
116
+
68
117
  const module = this.load(specifier, {
69
118
  protocol: this._protocolFor(specifier, protocol),
70
119
  imports,
71
120
  referrer,
72
- dynamic
121
+ dynamic,
122
+ type
73
123
  })
74
124
 
75
125
  return module._handle
@@ -103,6 +153,7 @@ const Module = module.exports = class Module {
103
153
 
104
154
  static Protocol = Protocol
105
155
  static Bundle = Bundle
156
+ static constants = constants
106
157
 
107
158
  static get cache () {
108
159
  return this._cache
@@ -152,7 +203,9 @@ const Module = module.exports = class Module {
152
203
  protocol = this._protocolFor(specifier, this._protocols['file:']),
153
204
  referrer = null,
154
205
  dynamic = false,
155
- main = referrer ? referrer.main : null
206
+ main = referrer ? referrer._main : null,
207
+ type = 0,
208
+ defaultType = referrer ? referrer._defaultType : 0
156
209
  } = opts
157
210
 
158
211
  if (this._cache[specifier]) return this._transform(this._cache[specifier], referrer, dynamic)
@@ -175,6 +228,8 @@ const Module = module.exports = class Module {
175
228
 
176
229
  const module = this._cache[specifier] = new this(specifier, main)
177
230
 
231
+ module._defaultType = defaultType
232
+
178
233
  let dirname = module.dirname
179
234
  do {
180
235
  const pkg = path.join(dirname, 'package.json')
@@ -192,9 +247,12 @@ const Module = module.exports = class Module {
192
247
  if (specifier in this._builtins) {
193
248
  module.exports = this._builtins[specifier]
194
249
  } else {
195
- let extension = path.extname(specifier)
250
+ let extension = this._extensionFor(type) || path.extname(specifier)
196
251
 
197
- if (extension in this._extensions === false) extension = '.js'
252
+ if (extension in this._extensions === false) {
253
+ if (defaultType) extension = this._extensionFor(defaultType) || '.js'
254
+ else extension = '.js'
255
+ }
198
256
 
199
257
  this._extensions[extension].call(this, module, source, referrer, protocol, imports)
200
258
  }
@@ -331,6 +389,23 @@ const Module = module.exports = class Module {
331
389
  }
332
390
  }
333
391
 
392
+ static _extensionFor (type) {
393
+ switch (type) {
394
+ case constants.types.SCRIPT:
395
+ return '.cjs'
396
+ case constants.types.MODULE:
397
+ return '.esm'
398
+ case constants.types.JSON:
399
+ return '.json'
400
+ case constants.types.BUNDLE:
401
+ return '.bundle'
402
+ case constants.types.ADDON:
403
+ return '.bare'
404
+ default:
405
+ return null
406
+ }
407
+ }
408
+
334
409
  static _protocolFor (specifier, fallback = null) {
335
410
  let protocol = fallback
336
411
 
@@ -390,20 +465,20 @@ const Module = module.exports = class Module {
390
465
 
391
466
  static _transform (module, referrer = null, dynamic = false) {
392
467
  if (dynamic) {
393
- if (module._type !== constants.TYPE_ESM && module._handle === null) {
468
+ if (module._type !== constants.types.MODULE && module._handle === null) {
394
469
  this._synthesize(module)
395
470
  }
396
471
 
397
472
  this._evaluate(module)
398
473
  } else if (referrer) {
399
- if (referrer._type === constants.TYPE_ESM) {
400
- if (module._type !== constants.TYPE_ESM && module._handle === null) {
474
+ if (referrer._type === constants.types.MODULE) {
475
+ if (module._type !== constants.types.MODULE && module._handle === null) {
401
476
  this._synthesize(module)
402
477
  }
403
- } else if (module._type === constants.TYPE_ESM) {
478
+ } else if (module._type === constants.types.MODULE) {
404
479
  this._evaluate(module)
405
480
  }
406
- } else if (module._type === constants.TYPE_ESM) {
481
+ } else if (module._type === constants.types.MODULE) {
407
482
  this._evaluate(module)
408
483
  }
409
484
 
@@ -411,15 +486,15 @@ const Module = module.exports = class Module {
411
486
  }
412
487
 
413
488
  static _evaluate (module) {
414
- if ((module._state & constants.STATE_EVALUATED) !== 0) return
489
+ if ((module._state & constants.states.EVALUATED) !== 0) return
415
490
 
416
491
  binding.runModule(module._handle, this._context)
417
492
 
418
- if (module._type === constants.TYPE_ESM) {
493
+ if (module._type === constants.types.MODULE) {
419
494
  module.exports = binding.getNamespace(module._handle)
420
495
  }
421
496
 
422
- module._state |= constants.STATE_EVALUATED
497
+ module._state |= constants.states.EVALUATED
423
498
  }
424
499
 
425
500
  static _synthesize (module) {
@@ -431,17 +506,20 @@ const Module = module.exports = class Module {
431
506
 
432
507
  module._handle = binding.createSyntheticModule(module.filename, names, this._context)
433
508
 
434
- module._state &= ~constants.STATE_EVALUATED
509
+ module._state &= ~constants.states.EVALUATED
435
510
  }
436
511
  }
437
512
 
438
- Module._extensions['.js'] = function (module, source, referrer, protocol, imports) {
513
+ exports._extensions['.js'] = function (module, source, referrer, protocol, imports) {
439
514
  const isESM = (
515
+ // The default type is ES modules.
516
+ (module._defaultType === constants.types.MODULE) ||
517
+
440
518
  // The package is explicitly declared as an ES module.
441
519
  (module._info && module._info.type === 'module') ||
442
520
 
443
521
  // The source is a data: URI and the referrer is itself an ES module.
444
- (protocol === this._protocols['data:'] && referrer && referrer._type === constants.TYPE_ESM)
522
+ (protocol === this._protocols['data:'] && referrer && referrer._type === constants.types.MODULE)
445
523
  )
446
524
 
447
525
  const loader = this._extensions[isESM ? '.mjs' : '.cjs']
@@ -449,7 +527,7 @@ Module._extensions['.js'] = function (module, source, referrer, protocol, import
449
527
  return loader.call(this, module, source, referrer, protocol, imports)
450
528
  }
451
529
 
452
- Module._extensions['.cjs'] = function (module, source, referrer, protocol, imports) {
530
+ exports._extensions['.cjs'] = function (module, source, referrer, protocol, imports) {
453
531
  if (source === null) source = protocol.read(module.filename)
454
532
 
455
533
  if (typeof source !== 'string') source = Buffer.coerce(source).toString()
@@ -474,7 +552,7 @@ Module._extensions['.cjs'] = function (module, source, referrer, protocol, impor
474
552
  return module.exports
475
553
  }
476
554
 
477
- module._type = constants.TYPE_CJS
555
+ module._type = constants.types.SCRIPT
478
556
  module._protocol = protocol
479
557
  module._imports = imports
480
558
 
@@ -493,65 +571,65 @@ Module._extensions['.cjs'] = function (module, source, referrer, protocol, impor
493
571
  )
494
572
  }
495
573
 
496
- Module._extensions['.mjs'] = function (module, source, referrer, protocol, imports) {
574
+ exports._extensions['.mjs'] = function (module, source, referrer, protocol, imports) {
497
575
  if (source === null) source = protocol.read(module.filename)
498
576
 
499
577
  if (typeof source !== 'string') source = Buffer.coerce(source).toString()
500
578
 
501
- module._type = constants.TYPE_ESM
579
+ module._type = constants.types.MODULE
502
580
  module._protocol = protocol
503
581
  module._imports = imports
504
582
 
505
583
  module._handle = binding.createModule(module.filename, source, 0, this._context)
506
584
  }
507
585
 
508
- Module._extensions['.json'] = function (module, source, referrer, protocol, imports) {
586
+ exports._extensions['.json'] = function (module, source, referrer, protocol, imports) {
509
587
  if (source === null) source = protocol.read(module.filename)
510
588
 
511
589
  if (typeof source !== 'string') source = Buffer.coerce(source).toString()
512
590
 
513
- module._type = constants.TYPE_JSON
591
+ module._type = constants.types.JSON
514
592
  module._protocol = protocol
515
593
  module._imports = imports
516
594
 
517
595
  module.exports = JSON.parse(source)
518
596
  }
519
597
 
520
- Module._extensions['.bare'] = function (module, source, referrer, protocol, imports) {
521
- module._type = constants.TYPE_ADDON
598
+ exports._extensions['.bare'] = function (module, source, referrer, protocol, imports) {
599
+ module._type = constants.types.ADDON
522
600
  module._protocol = protocol
523
601
  module._imports = imports
524
602
 
525
603
  module.exports = process.addon(module.filename)
526
604
  }
527
605
 
528
- Module._extensions['.node'] = function (module, source, referrer, protocol, imports) {
529
- module._type = constants.TYPE_ADDON
606
+ exports._extensions['.node'] = function (module, source, referrer, protocol, imports) {
607
+ module._type = constants.types.ADDON
530
608
  module._protocol = protocol
531
609
  module._imports = imports
532
610
 
533
611
  module.exports = process.addon(module.filename)
534
612
  }
535
613
 
536
- Module._extensions['.bundle'] = function (module, source, referrer, protocol, imports) {
614
+ exports._extensions['.bundle'] = function (module, source, referrer, protocol, imports) {
537
615
  if (typeof source === 'string') source = Buffer.from(source)
538
616
 
539
617
  const bundle = this._bundleFor(module.filename, protocol, source)
540
618
 
541
- module._type = constants.TYPE_BUNDLE
619
+ module._type = constants.types.BUNDLE
542
620
  module._protocol = protocol
543
621
  module._imports = imports
544
622
 
545
- module.exports = Module.load(bundle.main, bundle.read(bundle.main), { protocol, imports, referrer }).exports
623
+ module.exports = exports.load(bundle.main, bundle.read(bundle.main), { protocol, imports, referrer }).exports
546
624
  }
547
625
 
548
- Module._protocols['file:'] = new Protocol({
626
+ exports._protocols['file:'] = new Protocol({
549
627
  preresolve (specifier) {
550
628
  return specifier.replace(/^file:/, '')
551
629
  },
552
630
 
553
631
  postresolve (specifier) {
554
- if (Module.isBuiltin(specifier)) return specifier
632
+ if (exports.isBuiltin(specifier)) return specifier
555
633
  return binding.realpath(specifier)
556
634
  },
557
635
 
@@ -564,13 +642,13 @@ Module._protocols['file:'] = new Protocol({
564
642
  }
565
643
  })
566
644
 
567
- Module._protocols['node:'] = new Protocol({
645
+ exports._protocols['node:'] = new Protocol({
568
646
  preresolve (specifier) {
569
647
  return specifier.replace(/^node:/, '')
570
648
  }
571
649
  })
572
650
 
573
- Module._protocols['data:'] = new Protocol({
651
+ exports._protocols['data:'] = new Protocol({
574
652
  * resolve (specifier) {
575
653
  yield specifier
576
654
  },
@@ -582,8 +660,8 @@ Module._protocols['data:'] = new Protocol({
582
660
  }
583
661
  })
584
662
 
585
- process.on('exit', () => binding.destroy(Module._context))
663
+ process.on('exit', () => binding.destroy(exports._context))
586
664
 
587
665
  if (process.thread) {
588
- process.thread.on('exit', () => binding.destroy(Module._context))
666
+ process.thread.on('exit', () => binding.destroy(exports._context))
589
667
  }
package/lib/constants.js CHANGED
@@ -1,9 +1,13 @@
1
1
  module.exports = {
2
- STATE_EVALUATED: 1,
2
+ states: {
3
+ EVALUATED: 1
4
+ },
3
5
 
4
- TYPE_ESM: 'esm',
5
- TYPE_CJS: 'cjs',
6
- TYPE_JSON: 'json',
7
- TYPE_BUNDLE: 'bundle',
8
- TYPE_ADDON: 'addon'
6
+ types: {
7
+ SCRIPT: 1,
8
+ MODULE: 2,
9
+ JSON: 3,
10
+ BUNDLE: 4,
11
+ ADDON: 5
12
+ }
9
13
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "1.12.1",
3
+ "version": "1.13.0",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [