bare-module 1.14.3 → 1.14.4
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 +4 -1
- package/index.js +74 -13
- package/lib/protocol.js +3 -3
- package/package.json +1 -1
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)
|
|
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.
|
|
59
|
+
// For Node.js compatibility
|
|
60
60
|
get path () {
|
|
61
61
|
return this.dirname
|
|
62
62
|
}
|
|
@@ -343,9 +343,7 @@ module.exports = exports = class Module {
|
|
|
343
343
|
|
|
344
344
|
specifier = protocol.preresolve(specifier, dirname)
|
|
345
345
|
|
|
346
|
-
|
|
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
|
-
|
|
392
|
-
|
|
388
|
+
} catch {}
|
|
389
|
+
|
|
390
|
+
if (info) {
|
|
391
|
+
let specifier
|
|
393
392
|
|
|
394
|
-
|
|
395
|
-
|
|
393
|
+
if (info.exports) {
|
|
394
|
+
specifier = this._mapConditionalExport('.', dirname, info.exports)
|
|
396
395
|
|
|
397
|
-
|
|
398
|
-
|
|
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:
|
|
@@ -650,7 +711,7 @@ exports._extensions['.bundle'] = function (module, source, referrer, protocol, i
|
|
|
650
711
|
|
|
651
712
|
exports._protocols['file:'] = new Protocol({
|
|
652
713
|
preresolve (specifier) {
|
|
653
|
-
return specifier.replace(/^file:/, '')
|
|
714
|
+
return path.normalize(specifier.replace(/^file:/, ''))
|
|
654
715
|
},
|
|
655
716
|
|
|
656
717
|
postresolve (specifier) {
|
package/lib/protocol.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
module.exports = class
|
|
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
|
}
|