bare-module 2.5.3 → 2.6.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 (3) hide show
  1. package/index.js +56 -184
  2. package/lib/protocol.js +1 -1
  3. package/package.json +4 -2
package/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  /* global Bare */
2
2
  const path = require('bare-path')
3
3
  const os = require('bare-os')
4
+ const url = require('bare-url')
5
+ const resolve = require('bare-module-resolve')
4
6
  const Bundle = require('bare-bundle')
5
7
  const Protocol = require('./lib/protocol')
6
8
  const constants = require('./lib/constants')
@@ -171,7 +173,6 @@ const Module = module.exports = exports = class Module {
171
173
  function resolve (specifier) {
172
174
  return self.resolve(specifier, dirname, {
173
175
  protocol: self._protocolFor(specifier, module._protocol),
174
- imports: module._imports,
175
176
  referrer
176
177
  })
177
178
  }
@@ -287,8 +288,6 @@ const Module = module.exports = exports = class Module {
287
288
  protocol = new Protocol({
288
289
  imports: bundle.imports,
289
290
 
290
- preresolve: self._protocols['file:'].preresolve,
291
-
292
291
  exists (filename) {
293
292
  return bundle.exists(filename)
294
293
  },
@@ -350,8 +349,6 @@ const Module = module.exports = exports = class Module {
350
349
  protocol = new Protocol({
351
350
  imports: bundle.imports,
352
351
 
353
- preresolve: self._protocols['file:'].preresolve,
354
-
355
352
  exists (filename) {
356
353
  return bundle.exists(filename)
357
354
  },
@@ -362,185 +359,55 @@ const Module = module.exports = exports = class Module {
362
359
  })
363
360
  }
364
361
 
365
- const [resolved = null] = self._resolve(specifier, dirname, protocol, imports, builtins, conditions)
366
-
367
- if (resolved === null) {
368
- let msg = `Cannot find module '${specifier}'`
369
-
370
- if (referrer) msg += ` imported from '${referrer._filename}'`
371
-
372
- throw errors.MODULE_NOT_FOUND(msg)
373
- }
362
+ const resolved = protocol.preresolve(specifier, dirname)
374
363
 
375
- return protocol.postresolve(resolved, dirname)
376
- }
377
-
378
- static _loadPackageManifest (dirname, protocol, opts = {}) {
379
- const {
380
- traverse = true
381
- } = opts
382
-
383
- do {
384
- const specifier = path.join(dirname, 'package.json')
364
+ const [resolution] = protocol.resolve(specifier, dirname, imports)
385
365
 
386
- if (this._cache[specifier]) return this._cache[specifier]
387
-
388
- if (protocol.exists(specifier)) {
389
- try {
390
- return this.load(specifier, { protocol })
391
- } catch {}
392
- }
393
-
394
- if (traverse) dirname = path.dirname(dirname)
395
- else break
396
- } while (dirname !== path.sep && dirname !== '.')
397
-
398
- return null
399
- }
400
-
401
- static * _resolve (specifier, dirname, protocol, imports, builtins, conditions) {
402
- const pkg = this._loadPackageManifest(dirname, protocol)
403
-
404
- const info = (pkg && pkg._exports) || {}
405
-
406
- let resolved = specifier
407
-
408
- if (info.imports) {
409
- resolved = this._mapConditionalSpecifier(resolved, conditions, info.imports)
410
-
411
- if (resolved) dirname = path.dirname(pkg._filename)
412
- }
366
+ if (resolution) return protocol.postresolve(resolution, dirname)
413
367
 
414
368
  if (protocol.imports) {
415
- resolved = this._mapConditionalSpecifier(resolved, conditions, protocol.imports) || resolved
416
- }
417
-
418
- if (imports) {
419
- resolved = this._mapConditionalSpecifier(resolved, conditions, imports) || resolved
420
- }
421
-
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)
436
- }
437
-
438
- static * _resolveFile (filename, protocol) {
439
- const extensions = [
440
- '.js',
441
- '.cjs',
442
- '.mjs',
443
- '.json',
444
- '.bare',
445
- '.node'
446
- ]
447
-
448
- for (const candidate of [filename, ...extensions.map(ext => filename + ext)]) {
449
- if (protocol.exists(candidate)) yield candidate
450
- }
451
- }
452
-
453
- static * _resolveIndex (dirname, protocol) {
454
- yield * this._resolveFile(path.join(dirname, 'index'), protocol)
455
- }
456
-
457
- static * _resolveDirectory (dirname, protocol, conditions) {
458
- const pkg = this._loadPackageManifest(dirname, protocol, { traverse: false })
459
-
460
- const info = (pkg && pkg._exports) || {}
461
-
462
- let resolved = null
463
-
464
- if (info.exports) {
465
- resolved = this._mapConditionalSpecifier('.', conditions, info.exports)
466
-
467
- if (resolved) resolved = path.join(dirname, resolved)
468
- else return // Unexported
469
- } else if (info.main) {
470
- resolved = path.join(dirname, info.main)
471
- }
472
-
473
- if (resolved) {
474
- yield * this._resolveFile(resolved, protocol)
475
- yield * this._resolveIndex(resolved, protocol)
476
- }
477
-
478
- yield * this._resolveIndex(dirname, protocol)
479
- }
480
-
481
- static * _resolveNodeModules (specifier, dirname, protocol, conditions) {
482
- const [, name, expansion = '.'] = /^((?:@[^/\\%]+\/)?[^./\\%][^/\\%]*)(\/.*)?$/.exec(specifier) || []
483
-
484
- for (const nodeModules of this._resolveNodeModulesPaths(dirname)) {
485
- let resolved = specifier
486
-
487
- if (name) {
488
- const pkg = this._loadPackageManifest(path.join(nodeModules, name), protocol, { traverse: false })
489
-
490
- const info = (pkg && pkg._exports) || {}
491
-
492
- if (info.exports) {
493
- resolved = this._mapConditionalSpecifier(expansion, conditions, info.exports)
494
-
495
- if (resolved) resolved = path.join(name, resolved)
496
- else return // Unexported
369
+ imports = Object.assign(Object.create(null), protocol.imports, imports)
370
+ }
371
+
372
+ const parentURL = url.pathToFileURL(dirname[dirname.length - 1] === path.sep ? dirname : dirname + '/')
373
+
374
+ for (const resolution of resolve(resolved, parentURL, {
375
+ conditions,
376
+ imports,
377
+ builtins: builtins ? Object.keys(builtins) : [],
378
+ extensions: [
379
+ '.js',
380
+ '.cjs',
381
+ '.mjs',
382
+ '.json',
383
+ '.bare',
384
+ '.node'
385
+ ]
386
+ }, readPackage)) {
387
+ switch (resolution.protocol) {
388
+ case 'builtin:': return resolution.pathname
389
+
390
+ case 'file:': {
391
+ const path = url.fileURLToPath(resolution)
392
+
393
+ if (protocol.exists(path)) {
394
+ return protocol.postresolve(resolution.pathname, dirname)
395
+ }
497
396
  }
498
397
  }
499
-
500
- const filename = path.join(nodeModules, resolved)
501
-
502
- yield * this._resolveFile(filename, protocol)
503
- yield * this._resolveDirectory(filename, protocol, conditions)
504
398
  }
505
- }
506
399
 
507
- static * _resolveNodeModulesPaths (start) {
508
- if (start === path.sep) return yield path.join(start, 'node_modules')
400
+ let msg = `Cannot find module '${specifier}'`
509
401
 
510
- const parts = start.split(path.sep)
402
+ if (referrer) msg += ` imported from '${referrer._filename}'`
511
403
 
512
- for (let i = parts.length - 1; i >= 0; i--) {
513
- if (parts[i] !== 'node_modules') {
514
- yield path.join(parts.slice(0, i + 1).join(path.sep), 'node_modules')
515
- }
516
- }
517
- }
404
+ throw errors.MODULE_NOT_FOUND(msg)
518
405
 
519
- static _mapConditionalSpecifier (specifier, conditions, specifierMap) {
520
- if (typeof specifierMap === 'string') specifierMap = { '.': specifierMap }
406
+ function readPackage (packageURL) {
407
+ const path = url.fileURLToPath(packageURL)
521
408
 
522
- if (specifier in specifierMap) {
523
- specifier = search(specifierMap[specifier])
524
- } else {
525
- specifier = search(specifierMap)
526
- }
527
-
528
- return specifier
529
-
530
- function search (specifiers) {
531
- while (true) {
532
- if (typeof specifiers === 'string') return specifiers
533
- if (specifiers === null || typeof specifiers !== 'object') return specifiers
534
-
535
- specifiers = first(specifiers)
536
- }
537
- }
538
-
539
- function first (specifiers) {
540
- for (const key in specifiers) {
541
- if (key === 'default' || conditions.includes(key)) {
542
- return specifiers[key]
543
- }
409
+ if (protocol.exists(path)) {
410
+ return Module.load(path, { protocol })._exports
544
411
  }
545
412
 
546
413
  return null
@@ -604,8 +471,6 @@ const Module = module.exports = exports = class Module {
604
471
  protocol = new Protocol({
605
472
  imports: parent.imports,
606
473
 
607
- preresolve: this._protocols['file:'].preresolve,
608
-
609
474
  exists (filename) {
610
475
  return parent.exists(filename)
611
476
  },
@@ -674,7 +539,23 @@ Module._extensions['.js'] = function (module, source, referrer) {
674
539
 
675
540
  const protocol = module._protocol
676
541
 
677
- const pkg = self._loadPackageManifest(path.dirname(module._filename), protocol)
542
+ let pkg
543
+ let dirname = path.dirname(module._filename)
544
+ do {
545
+ const specifier = path.join(dirname, 'package.json')
546
+
547
+ if (self._cache[specifier]) {
548
+ pkg = self._cache[specifier]
549
+ break
550
+ }
551
+
552
+ if (protocol.exists(specifier)) {
553
+ pkg = self.load(specifier, { protocol })
554
+ break
555
+ }
556
+
557
+ dirname = path.dirname(dirname)
558
+ } while (dirname !== path.sep && dirname !== '.')
678
559
 
679
560
  const info = (pkg && pkg._exports) || {}
680
561
 
@@ -812,15 +693,6 @@ Module._extensions['.bundle'] = function (module, source, referrer) {
812
693
  }
813
694
 
814
695
  Module._protocols['file:'] = new Protocol({
815
- preresolve (specifier, dirname) {
816
- specifier = specifier.replace(/^file:/, '')
817
-
818
- if (/^\.(\/|\\)/.test(specifier)) specifier = path.join(dirname, specifier)
819
- else if (path.isAbsolute(specifier)) specifier = path.normalize(specifier)
820
-
821
- return specifier
822
- },
823
-
824
696
  postresolve (specifier) {
825
697
  if (path.isAbsolute(specifier)) return binding.realpath(specifier)
826
698
  return specifier
package/lib/protocol.js CHANGED
@@ -1,7 +1,7 @@
1
1
  module.exports = class ModuleProtocol {
2
2
  constructor (opts = {}) {
3
3
  const {
4
- imports = Object.create(null),
4
+ imports = null,
5
5
  preresolve = null,
6
6
  postresolve = null,
7
7
  resolve = null,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "2.5.3",
3
+ "version": "2.6.0",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -26,8 +26,10 @@
26
26
  "homepage": "https://github.com/holepunchto/bare-module#readme",
27
27
  "dependencies": {
28
28
  "bare-bundle": "^0.3.0",
29
+ "bare-module-resolve": "^1.0.0",
29
30
  "bare-os": "^2.0.0",
30
- "bare-path": "^2.0.0"
31
+ "bare-path": "^2.0.0",
32
+ "bare-url": "^0.3.4"
31
33
  },
32
34
  "devDependencies": {
33
35
  "brittle": "^3.1.1",