bare-module 2.5.4 → 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.
- package/index.js +56 -185
- package/lib/protocol.js +1 -1
- 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,186 +359,55 @@ const Module = module.exports = exports = class Module {
|
|
|
362
359
|
})
|
|
363
360
|
}
|
|
364
361
|
|
|
365
|
-
const
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
else resolved = specifier
|
|
413
|
-
}
|
|
366
|
+
if (resolution) return protocol.postresolve(resolution, dirname)
|
|
414
367
|
|
|
415
368
|
if (protocol.imports) {
|
|
416
|
-
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
'.mjs',
|
|
444
|
-
'.json',
|
|
445
|
-
'.bare',
|
|
446
|
-
'.node'
|
|
447
|
-
]
|
|
448
|
-
|
|
449
|
-
for (const candidate of [filename, ...extensions.map(ext => filename + ext)]) {
|
|
450
|
-
if (protocol.exists(candidate)) yield candidate
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
static * _resolveIndex (dirname, protocol) {
|
|
455
|
-
yield * this._resolveFile(path.join(dirname, 'index'), protocol)
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
static * _resolveDirectory (dirname, protocol, conditions) {
|
|
459
|
-
const pkg = this._loadPackageManifest(dirname, protocol, { traverse: false })
|
|
460
|
-
|
|
461
|
-
const info = (pkg && pkg._exports) || {}
|
|
462
|
-
|
|
463
|
-
let resolved = null
|
|
464
|
-
|
|
465
|
-
if (info.exports) {
|
|
466
|
-
resolved = this._mapConditionalSpecifier('.', conditions, info.exports)
|
|
467
|
-
|
|
468
|
-
if (resolved) resolved = path.join(dirname, resolved)
|
|
469
|
-
else return // Unexported
|
|
470
|
-
} else if (info.main) {
|
|
471
|
-
resolved = path.join(dirname, info.main)
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
if (resolved) {
|
|
475
|
-
yield * this._resolveFile(resolved, protocol)
|
|
476
|
-
yield * this._resolveIndex(resolved, protocol)
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
yield * this._resolveIndex(dirname, protocol)
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
static * _resolveNodeModules (specifier, dirname, protocol, conditions) {
|
|
483
|
-
const [, name, expansion = '.'] = /^((?:@[^/\\%]+\/)?[^./\\%][^/\\%]*)(\/.*)?$/.exec(specifier) || []
|
|
484
|
-
|
|
485
|
-
for (const nodeModules of this._resolveNodeModulesPaths(dirname)) {
|
|
486
|
-
let resolved = specifier
|
|
487
|
-
|
|
488
|
-
if (name) {
|
|
489
|
-
const pkg = this._loadPackageManifest(path.join(nodeModules, name), protocol, { traverse: false })
|
|
490
|
-
|
|
491
|
-
const info = (pkg && pkg._exports) || {}
|
|
492
|
-
|
|
493
|
-
if (info.exports) {
|
|
494
|
-
resolved = this._mapConditionalSpecifier(expansion, conditions, info.exports)
|
|
495
|
-
|
|
496
|
-
if (resolved) resolved = path.join(name, resolved)
|
|
497
|
-
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
|
+
}
|
|
498
396
|
}
|
|
499
397
|
}
|
|
500
|
-
|
|
501
|
-
const filename = path.join(nodeModules, resolved)
|
|
502
|
-
|
|
503
|
-
yield * this._resolveFile(filename, protocol)
|
|
504
|
-
yield * this._resolveDirectory(filename, protocol, conditions)
|
|
505
398
|
}
|
|
506
|
-
}
|
|
507
399
|
|
|
508
|
-
|
|
509
|
-
if (start === path.sep) return yield path.join(start, 'node_modules')
|
|
400
|
+
let msg = `Cannot find module '${specifier}'`
|
|
510
401
|
|
|
511
|
-
|
|
402
|
+
if (referrer) msg += ` imported from '${referrer._filename}'`
|
|
512
403
|
|
|
513
|
-
|
|
514
|
-
if (parts[i] !== 'node_modules') {
|
|
515
|
-
yield path.join(parts.slice(0, i + 1).join(path.sep), 'node_modules')
|
|
516
|
-
}
|
|
517
|
-
}
|
|
518
|
-
}
|
|
404
|
+
throw errors.MODULE_NOT_FOUND(msg)
|
|
519
405
|
|
|
520
|
-
|
|
521
|
-
|
|
406
|
+
function readPackage (packageURL) {
|
|
407
|
+
const path = url.fileURLToPath(packageURL)
|
|
522
408
|
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
} else {
|
|
526
|
-
specifier = search(specifierMap)
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
return specifier
|
|
530
|
-
|
|
531
|
-
function search (specifiers) {
|
|
532
|
-
while (true) {
|
|
533
|
-
if (typeof specifiers === 'string') return specifiers
|
|
534
|
-
if (specifiers === null || typeof specifiers !== 'object') return specifiers
|
|
535
|
-
|
|
536
|
-
specifiers = first(specifiers)
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
function first (specifiers) {
|
|
541
|
-
for (const key in specifiers) {
|
|
542
|
-
if (key === 'default' || conditions.includes(key)) {
|
|
543
|
-
return specifiers[key]
|
|
544
|
-
}
|
|
409
|
+
if (protocol.exists(path)) {
|
|
410
|
+
return Module.load(path, { protocol })._exports
|
|
545
411
|
}
|
|
546
412
|
|
|
547
413
|
return null
|
|
@@ -605,8 +471,6 @@ const Module = module.exports = exports = class Module {
|
|
|
605
471
|
protocol = new Protocol({
|
|
606
472
|
imports: parent.imports,
|
|
607
473
|
|
|
608
|
-
preresolve: this._protocols['file:'].preresolve,
|
|
609
|
-
|
|
610
474
|
exists (filename) {
|
|
611
475
|
return parent.exists(filename)
|
|
612
476
|
},
|
|
@@ -675,7 +539,23 @@ Module._extensions['.js'] = function (module, source, referrer) {
|
|
|
675
539
|
|
|
676
540
|
const protocol = module._protocol
|
|
677
541
|
|
|
678
|
-
|
|
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 !== '.')
|
|
679
559
|
|
|
680
560
|
const info = (pkg && pkg._exports) || {}
|
|
681
561
|
|
|
@@ -813,15 +693,6 @@ Module._extensions['.bundle'] = function (module, source, referrer) {
|
|
|
813
693
|
}
|
|
814
694
|
|
|
815
695
|
Module._protocols['file:'] = new Protocol({
|
|
816
|
-
preresolve (specifier, dirname) {
|
|
817
|
-
specifier = specifier.replace(/^file:/, '')
|
|
818
|
-
|
|
819
|
-
if (specifier === '.' || specifier.startsWith('./') || specifier.startsWith('.\\')) specifier = path.join(dirname, specifier)
|
|
820
|
-
else if (path.isAbsolute(specifier)) specifier = path.normalize(specifier)
|
|
821
|
-
|
|
822
|
-
return specifier
|
|
823
|
-
},
|
|
824
|
-
|
|
825
696
|
postresolve (specifier) {
|
|
826
697
|
if (path.isAbsolute(specifier)) return binding.realpath(specifier)
|
|
827
698
|
return specifier
|
package/lib/protocol.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bare-module",
|
|
3
|
-
"version": "2.
|
|
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",
|