bare-module 2.6.2 → 2.6.4

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 (2) hide show
  1. package/index.js +70 -76
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -21,6 +21,7 @@ const Module = module.exports = exports = class Module {
21
21
  this._builtins = null
22
22
  this._conditions = null
23
23
  this._protocol = null
24
+ this._bundle = null
24
25
  this._handle = null
25
26
 
26
27
  Module._modules.add(this)
@@ -91,6 +92,59 @@ const Module = module.exports = exports = class Module {
91
92
  Module._modules.delete(this)
92
93
  }
93
94
 
95
+ _transform (referrer = null, dynamic = false) {
96
+ if (dynamic) {
97
+ this._synthesize()
98
+ this._evaluate()
99
+
100
+ return this
101
+ }
102
+
103
+ if (referrer) {
104
+ if (referrer._type === constants.types.MODULE) {
105
+ this._synthesize()
106
+ } else if (this._type === constants.types.MODULE) {
107
+ this._evaluate()
108
+ }
109
+
110
+ return this
111
+ }
112
+
113
+ if (this._type === constants.types.MODULE) {
114
+ this._evaluate()
115
+ }
116
+
117
+ return this
118
+ }
119
+
120
+ _evaluate () {
121
+ if ((this._state & constants.states.EVALUATED) !== 0) return
122
+
123
+ binding.runModule(this._handle, Module._handle)
124
+
125
+ if (this._type === constants.types.MODULE) {
126
+ this._exports = binding.getNamespace(this._handle)
127
+ }
128
+
129
+ this._state |= constants.states.EVALUATED
130
+ }
131
+
132
+ _synthesize () {
133
+ if ((this._state & constants.states.SYNTHESIZED) !== 0) return
134
+
135
+ if (this._type !== constants.types.MODULE) {
136
+ const names = ['default']
137
+
138
+ for (const key of Object.keys(this._exports)) {
139
+ if (key !== 'default') names.push(key)
140
+ }
141
+
142
+ this._handle = binding.createSyntheticModule(this._filename, names, Module._handle)
143
+ }
144
+
145
+ this._state |= constants.states.SYNTHESIZED
146
+ }
147
+
94
148
  [Symbol.for('bare.inspect')] () {
95
149
  return {
96
150
  __proto__: { constructor: Module },
@@ -110,7 +164,6 @@ const Module = module.exports = exports = class Module {
110
164
  static _extensions = Object.create(null)
111
165
  static _protocols = Object.create(null)
112
166
  static _cache = Object.create(null)
113
- static _bundles = Object.create(null)
114
167
  static _modules = new Set()
115
168
  static _conditions = ['import', 'require', 'bare', 'node']
116
169
 
@@ -263,6 +316,10 @@ const Module = module.exports = exports = class Module {
263
316
  static load (specifier, source = null, opts = {}) {
264
317
  const self = Module
265
318
 
319
+ if (typeof specifier !== 'string') {
320
+ throw new TypeError(`Specifier must be a string. Received type ${typeof specifier} (${specifier})`)
321
+ }
322
+
266
323
  if (!ArrayBuffer.isView(source) && typeof source !== 'string' && source !== null) {
267
324
  opts = source
268
325
  source = null
@@ -280,7 +337,7 @@ const Module = module.exports = exports = class Module {
280
337
  type = 0
281
338
  } = opts
282
339
 
283
- if (self._cache[specifier]) return self._transform(self._cache[specifier], referrer, dynamic)
340
+ if (self._cache[specifier]) return self._cache[specifier]._transform(referrer, dynamic)
284
341
 
285
342
  const bundle = self._bundleFor(path.dirname(specifier), protocol)
286
343
 
@@ -324,12 +381,16 @@ const Module = module.exports = exports = class Module {
324
381
  self._extensions[extension](module, source, referrer)
325
382
  }
326
383
 
327
- return self._transform(module, referrer, dynamic)
384
+ return module._transform(referrer, dynamic)
328
385
  }
329
386
 
330
387
  static resolve (specifier, dirname = os.cwd(), opts = {}) {
331
388
  const self = Module
332
389
 
390
+ if (typeof specifier !== 'string') {
391
+ throw new TypeError(`Specifier must be a string. Received type ${typeof specifier} (${specifier})`)
392
+ }
393
+
333
394
  if (typeof dirname !== 'string') {
334
395
  opts = dirname
335
396
  dirname = os.cwd()
@@ -449,7 +510,7 @@ const Module = module.exports = exports = class Module {
449
510
  return protocol
450
511
  }
451
512
 
452
- static _bundleFor (specifier, protocol, source = null) {
513
+ static _bundleFor (specifier, protocol) {
453
514
  let name = specifier
454
515
  do {
455
516
  if (path.extname(name) === '.bundle') {
@@ -461,76 +522,7 @@ const Module = module.exports = exports = class Module {
461
522
 
462
523
  if (path.extname(name) !== '.bundle') return null
463
524
 
464
- let bundle = this._bundles[name]
465
-
466
- if (bundle) return bundle
467
-
468
- const parent = this._bundleFor(path.dirname(name), protocol)
469
-
470
- if (parent) {
471
- protocol = new Protocol({
472
- imports: parent.imports,
473
-
474
- exists (filename) {
475
- return parent.exists(filename)
476
- },
477
-
478
- read (filename) {
479
- return parent.read(filename)
480
- }
481
- })
482
- }
483
-
484
- if (source === null || name !== specifier) source = protocol.read(name)
485
-
486
- bundle = this._bundles[name] = Bundle.from(source).mount(name)
487
-
488
- return bundle
489
- }
490
-
491
- static _transform (module, referrer = null, dynamic = false) {
492
- if (dynamic) {
493
- this._synthesize(module)
494
- this._evaluate(module)
495
- } else if (referrer) {
496
- if (referrer._type === constants.types.MODULE) {
497
- this._synthesize(module)
498
- } else if (module._type === constants.types.MODULE) {
499
- this._evaluate(module)
500
- }
501
- } else if (module._type === constants.types.MODULE) {
502
- this._evaluate(module)
503
- }
504
-
505
- return module
506
- }
507
-
508
- static _evaluate (module) {
509
- if ((module._state & constants.states.EVALUATED) !== 0) return
510
-
511
- binding.runModule(module._handle, this._handle)
512
-
513
- if (module._type === constants.types.MODULE) {
514
- module._exports = binding.getNamespace(module._handle)
515
- }
516
-
517
- module._state |= constants.states.EVALUATED
518
- }
519
-
520
- static _synthesize (module) {
521
- if ((module._state & constants.states.SYNTHESIZED) !== 0) return
522
-
523
- if (module._type !== constants.types.MODULE) {
524
- const names = ['default']
525
-
526
- for (const key of Object.keys(module._exports)) {
527
- if (key !== 'default') names.push(key)
528
- }
529
-
530
- module._handle = binding.createSyntheticModule(module._filename, names, this._handle)
531
- }
532
-
533
- module._state |= constants.states.SYNTHESIZED
525
+ return Module.load(name, { protocol })._bundle
534
526
  }
535
527
  }
536
528
 
@@ -687,9 +679,11 @@ Module._extensions['.bundle'] = function (module, source, referrer) {
687
679
 
688
680
  if (typeof source === 'string') source = Buffer.from(source)
689
681
 
690
- const bundle = self._bundleFor(module._filename, protocol, source)
682
+ const bundle = module._bundle = Bundle.from(source).mount(module._filename)
691
683
 
692
- module._exports = self.load(bundle.main, bundle.read(bundle.main), { protocol, referrer })._exports
684
+ if (bundle.main) {
685
+ module._exports = self.load(bundle.main, bundle.read(bundle.main), { protocol, referrer })._exports
686
+ }
693
687
  }
694
688
 
695
689
  Module._protocols['file:'] = new Protocol({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "2.6.2",
3
+ "version": "2.6.4",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -26,7 +26,7 @@
26
26
  "homepage": "https://github.com/holepunchto/bare-module#readme",
27
27
  "dependencies": {
28
28
  "bare-bundle": "^0.3.0",
29
- "bare-module-resolve": "^1.0.0",
29
+ "bare-module-resolve": "^1.2.6",
30
30
  "bare-os": "^2.0.0",
31
31
  "bare-path": "^2.0.0",
32
32
  "bare-url": "^0.3.4"