bare-module 2.5.2 → 2.5.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 +54 -64
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -383,11 +383,11 @@ const Module = module.exports = exports = class Module {
383
383
  do {
384
384
  const specifier = path.join(dirname, 'package.json')
385
385
 
386
- if (this._cache[specifier]) return this._cache[specifier]._exports
386
+ if (this._cache[specifier]) return this._cache[specifier]
387
387
 
388
388
  if (protocol.exists(specifier)) {
389
389
  try {
390
- return this.load(specifier, { protocol })._exports
390
+ return this.load(specifier, { protocol })
391
391
  } catch {}
392
392
  }
393
393
 
@@ -395,32 +395,44 @@ const Module = module.exports = exports = class Module {
395
395
  else break
396
396
  } while (dirname !== path.sep && dirname !== '.')
397
397
 
398
- return {}
398
+ return null
399
399
  }
400
400
 
401
401
  static * _resolve (specifier, dirname, protocol, imports, builtins, conditions) {
402
- const info = this._loadPackageManifest(dirname, protocol)
402
+ const pkg = this._loadPackageManifest(dirname, protocol)
403
403
 
404
- specifier = this._mapConditionalSpecifier(
405
- specifier,
406
- conditions,
407
- [imports, protocol.imports, info.imports]
408
- )
404
+ const info = (pkg && pkg._exports) || {}
409
405
 
410
- protocol = this._protocolFor(specifier, protocol)
406
+ let resolved = specifier
411
407
 
412
- specifier = protocol.preresolve(specifier, dirname)
408
+ if (info.imports) {
409
+ resolved = this._mapConditionalSpecifier(resolved, conditions, info.imports)
413
410
 
414
- yield * protocol.resolve(specifier, dirname, imports)
411
+ if (resolved) dirname = path.dirname(pkg._filename)
412
+ }
415
413
 
416
- if (builtins && specifier in builtins) yield specifier
414
+ if (protocol.imports) {
415
+ resolved = this._mapConditionalSpecifier(resolved, conditions, protocol.imports) || resolved
416
+ }
417
417
 
418
- if (path.isAbsolute(specifier)) {
419
- yield * this._resolveFile(specifier, protocol)
420
- yield * this._resolveDirectory(specifier, protocol, conditions)
418
+ if (imports) {
419
+ resolved = this._mapConditionalSpecifier(resolved, conditions, imports) || resolved
421
420
  }
422
421
 
423
- yield * this._resolveNodeModules(specifier, dirname, protocol, conditions)
422
+ protocol = this._protocolFor(resolved, protocol)
423
+
424
+ resolved = protocol.preresolve(resolved, dirname)
425
+
426
+ yield * protocol.resolve(resolved, dirname, imports)
427
+
428
+ if (builtins && resolved in builtins) yield resolved
429
+
430
+ if (path.isAbsolute(resolved)) {
431
+ yield * this._resolveFile(resolved, protocol)
432
+ yield * this._resolveDirectory(resolved, protocol, conditions)
433
+ }
434
+
435
+ yield * this._resolveNodeModules(resolved, dirname, protocol, conditions)
424
436
  }
425
437
 
426
438
  static * _resolveFile (filename, protocol) {
@@ -443,27 +455,24 @@ const Module = module.exports = exports = class Module {
443
455
  }
444
456
 
445
457
  static * _resolveDirectory (dirname, protocol, conditions) {
446
- const info = this._loadPackageManifest(dirname, protocol, { traverse: false })
458
+ const pkg = this._loadPackageManifest(dirname, protocol, { traverse: false })
447
459
 
448
- let specifier = null
460
+ const info = (pkg && pkg._exports) || {}
461
+
462
+ let resolved = null
449
463
 
450
464
  if (info.exports) {
451
- specifier = this._mapConditionalSpecifier(
452
- '.',
453
- conditions,
454
- [info.exports],
455
- null // Disable fallback
456
- )
457
-
458
- if (specifier) specifier = path.join(dirname, specifier)
465
+ resolved = this._mapConditionalSpecifier('.', conditions, info.exports)
466
+
467
+ if (resolved) resolved = path.join(dirname, resolved)
459
468
  else return // Unexported
460
469
  } else if (info.main) {
461
- specifier = path.join(dirname, info.main)
470
+ resolved = path.join(dirname, info.main)
462
471
  }
463
472
 
464
- if (specifier) {
465
- yield * this._resolveFile(specifier, protocol)
466
- yield * this._resolveIndex(specifier, protocol)
473
+ if (resolved) {
474
+ yield * this._resolveFile(resolved, protocol)
475
+ yield * this._resolveIndex(resolved, protocol)
467
476
  }
468
477
 
469
478
  yield * this._resolveIndex(dirname, protocol)
@@ -476,15 +485,12 @@ const Module = module.exports = exports = class Module {
476
485
  let resolved = specifier
477
486
 
478
487
  if (name) {
479
- const info = this._loadPackageManifest(path.join(nodeModules, name), protocol, { traverse: false })
488
+ const pkg = this._loadPackageManifest(path.join(nodeModules, name), protocol, { traverse: false })
489
+
490
+ const info = (pkg && pkg._exports) || {}
480
491
 
481
492
  if (info.exports) {
482
- resolved = this._mapConditionalSpecifier(
483
- expansion,
484
- conditions,
485
- [info.exports],
486
- null // Disable fallback
487
- )
493
+ resolved = this._mapConditionalSpecifier(expansion, conditions, info.exports)
488
494
 
489
495
  if (resolved) resolved = path.join(name, resolved)
490
496
  else return // Unexported
@@ -510,16 +516,16 @@ const Module = module.exports = exports = class Module {
510
516
  }
511
517
  }
512
518
 
513
- static _mapConditionalSpecifier (specifier, conditions, specifierMaps, fallback = specifier) {
514
- const specifiers = this._flattenSpecifierMaps(specifierMaps)
519
+ static _mapConditionalSpecifier (specifier, conditions, specifierMap) {
520
+ if (typeof specifierMap === 'string') specifierMap = { '.': specifierMap }
515
521
 
516
- if (specifier in specifiers) {
517
- specifier = search(specifiers[specifier])
522
+ if (specifier in specifierMap) {
523
+ specifier = search(specifierMap[specifier])
518
524
  } else {
519
- specifier = search(specifiers)
525
+ specifier = search(specifierMap)
520
526
  }
521
527
 
522
- return specifier || fallback
528
+ return specifier
523
529
 
524
530
  function search (specifiers) {
525
531
  while (true) {
@@ -541,24 +547,6 @@ const Module = module.exports = exports = class Module {
541
547
  }
542
548
  }
543
549
 
544
- static _flattenSpecifierMaps (specifierMaps) {
545
- const specifiers = Object.create(null)
546
-
547
- for (let map of specifierMaps) {
548
- if (typeof map === 'string') map = { '.': map }
549
- if (map === null || typeof map !== 'object') continue
550
-
551
- this._mergeSpecifierMaps(specifiers, map)
552
- }
553
-
554
- return specifiers
555
- }
556
-
557
- static _mergeSpecifierMaps (destination, source) {
558
- // TODO Do a deep merge
559
- Object.assign(destination, source)
560
- }
561
-
562
550
  static _extensionFor (type) {
563
551
  switch (type) {
564
552
  case constants.types.SCRIPT:
@@ -686,7 +674,9 @@ Module._extensions['.js'] = function (module, source, referrer) {
686
674
 
687
675
  const protocol = module._protocol
688
676
 
689
- const info = self._loadPackageManifest(path.dirname(module._filename), protocol)
677
+ const pkg = self._loadPackageManifest(path.dirname(module._filename), protocol)
678
+
679
+ const info = (pkg && pkg._exports) || {}
690
680
 
691
681
  const isESM = (
692
682
  // The default type is ES modules.
@@ -825,7 +815,7 @@ Module._protocols['file:'] = new Protocol({
825
815
  preresolve (specifier, dirname) {
826
816
  specifier = specifier.replace(/^file:/, '')
827
817
 
828
- if (specifier[0] === '.') specifier = path.join(dirname, specifier)
818
+ if (/^\.(\/|\\)/.test(specifier)) specifier = path.join(dirname, specifier)
829
819
  else if (path.isAbsolute(specifier)) specifier = path.normalize(specifier)
830
820
 
831
821
  return specifier
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "2.5.2",
3
+ "version": "2.5.3",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [