bare-module 1.14.3 → 1.14.5

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/binding.c CHANGED
@@ -542,7 +542,10 @@ bare_module_read (js_env_t *env, js_callback_info_t *info) {
542
542
  int fd = req.result;
543
543
  uv_fs_req_cleanup(&req);
544
544
 
545
- if (fd < 0) goto err;
545
+ if (fd < 0) {
546
+ err = fd;
547
+ goto err;
548
+ }
546
549
 
547
550
  uv_fs_fstat(loop, &req, fd, NULL);
548
551
  uv_stat_t *st = req.ptr;
package/index.js CHANGED
@@ -56,7 +56,7 @@ module.exports = exports = class Module {
56
56
  return this.filename
57
57
  }
58
58
 
59
- // For Node.s compatibility
59
+ // For Node.js compatibility
60
60
  get path () {
61
61
  return this.dirname
62
62
  }
@@ -238,7 +238,7 @@ module.exports = exports = class Module {
238
238
 
239
239
  if (this._cache[specifier]) return this._transform(this._cache[specifier], referrer, dynamic)
240
240
 
241
- const bundle = this._bundleFor(path.dirname(specifier), protocol, source)
241
+ const bundle = this._bundleFor(path.dirname(specifier), protocol)
242
242
 
243
243
  if (bundle) {
244
244
  protocol = new Protocol({
@@ -343,9 +343,7 @@ module.exports = exports = class Module {
343
343
 
344
344
  specifier = protocol.preresolve(specifier, dirname)
345
345
 
346
- if (protocol.resolve) {
347
- yield * protocol.resolve(specifier, dirname, imports)
348
- }
346
+ yield * protocol.resolve(specifier, dirname, imports)
349
347
 
350
348
  if (this.isBuiltin(specifier)) {
351
349
  yield specifier
@@ -384,18 +382,25 @@ module.exports = exports = class Module {
384
382
  const pkg = path.join(dirname, 'package.json')
385
383
 
386
384
  if (protocol.exists(pkg)) {
387
- let info
385
+ let info = null
388
386
  try {
389
387
  info = this.load(pkg, { protocol })._exports
390
- } catch {
391
- info = null
392
- }
388
+ } catch {}
393
389
 
394
- if (info && info.main) {
395
- const main = path.join(dirname, info.main)
390
+ if (info) {
391
+ let specifier
396
392
 
397
- yield * this._resolveFile(main, protocol)
398
- yield * this._resolveIndex(main, protocol)
393
+ if (info.exports) {
394
+ specifier = this._mapConditionalExport('.', dirname, info.exports)
395
+
396
+ if (specifier) specifier = path.join(dirname, specifier)
397
+ else return
398
+ } else if (info.main) {
399
+ specifier = path.join(dirname, info.main)
400
+ }
401
+
402
+ yield * this._resolveFile(specifier, protocol)
403
+ yield * this._resolveIndex(specifier, protocol)
399
404
  }
400
405
  }
401
406
 
@@ -404,6 +409,28 @@ module.exports = exports = class Module {
404
409
 
405
410
  static * _resolveNodeModules (specifier, dirname, protocol) {
406
411
  for (const nodeModules of this._resolveNodeModulesPaths(dirname)) {
412
+ const [, name, expansion = '.'] = /^((?:@[^/\\%]+\/)?[^./\\%][^/\\%]*)(\/.*)?$/.exec(specifier) || []
413
+
414
+ if (name) {
415
+ const pkg = path.join(nodeModules, name, 'package.json')
416
+
417
+ if (protocol.exists(pkg)) {
418
+ let info = null
419
+ try {
420
+ info = this.load(pkg, { protocol })._exports
421
+ } catch {}
422
+
423
+ if (info) {
424
+ if (info.exports) {
425
+ specifier = this._mapConditionalExport(expansion, dirname, info.exports)
426
+
427
+ if (specifier) specifier = path.join(name, specifier)
428
+ else return
429
+ }
430
+ }
431
+ }
432
+ }
433
+
407
434
  const filename = path.join(nodeModules, specifier)
408
435
 
409
436
  yield * this._resolveFile(filename, protocol)
@@ -423,6 +450,40 @@ module.exports = exports = class Module {
423
450
  }
424
451
  }
425
452
 
453
+ static _mapConditionalExport (specifier, dirname, exports) {
454
+ if (typeof exports !== 'object') exports = { '.': exports }
455
+
456
+ if (specifier in exports) {
457
+ specifier = search(exports[specifier])
458
+ } else {
459
+ specifier = search(exports)
460
+ }
461
+
462
+ if (specifier) specifier = path.join(dirname, specifier)
463
+
464
+ return specifier
465
+
466
+ function search (specifier) {
467
+ while (true) {
468
+ if (typeof specifier === 'string') return specifier
469
+ if (specifier === null || typeof specifier !== 'object') return specifier
470
+ specifier = first(specifier)
471
+ }
472
+ }
473
+
474
+ function first (exports) {
475
+ for (const key in exports) {
476
+ switch (key) {
477
+ case 'require':
478
+ case 'import':
479
+ return exports[key]
480
+ }
481
+ }
482
+
483
+ return null
484
+ }
485
+ }
486
+
426
487
  static _extensionFor (type) {
427
488
  switch (type) {
428
489
  case constants.types.SCRIPT:
@@ -638,6 +699,8 @@ exports._extensions['.node'] = function (module, source, referrer, protocol, imp
638
699
  }
639
700
 
640
701
  exports._extensions['.bundle'] = function (module, source, referrer, protocol, imports) {
702
+ if (source === null) source = protocol.read(module._filename)
703
+
641
704
  if (typeof source === 'string') source = Buffer.from(source)
642
705
 
643
706
  const bundle = this._bundleFor(module._filename, protocol, source)
package/lib/protocol.js CHANGED
@@ -1,4 +1,4 @@
1
- module.exports = class Protocol {
1
+ module.exports = class ModuleProtocol {
2
2
  constructor (opts = {}) {
3
3
  const {
4
4
  imports = Object.create(null),
@@ -13,9 +13,7 @@ module.exports = class Protocol {
13
13
 
14
14
  if (preresolve) this.preresolve = preresolve.bind(this)
15
15
  if (postresolve) this.postresolve = postresolve.bind(this)
16
-
17
16
  if (resolve) this.resolve = resolve.bind(this)
18
- else this.resolve = null
19
17
  if (exists) this.exists = exists.bind(this)
20
18
  if (read) this.read = read.bind(this)
21
19
  }
@@ -28,6 +26,8 @@ module.exports = class Protocol {
28
26
  return specifier
29
27
  }
30
28
 
29
+ * resolve (specifier, dirname, imports) {}
30
+
31
31
  exists (filename) {
32
32
  return false
33
33
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "1.14.3",
3
+ "version": "1.14.5",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [