bare-module 2.5.2 → 2.5.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 +55 -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,45 @@ 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
+ else resolved = specifier
413
+ }
415
414
 
416
- if (builtins && specifier in builtins) yield specifier
415
+ if (protocol.imports) {
416
+ resolved = this._mapConditionalSpecifier(resolved, conditions, protocol.imports) || resolved
417
+ }
417
418
 
418
- if (path.isAbsolute(specifier)) {
419
- yield * this._resolveFile(specifier, protocol)
420
- yield * this._resolveDirectory(specifier, protocol, conditions)
419
+ if (imports) {
420
+ resolved = this._mapConditionalSpecifier(resolved, conditions, imports) || resolved
421
421
  }
422
422
 
423
- yield * this._resolveNodeModules(specifier, dirname, protocol, conditions)
423
+ protocol = this._protocolFor(resolved, protocol)
424
+
425
+ resolved = protocol.preresolve(resolved, dirname)
426
+
427
+ yield * protocol.resolve(resolved, dirname, imports)
428
+
429
+ if (builtins && resolved in builtins) yield resolved
430
+
431
+ if (path.isAbsolute(resolved)) {
432
+ yield * this._resolveFile(resolved, protocol)
433
+ yield * this._resolveDirectory(resolved, protocol, conditions)
434
+ }
435
+
436
+ yield * this._resolveNodeModules(resolved, dirname, protocol, conditions)
424
437
  }
425
438
 
426
439
  static * _resolveFile (filename, protocol) {
@@ -443,27 +456,24 @@ const Module = module.exports = exports = class Module {
443
456
  }
444
457
 
445
458
  static * _resolveDirectory (dirname, protocol, conditions) {
446
- const info = this._loadPackageManifest(dirname, protocol, { traverse: false })
459
+ const pkg = this._loadPackageManifest(dirname, protocol, { traverse: false })
447
460
 
448
- let specifier = null
461
+ const info = (pkg && pkg._exports) || {}
462
+
463
+ let resolved = null
449
464
 
450
465
  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)
466
+ resolved = this._mapConditionalSpecifier('.', conditions, info.exports)
467
+
468
+ if (resolved) resolved = path.join(dirname, resolved)
459
469
  else return // Unexported
460
470
  } else if (info.main) {
461
- specifier = path.join(dirname, info.main)
471
+ resolved = path.join(dirname, info.main)
462
472
  }
463
473
 
464
- if (specifier) {
465
- yield * this._resolveFile(specifier, protocol)
466
- yield * this._resolveIndex(specifier, protocol)
474
+ if (resolved) {
475
+ yield * this._resolveFile(resolved, protocol)
476
+ yield * this._resolveIndex(resolved, protocol)
467
477
  }
468
478
 
469
479
  yield * this._resolveIndex(dirname, protocol)
@@ -476,15 +486,12 @@ const Module = module.exports = exports = class Module {
476
486
  let resolved = specifier
477
487
 
478
488
  if (name) {
479
- const info = this._loadPackageManifest(path.join(nodeModules, name), protocol, { traverse: false })
489
+ const pkg = this._loadPackageManifest(path.join(nodeModules, name), protocol, { traverse: false })
490
+
491
+ const info = (pkg && pkg._exports) || {}
480
492
 
481
493
  if (info.exports) {
482
- resolved = this._mapConditionalSpecifier(
483
- expansion,
484
- conditions,
485
- [info.exports],
486
- null // Disable fallback
487
- )
494
+ resolved = this._mapConditionalSpecifier(expansion, conditions, info.exports)
488
495
 
489
496
  if (resolved) resolved = path.join(name, resolved)
490
497
  else return // Unexported
@@ -510,16 +517,16 @@ const Module = module.exports = exports = class Module {
510
517
  }
511
518
  }
512
519
 
513
- static _mapConditionalSpecifier (specifier, conditions, specifierMaps, fallback = specifier) {
514
- const specifiers = this._flattenSpecifierMaps(specifierMaps)
520
+ static _mapConditionalSpecifier (specifier, conditions, specifierMap) {
521
+ if (typeof specifierMap === 'string') specifierMap = { '.': specifierMap }
515
522
 
516
- if (specifier in specifiers) {
517
- specifier = search(specifiers[specifier])
523
+ if (specifier in specifierMap) {
524
+ specifier = search(specifierMap[specifier])
518
525
  } else {
519
- specifier = search(specifiers)
526
+ specifier = search(specifierMap)
520
527
  }
521
528
 
522
- return specifier || fallback
529
+ return specifier
523
530
 
524
531
  function search (specifiers) {
525
532
  while (true) {
@@ -541,24 +548,6 @@ const Module = module.exports = exports = class Module {
541
548
  }
542
549
  }
543
550
 
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
551
  static _extensionFor (type) {
563
552
  switch (type) {
564
553
  case constants.types.SCRIPT:
@@ -686,7 +675,9 @@ Module._extensions['.js'] = function (module, source, referrer) {
686
675
 
687
676
  const protocol = module._protocol
688
677
 
689
- const info = self._loadPackageManifest(path.dirname(module._filename), protocol)
678
+ const pkg = self._loadPackageManifest(path.dirname(module._filename), protocol)
679
+
680
+ const info = (pkg && pkg._exports) || {}
690
681
 
691
682
  const isESM = (
692
683
  // The default type is ES modules.
@@ -825,7 +816,7 @@ Module._protocols['file:'] = new Protocol({
825
816
  preresolve (specifier, dirname) {
826
817
  specifier = specifier.replace(/^file:/, '')
827
818
 
828
- if (specifier[0] === '.') specifier = path.join(dirname, specifier)
819
+ if (specifier === '.' || specifier.startsWith('./') || specifier.startsWith('.\\')) specifier = path.join(dirname, specifier)
829
820
  else if (path.isAbsolute(specifier)) specifier = path.normalize(specifier)
830
821
 
831
822
  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.4",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [