bare-module 2.3.0 → 2.4.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.
package/index.js CHANGED
@@ -322,7 +322,8 @@ const Module = module.exports = exports = class Module {
322
322
  let {
323
323
  imports = null,
324
324
  protocol = this._protocols['file:'],
325
- referrer = null
325
+ referrer = null,
326
+ conditions = ['import', 'require', 'bare', 'node']
326
327
  } = opts
327
328
 
328
329
  const bundle = this._bundleFor(path.dirname(specifier), protocol)
@@ -343,7 +344,7 @@ const Module = module.exports = exports = class Module {
343
344
  })
344
345
  }
345
346
 
346
- const [resolved = null] = this._resolve(specifier, dirname, protocol, imports)
347
+ const [resolved = null] = this._resolve(specifier, dirname, protocol, imports, conditions)
347
348
 
348
349
  if (resolved === null) {
349
350
  let msg = `Cannot find module '${specifier}'`
@@ -356,10 +357,14 @@ const Module = module.exports = exports = class Module {
356
357
  return protocol.postresolve(resolved, dirname)
357
358
  }
358
359
 
359
- static * _resolve (specifier, dirname, protocol, imports) {
360
+ static * _resolve (specifier, dirname, protocol, imports, conditions) {
360
361
  const info = this._loadPackageManifest(dirname, protocol)
361
362
 
362
- specifier = this._mapConditionalSpecifier(specifier, specifier, imports, protocol.imports, info.imports)
363
+ specifier = this._mapConditionalSpecifier(
364
+ specifier,
365
+ conditions,
366
+ [imports, protocol.imports, info.imports]
367
+ )
363
368
 
364
369
  protocol = this._protocolFor(specifier, protocol)
365
370
 
@@ -369,10 +374,10 @@ const Module = module.exports = exports = class Module {
369
374
 
370
375
  if (path.isAbsolute(specifier)) {
371
376
  yield * this._resolveFile(specifier, protocol)
372
- yield * this._resolveDirectory(specifier, protocol)
377
+ yield * this._resolveDirectory(specifier, protocol, conditions)
373
378
  }
374
379
 
375
- yield * this._resolveNodeModules(specifier, dirname, protocol)
380
+ yield * this._resolveNodeModules(specifier, dirname, protocol, conditions)
376
381
  }
377
382
 
378
383
  static * _resolveFile (filename, protocol) {
@@ -394,13 +399,18 @@ const Module = module.exports = exports = class Module {
394
399
  yield * this._resolveFile(path.join(dirname, 'index'), protocol)
395
400
  }
396
401
 
397
- static * _resolveDirectory (dirname, protocol) {
402
+ static * _resolveDirectory (dirname, protocol, conditions) {
398
403
  const info = this._loadPackageManifest(dirname, protocol, { traverse: false })
399
404
 
400
405
  let specifier = null
401
406
 
402
407
  if (info.exports) {
403
- specifier = this._mapConditionalSpecifier('.', null, info.exports)
408
+ specifier = this._mapConditionalSpecifier(
409
+ '.',
410
+ conditions,
411
+ [info.exports],
412
+ null // Disable fallback
413
+ )
404
414
 
405
415
  if (specifier) specifier = path.join(dirname, specifier)
406
416
  else return // Unexported
@@ -416,7 +426,7 @@ const Module = module.exports = exports = class Module {
416
426
  yield * this._resolveIndex(dirname, protocol)
417
427
  }
418
428
 
419
- static * _resolveNodeModules (specifier, dirname, protocol) {
429
+ static * _resolveNodeModules (specifier, dirname, protocol, conditions) {
420
430
  const [, name, expansion = '.'] = /^((?:@[^/\\%]+\/)?[^./\\%][^/\\%]*)(\/.*)?$/.exec(specifier) || []
421
431
 
422
432
  for (const nodeModules of this._resolveNodeModulesPaths(dirname)) {
@@ -426,7 +436,12 @@ const Module = module.exports = exports = class Module {
426
436
  const info = this._loadPackageManifest(path.join(nodeModules, name), protocol, { traverse: false })
427
437
 
428
438
  if (info.exports) {
429
- resolved = this._mapConditionalSpecifier(expansion, null, info.exports)
439
+ resolved = this._mapConditionalSpecifier(
440
+ expansion,
441
+ conditions,
442
+ [info.exports],
443
+ null // Disable fallback
444
+ )
430
445
 
431
446
  if (resolved) resolved = path.join(name, resolved)
432
447
  else return // Unexported
@@ -436,7 +451,7 @@ const Module = module.exports = exports = class Module {
436
451
  const filename = path.join(nodeModules, resolved)
437
452
 
438
453
  yield * this._resolveFile(filename, protocol)
439
- yield * this._resolveDirectory(filename, protocol)
454
+ yield * this._resolveDirectory(filename, protocol, conditions)
440
455
  }
441
456
  }
442
457
 
@@ -452,7 +467,7 @@ const Module = module.exports = exports = class Module {
452
467
  }
453
468
  }
454
469
 
455
- static _mapConditionalSpecifier (specifier, fallback, ...specifierMaps) {
470
+ static _mapConditionalSpecifier (specifier, conditions, specifierMaps, fallback = specifier) {
456
471
  const specifiers = this._flattenSpecifierMaps(specifierMaps)
457
472
 
458
473
  if (specifier in specifiers) {
@@ -467,19 +482,15 @@ const Module = module.exports = exports = class Module {
467
482
  while (true) {
468
483
  if (typeof specifiers === 'string') return specifiers
469
484
  if (specifiers === null || typeof specifiers !== 'object') return specifiers
485
+
470
486
  specifiers = first(specifiers)
471
487
  }
472
488
  }
473
489
 
474
490
  function first (specifiers) {
475
491
  for (const key in specifiers) {
476
- switch (key) {
477
- case 'require':
478
- case 'import':
479
- case 'bare':
480
- case 'node':
481
- case 'default':
482
- return specifiers[key]
492
+ if (key === 'default' || conditions.includes(key)) {
493
+ return specifiers[key]
483
494
  }
484
495
  }
485
496
 
@@ -490,14 +501,17 @@ const Module = module.exports = exports = class Module {
490
501
  static _flattenSpecifierMaps (specifierMaps) {
491
502
  const specifiers = Object.create(null)
492
503
 
493
- for (const map of specifierMaps) {
494
- this._mergeSpecifierMaps(typeof map === 'object' ? map : { '.': map }, specifiers)
504
+ for (let map of specifierMaps) {
505
+ if (typeof map === 'string') map = { '.': map }
506
+ if (map === null || typeof map !== 'object') continue
507
+
508
+ this._mergeSpecifierMaps(specifiers, map)
495
509
  }
496
510
 
497
511
  return specifiers
498
512
  }
499
513
 
500
- static _mergeSpecifierMaps (source, destination) {
514
+ static _mergeSpecifierMaps (destination, source) {
501
515
  // TODO Do a deep merge
502
516
  Object.assign(destination, source)
503
517
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "2.3.0",
3
+ "version": "2.4.0",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [