bare-module 2.6.1 → 2.6.3

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 +63 -77
  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
 
@@ -280,7 +333,7 @@ const Module = module.exports = exports = class Module {
280
333
  type = 0
281
334
  } = opts
282
335
 
283
- if (self._cache[specifier]) return self._transform(self._cache[specifier], referrer, dynamic)
336
+ if (self._cache[specifier]) return self._cache[specifier]._transform(referrer, dynamic)
284
337
 
285
338
  const bundle = self._bundleFor(path.dirname(specifier), protocol)
286
339
 
@@ -324,7 +377,7 @@ const Module = module.exports = exports = class Module {
324
377
  self._extensions[extension](module, source, referrer)
325
378
  }
326
379
 
327
- return self._transform(module, referrer, dynamic)
380
+ return module._transform(referrer, dynamic)
328
381
  }
329
382
 
330
383
  static resolve (specifier, dirname = os.cwd(), opts = {}) {
@@ -391,7 +444,7 @@ const Module = module.exports = exports = class Module {
391
444
  const path = url.fileURLToPath(resolution)
392
445
 
393
446
  if (protocol.exists(path)) {
394
- return protocol.postresolve(resolution.pathname, dirname)
447
+ return protocol.postresolve(path, dirname)
395
448
  }
396
449
  }
397
450
  }
@@ -449,7 +502,7 @@ const Module = module.exports = exports = class Module {
449
502
  return protocol
450
503
  }
451
504
 
452
- static _bundleFor (specifier, protocol, source = null) {
505
+ static _bundleFor (specifier, protocol) {
453
506
  let name = specifier
454
507
  do {
455
508
  if (path.extname(name) === '.bundle') {
@@ -461,76 +514,7 @@ const Module = module.exports = exports = class Module {
461
514
 
462
515
  if (path.extname(name) !== '.bundle') return null
463
516
 
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
517
+ return Module.load(name, { protocol })._bundle
534
518
  }
535
519
  }
536
520
 
@@ -687,9 +671,11 @@ Module._extensions['.bundle'] = function (module, source, referrer) {
687
671
 
688
672
  if (typeof source === 'string') source = Buffer.from(source)
689
673
 
690
- const bundle = self._bundleFor(module._filename, protocol, source)
674
+ const bundle = module._bundle = Bundle.from(source).mount(module._filename)
691
675
 
692
- module._exports = self.load(bundle.main, bundle.read(bundle.main), { protocol, referrer })._exports
676
+ if (bundle.main) {
677
+ module._exports = self.load(bundle.main, bundle.read(bundle.main), { protocol, referrer })._exports
678
+ }
693
679
  }
694
680
 
695
681
  Module._protocols['file:'] = new Protocol({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "2.6.1",
3
+ "version": "2.6.3",
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"