bare-module 2.0.1 → 2.1.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 (2) hide show
  1. package/index.js +75 -46
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -6,7 +6,6 @@ const Protocol = require('./lib/protocol')
6
6
  const constants = require('./lib/constants')
7
7
  const errors = require('./lib/errors')
8
8
  const binding = require('./binding')
9
- const addon = require.addon.bind(require)
10
9
 
11
10
  const Module = module.exports = exports = class Module {
12
11
  constructor (filename) {
@@ -88,7 +87,6 @@ const Module = module.exports = exports = class Module {
88
87
 
89
88
  static _extensions = Object.create(null)
90
89
  static _protocols = Object.create(null)
91
- static _imports = Object.create(null)
92
90
  static _cache = Object.create(null)
93
91
  static _bundles = Object.create(null)
94
92
  static _modules = new Set()
@@ -172,7 +170,7 @@ const Module = module.exports = exports = class Module {
172
170
 
173
171
  static createRequire (filename, opts = {}) {
174
172
  const {
175
- imports = this._imports,
173
+ imports = null,
176
174
  protocol = this._protocolFor(filename, this._protocols['file:']),
177
175
  type = constants.types.SCRIPT,
178
176
  defaultType = constants.types.SCRIPT
@@ -186,9 +184,10 @@ const Module = module.exports = exports = class Module {
186
184
  module._protocol = protocol
187
185
 
188
186
  const referrer = module
187
+ const dirname = path.dirname(module._filename)
189
188
 
190
189
  const resolve = (specifier) => {
191
- return this.resolve(specifier, path.dirname(module._filename), {
190
+ return this.resolve(specifier, dirname, {
192
191
  protocol: this._protocolFor(specifier, protocol),
193
192
  imports,
194
193
  referrer
@@ -205,10 +204,16 @@ const Module = module.exports = exports = class Module {
205
204
  return module._exports
206
205
  }
207
206
 
207
+ const addon = (specifier = '.') => {
208
+ return Bare.Addon.load(Bare.Addon.resolve(specifier, dirname, {
209
+ referrer
210
+ }))
211
+ }
212
+
208
213
  require.main = module._main
209
214
  require.cache = this._cache
210
215
  require.resolve = resolve
211
- require.addon = (specifier = path.dirname(module._filename)) => addon(specifier)
216
+ require.addon = addon
212
217
 
213
218
  return require
214
219
  }
@@ -220,7 +225,7 @@ const Module = module.exports = exports = class Module {
220
225
  }
221
226
 
222
227
  let {
223
- imports = this._imports,
228
+ imports = null,
224
229
  protocol = this._protocolFor(specifier, this._protocols['file:']),
225
230
  referrer = null,
226
231
  dynamic = false,
@@ -252,22 +257,8 @@ const Module = module.exports = exports = class Module {
252
257
  const module = this._cache[specifier] = new this(specifier)
253
258
 
254
259
  module._defaultType = defaultType
255
-
256
- let dirname = path.dirname(module._filename)
257
- do {
258
- const pkg = path.join(dirname, 'package.json')
259
-
260
- if (protocol.exists(pkg)) {
261
- try {
262
- module._info = this.load(pkg, { protocol })._exports
263
- } catch {}
264
- break
265
- }
266
-
267
- dirname = path.dirname(dirname)
268
- } while (dirname !== path.sep && dirname !== '.')
269
-
270
260
  module._main = main || module
261
+ module._info = this._loadPackageManifest(path.dirname(module._filename), protocol)
271
262
 
272
263
  let extension = this._extensionFor(type) || path.extname(specifier)
273
264
 
@@ -285,6 +276,22 @@ const Module = module.exports = exports = class Module {
285
276
  return this._transform(module, referrer, dynamic)
286
277
  }
287
278
 
279
+ static _loadPackageManifest (dirname, protocol) {
280
+ do {
281
+ const pkg = path.join(dirname, 'package.json')
282
+
283
+ if (protocol.exists(pkg)) {
284
+ try {
285
+ return this.load(pkg, { protocol })._exports
286
+ } catch {}
287
+ }
288
+
289
+ dirname = path.dirname(dirname)
290
+ } while (dirname !== path.sep && dirname !== '.')
291
+
292
+ return {}
293
+ }
294
+
288
295
  static resolve (specifier, dirname = os.cwd(), opts = {}) {
289
296
  if (typeof dirname !== 'string') {
290
297
  opts = dirname
@@ -292,7 +299,7 @@ const Module = module.exports = exports = class Module {
292
299
  }
293
300
 
294
301
  let {
295
- imports = this._imports,
302
+ imports = null,
296
303
  protocol = this._protocols['file:'],
297
304
  referrer = null
298
305
  } = opts
@@ -329,8 +336,9 @@ const Module = module.exports = exports = class Module {
329
336
  }
330
337
 
331
338
  static * _resolve (specifier, dirname, protocol, imports) {
332
- if (specifier in imports) specifier = imports[specifier]
333
- else if (specifier in protocol.imports) specifier = protocol.imports[specifier]
339
+ const info = this._loadPackageManifest(dirname, protocol)
340
+
341
+ specifier = this._mapConditionalSpecifier(specifier, specifier, imports, protocol.imports, info.imports)
334
342
 
335
343
  protocol = this._protocolFor(specifier, protocol)
336
344
 
@@ -378,7 +386,7 @@ const Module = module.exports = exports = class Module {
378
386
  let specifier
379
387
 
380
388
  if (info.exports) {
381
- specifier = this._mapConditionalExport('.', dirname, info.exports)
389
+ specifier = this._mapConditionalSpecifier('.', null, info.exports)
382
390
 
383
391
  if (specifier) specifier = path.join(dirname, specifier)
384
392
  else return
@@ -411,7 +419,7 @@ const Module = module.exports = exports = class Module {
411
419
 
412
420
  if (info) {
413
421
  if (info.exports) {
414
- resolved = this._mapConditionalExport(expansion, dirname, info.exports)
422
+ resolved = this._mapConditionalSpecifier(expansion, null, info.exports)
415
423
 
416
424
  if (resolved) resolved = path.join(name, resolved)
417
425
  else return
@@ -439,33 +447,31 @@ const Module = module.exports = exports = class Module {
439
447
  }
440
448
  }
441
449
 
442
- static _mapConditionalExport (specifier, dirname, exports) {
443
- if (typeof exports !== 'object') exports = { '.': exports }
450
+ static _mapConditionalSpecifier (specifier, fallback, ...specifierMaps) {
451
+ const specifiers = this._flattenSpecifierMaps(specifierMaps)
444
452
 
445
- if (specifier in exports) {
446
- specifier = search(exports[specifier])
453
+ if (specifier in specifiers) {
454
+ specifier = search(specifiers[specifier])
447
455
  } else {
448
- specifier = search(exports)
456
+ specifier = search(specifiers)
449
457
  }
450
458
 
451
- if (specifier) specifier = path.join(dirname, specifier)
452
-
453
- return specifier
459
+ return specifier || fallback
454
460
 
455
- function search (specifier) {
461
+ function search (specifiers) {
456
462
  while (true) {
457
- if (typeof specifier === 'string') return specifier
458
- if (specifier === null || typeof specifier !== 'object') return specifier
459
- specifier = first(specifier)
463
+ if (typeof specifiers === 'string') return specifiers
464
+ if (specifiers === null || typeof specifiers !== 'object') return specifiers
465
+ specifiers = first(specifiers)
460
466
  }
461
467
  }
462
468
 
463
- function first (exports) {
464
- for (const key in exports) {
469
+ function first (specifiers) {
470
+ for (const key in specifiers) {
465
471
  switch (key) {
466
472
  case 'require':
467
473
  case 'import':
468
- return exports[key]
474
+ return specifiers[key]
469
475
  }
470
476
  }
471
477
 
@@ -473,6 +479,21 @@ const Module = module.exports = exports = class Module {
473
479
  }
474
480
  }
475
481
 
482
+ static _flattenSpecifierMaps (specifierMaps) {
483
+ const specifiers = Object.create(null)
484
+
485
+ for (const map of specifierMaps) {
486
+ this._mergeSpecifierMaps(typeof map === 'object' ? map : { '.': map }, specifiers)
487
+ }
488
+
489
+ return specifiers
490
+ }
491
+
492
+ static _mergeSpecifierMaps (source, destination) {
493
+ // TODO Do a deep merge
494
+ Object.assign(destination, source)
495
+ }
496
+
476
497
  static _extensionFor (type) {
477
498
  switch (type) {
478
499
  case constants.types.SCRIPT:
@@ -624,8 +645,10 @@ Module._extensions['.cjs'] = function (module, source, referrer, protocol, impor
624
645
 
625
646
  referrer = module
626
647
 
648
+ const dirname = path.dirname(module._filename)
649
+
627
650
  const resolve = (specifier) => {
628
- return this.resolve(specifier, path.dirname(module._filename), {
651
+ return this.resolve(specifier, dirname, {
629
652
  protocol: this._protocolFor(specifier, protocol),
630
653
  imports,
631
654
  referrer
@@ -642,12 +665,18 @@ Module._extensions['.cjs'] = function (module, source, referrer, protocol, impor
642
665
  return module._exports
643
666
  }
644
667
 
645
- module._exports = {}
668
+ const addon = (specifier = '.') => {
669
+ return Bare.Addon.load(Bare.Addon.resolve(specifier, dirname, {
670
+ referrer
671
+ }))
672
+ }
646
673
 
647
674
  require.main = module._main
648
675
  require.cache = this._cache
649
676
  require.resolve = resolve
650
- require.addon = (specifier = path.dirname(module._filename)) => addon(specifier)
677
+ require.addon = addon
678
+
679
+ module._exports = {}
651
680
 
652
681
  binding.createFunction(module._filename, ['require', 'module', 'exports', '__filename', '__dirname'], source, 0)(
653
682
  require,
@@ -696,7 +725,7 @@ Module._extensions['.bare'] = function (module, source, referrer, protocol, impo
696
725
  module._protocol = protocol
697
726
  module._imports = imports
698
727
 
699
- module._exports = addon(module._filename)
728
+ module._exports = Bare.Addon.load(module._filename)
700
729
  }
701
730
 
702
731
  Module._extensions['.node'] = function (module, source, referrer, protocol, imports) {
@@ -704,7 +733,7 @@ Module._extensions['.node'] = function (module, source, referrer, protocol, impo
704
733
  module._protocol = protocol
705
734
  module._imports = imports
706
735
 
707
- module._exports = addon(module._filename)
736
+ module._exports = Bare.Addon.load(module._filename)
708
737
  }
709
738
 
710
739
  Module._extensions['.bundle'] = function (module, source, referrer, protocol, imports) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [