bare-module 2.3.1 → 2.5.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 +61 -38
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -239,9 +239,10 @@ const Module = module.exports = exports = class Module {
239
239
  }
240
240
 
241
241
  let {
242
- imports = null,
243
- protocol = this._protocolFor(specifier, this._protocols['file:']),
244
242
  referrer = null,
243
+ protocol = this._protocolFor(specifier, this._protocols['file:']),
244
+ imports = null,
245
+ builtins = null,
245
246
  dynamic = false,
246
247
  main = referrer ? referrer._main : null,
247
248
  type = 0,
@@ -270,22 +271,26 @@ const Module = module.exports = exports = class Module {
270
271
 
271
272
  const module = this._cache[specifier] = new this(specifier)
272
273
 
273
- module._defaultType = defaultType
274
- module._main = main || module
275
- module._info = this._loadPackageManifest(path.dirname(module._filename), protocol)
274
+ if (builtins && specifier in builtins) {
275
+ module._exports = builtins[specifier]
276
+ } else {
277
+ module._defaultType = defaultType
278
+ module._main = main || module
279
+ module._info = this._loadPackageManifest(path.dirname(module._filename), protocol)
276
280
 
277
- let extension = this._extensionFor(type) || path.extname(specifier)
281
+ let extension = this._extensionFor(type) || path.extname(specifier)
278
282
 
279
- if (extension in this._extensions === false) {
280
- if (defaultType) extension = this._extensionFor(defaultType) || '.js'
281
- else extension = '.js'
282
- }
283
+ if (extension in this._extensions === false) {
284
+ if (defaultType) extension = this._extensionFor(defaultType) || '.js'
285
+ else extension = '.js'
286
+ }
283
287
 
284
- if (extension === '.bundle' && path.extname(specifier) !== extension) {
285
- throw errors.INVALID_BUNDLE_EXTENSION(`Invalid extension for bundle '${specifier}'`)
286
- }
288
+ if (extension === '.bundle' && path.extname(specifier) !== extension) {
289
+ throw errors.INVALID_BUNDLE_EXTENSION(`Invalid extension for bundle '${specifier}'`)
290
+ }
287
291
 
288
- this._extensions[extension].call(this, module, source, referrer, protocol, imports)
292
+ this._extensions[extension].call(this, module, source, referrer, protocol, imports)
293
+ }
289
294
 
290
295
  return this._transform(module, referrer, dynamic)
291
296
  }
@@ -320,9 +325,11 @@ const Module = module.exports = exports = class Module {
320
325
  }
321
326
 
322
327
  let {
323
- imports = null,
328
+ referrer = null,
324
329
  protocol = this._protocols['file:'],
325
- referrer = null
330
+ imports = null,
331
+ builtins = null,
332
+ conditions = ['import', 'require', 'bare', 'node']
326
333
  } = opts
327
334
 
328
335
  const bundle = this._bundleFor(path.dirname(specifier), protocol)
@@ -343,7 +350,7 @@ const Module = module.exports = exports = class Module {
343
350
  })
344
351
  }
345
352
 
346
- const [resolved = null] = this._resolve(specifier, dirname, protocol, imports)
353
+ const [resolved = null] = this._resolve(specifier, dirname, protocol, imports, builtins, conditions)
347
354
 
348
355
  if (resolved === null) {
349
356
  let msg = `Cannot find module '${specifier}'`
@@ -356,10 +363,14 @@ const Module = module.exports = exports = class Module {
356
363
  return protocol.postresolve(resolved, dirname)
357
364
  }
358
365
 
359
- static * _resolve (specifier, dirname, protocol, imports) {
366
+ static * _resolve (specifier, dirname, protocol, imports, builtins, conditions) {
360
367
  const info = this._loadPackageManifest(dirname, protocol)
361
368
 
362
- specifier = this._mapConditionalSpecifier(specifier, specifier, imports, protocol.imports, info.imports)
369
+ specifier = this._mapConditionalSpecifier(
370
+ specifier,
371
+ conditions,
372
+ [imports, protocol.imports, info.imports]
373
+ )
363
374
 
364
375
  protocol = this._protocolFor(specifier, protocol)
365
376
 
@@ -367,12 +378,14 @@ const Module = module.exports = exports = class Module {
367
378
 
368
379
  yield * protocol.resolve(specifier, dirname, imports)
369
380
 
381
+ if (builtins && specifier in builtins) yield specifier
382
+
370
383
  if (path.isAbsolute(specifier)) {
371
384
  yield * this._resolveFile(specifier, protocol)
372
- yield * this._resolveDirectory(specifier, protocol)
385
+ yield * this._resolveDirectory(specifier, protocol, conditions)
373
386
  }
374
387
 
375
- yield * this._resolveNodeModules(specifier, dirname, protocol)
388
+ yield * this._resolveNodeModules(specifier, dirname, protocol, conditions)
376
389
  }
377
390
 
378
391
  static * _resolveFile (filename, protocol) {
@@ -394,13 +407,18 @@ const Module = module.exports = exports = class Module {
394
407
  yield * this._resolveFile(path.join(dirname, 'index'), protocol)
395
408
  }
396
409
 
397
- static * _resolveDirectory (dirname, protocol) {
410
+ static * _resolveDirectory (dirname, protocol, conditions) {
398
411
  const info = this._loadPackageManifest(dirname, protocol, { traverse: false })
399
412
 
400
413
  let specifier = null
401
414
 
402
415
  if (info.exports) {
403
- specifier = this._mapConditionalSpecifier('.', null, info.exports)
416
+ specifier = this._mapConditionalSpecifier(
417
+ '.',
418
+ conditions,
419
+ [info.exports],
420
+ null // Disable fallback
421
+ )
404
422
 
405
423
  if (specifier) specifier = path.join(dirname, specifier)
406
424
  else return // Unexported
@@ -416,7 +434,7 @@ const Module = module.exports = exports = class Module {
416
434
  yield * this._resolveIndex(dirname, protocol)
417
435
  }
418
436
 
419
- static * _resolveNodeModules (specifier, dirname, protocol) {
437
+ static * _resolveNodeModules (specifier, dirname, protocol, conditions) {
420
438
  const [, name, expansion = '.'] = /^((?:@[^/\\%]+\/)?[^./\\%][^/\\%]*)(\/.*)?$/.exec(specifier) || []
421
439
 
422
440
  for (const nodeModules of this._resolveNodeModulesPaths(dirname)) {
@@ -426,7 +444,12 @@ const Module = module.exports = exports = class Module {
426
444
  const info = this._loadPackageManifest(path.join(nodeModules, name), protocol, { traverse: false })
427
445
 
428
446
  if (info.exports) {
429
- resolved = this._mapConditionalSpecifier(expansion, null, info.exports)
447
+ resolved = this._mapConditionalSpecifier(
448
+ expansion,
449
+ conditions,
450
+ [info.exports],
451
+ null // Disable fallback
452
+ )
430
453
 
431
454
  if (resolved) resolved = path.join(name, resolved)
432
455
  else return // Unexported
@@ -436,7 +459,7 @@ const Module = module.exports = exports = class Module {
436
459
  const filename = path.join(nodeModules, resolved)
437
460
 
438
461
  yield * this._resolveFile(filename, protocol)
439
- yield * this._resolveDirectory(filename, protocol)
462
+ yield * this._resolveDirectory(filename, protocol, conditions)
440
463
  }
441
464
  }
442
465
 
@@ -452,7 +475,7 @@ const Module = module.exports = exports = class Module {
452
475
  }
453
476
  }
454
477
 
455
- static _mapConditionalSpecifier (specifier, fallback, ...specifierMaps) {
478
+ static _mapConditionalSpecifier (specifier, conditions, specifierMaps, fallback = specifier) {
456
479
  const specifiers = this._flattenSpecifierMaps(specifierMaps)
457
480
 
458
481
  if (specifier in specifiers) {
@@ -467,19 +490,15 @@ const Module = module.exports = exports = class Module {
467
490
  while (true) {
468
491
  if (typeof specifiers === 'string') return specifiers
469
492
  if (specifiers === null || typeof specifiers !== 'object') return specifiers
493
+
470
494
  specifiers = first(specifiers)
471
495
  }
472
496
  }
473
497
 
474
498
  function first (specifiers) {
475
499
  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]
500
+ if (key === 'default' || conditions.includes(key)) {
501
+ return specifiers[key]
483
502
  }
484
503
  }
485
504
 
@@ -490,14 +509,17 @@ const Module = module.exports = exports = class Module {
490
509
  static _flattenSpecifierMaps (specifierMaps) {
491
510
  const specifiers = Object.create(null)
492
511
 
493
- for (const map of specifierMaps) {
494
- this._mergeSpecifierMaps(typeof map === 'object' ? map : { '.': map }, specifiers)
512
+ for (let map of specifierMaps) {
513
+ if (typeof map === 'string') map = { '.': map }
514
+ if (map === null || typeof map !== 'object') continue
515
+
516
+ this._mergeSpecifierMaps(specifiers, map)
495
517
  }
496
518
 
497
519
  return specifiers
498
520
  }
499
521
 
500
- static _mergeSpecifierMaps (source, destination) {
522
+ static _mergeSpecifierMaps (destination, source) {
501
523
  // TODO Do a deep merge
502
524
  Object.assign(destination, source)
503
525
  }
@@ -769,7 +791,8 @@ Module._protocols['file:'] = new Protocol({
769
791
  },
770
792
 
771
793
  postresolve (specifier) {
772
- return binding.realpath(specifier)
794
+ if (path.isAbsolute(specifier)) return binding.realpath(specifier)
795
+ return specifier
773
796
  },
774
797
 
775
798
  exists (filename) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "2.3.1",
3
+ "version": "2.5.0",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [