bare-module 2.0.2 → 2.2.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 +57 -38
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -51,6 +51,10 @@ const Module = module.exports = exports = class Module {
|
|
|
51
51
|
this._exports = value
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
+
get protocol () {
|
|
55
|
+
return this._protocol
|
|
56
|
+
}
|
|
57
|
+
|
|
54
58
|
// For Node.js compatibility
|
|
55
59
|
get id () {
|
|
56
60
|
return this.filename
|
|
@@ -87,7 +91,6 @@ const Module = module.exports = exports = class Module {
|
|
|
87
91
|
|
|
88
92
|
static _extensions = Object.create(null)
|
|
89
93
|
static _protocols = Object.create(null)
|
|
90
|
-
static _imports = Object.create(null)
|
|
91
94
|
static _cache = Object.create(null)
|
|
92
95
|
static _bundles = Object.create(null)
|
|
93
96
|
static _modules = new Set()
|
|
@@ -171,7 +174,7 @@ const Module = module.exports = exports = class Module {
|
|
|
171
174
|
|
|
172
175
|
static createRequire (filename, opts = {}) {
|
|
173
176
|
const {
|
|
174
|
-
imports =
|
|
177
|
+
imports = null,
|
|
175
178
|
protocol = this._protocolFor(filename, this._protocols['file:']),
|
|
176
179
|
type = constants.types.SCRIPT,
|
|
177
180
|
defaultType = constants.types.SCRIPT
|
|
@@ -226,7 +229,7 @@ const Module = module.exports = exports = class Module {
|
|
|
226
229
|
}
|
|
227
230
|
|
|
228
231
|
let {
|
|
229
|
-
imports =
|
|
232
|
+
imports = null,
|
|
230
233
|
protocol = this._protocolFor(specifier, this._protocols['file:']),
|
|
231
234
|
referrer = null,
|
|
232
235
|
dynamic = false,
|
|
@@ -258,22 +261,8 @@ const Module = module.exports = exports = class Module {
|
|
|
258
261
|
const module = this._cache[specifier] = new this(specifier)
|
|
259
262
|
|
|
260
263
|
module._defaultType = defaultType
|
|
261
|
-
|
|
262
|
-
let dirname = path.dirname(module._filename)
|
|
263
|
-
do {
|
|
264
|
-
const pkg = path.join(dirname, 'package.json')
|
|
265
|
-
|
|
266
|
-
if (protocol.exists(pkg)) {
|
|
267
|
-
try {
|
|
268
|
-
module._info = this.load(pkg, { protocol })._exports
|
|
269
|
-
} catch {}
|
|
270
|
-
break
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
dirname = path.dirname(dirname)
|
|
274
|
-
} while (dirname !== path.sep && dirname !== '.')
|
|
275
|
-
|
|
276
264
|
module._main = main || module
|
|
265
|
+
module._info = this._loadPackageManifest(path.dirname(module._filename), protocol)
|
|
277
266
|
|
|
278
267
|
let extension = this._extensionFor(type) || path.extname(specifier)
|
|
279
268
|
|
|
@@ -291,6 +280,22 @@ const Module = module.exports = exports = class Module {
|
|
|
291
280
|
return this._transform(module, referrer, dynamic)
|
|
292
281
|
}
|
|
293
282
|
|
|
283
|
+
static _loadPackageManifest (dirname, protocol) {
|
|
284
|
+
do {
|
|
285
|
+
const pkg = path.join(dirname, 'package.json')
|
|
286
|
+
|
|
287
|
+
if (protocol.exists(pkg)) {
|
|
288
|
+
try {
|
|
289
|
+
return this.load(pkg, { protocol })._exports
|
|
290
|
+
} catch {}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
dirname = path.dirname(dirname)
|
|
294
|
+
} while (dirname !== path.sep && dirname !== '.')
|
|
295
|
+
|
|
296
|
+
return {}
|
|
297
|
+
}
|
|
298
|
+
|
|
294
299
|
static resolve (specifier, dirname = os.cwd(), opts = {}) {
|
|
295
300
|
if (typeof dirname !== 'string') {
|
|
296
301
|
opts = dirname
|
|
@@ -298,7 +303,7 @@ const Module = module.exports = exports = class Module {
|
|
|
298
303
|
}
|
|
299
304
|
|
|
300
305
|
let {
|
|
301
|
-
imports =
|
|
306
|
+
imports = null,
|
|
302
307
|
protocol = this._protocols['file:'],
|
|
303
308
|
referrer = null
|
|
304
309
|
} = opts
|
|
@@ -335,8 +340,9 @@ const Module = module.exports = exports = class Module {
|
|
|
335
340
|
}
|
|
336
341
|
|
|
337
342
|
static * _resolve (specifier, dirname, protocol, imports) {
|
|
338
|
-
|
|
339
|
-
|
|
343
|
+
const info = this._loadPackageManifest(dirname, protocol)
|
|
344
|
+
|
|
345
|
+
specifier = this._mapConditionalSpecifier(specifier, specifier, imports, protocol.imports, info.imports)
|
|
340
346
|
|
|
341
347
|
protocol = this._protocolFor(specifier, protocol)
|
|
342
348
|
|
|
@@ -384,7 +390,7 @@ const Module = module.exports = exports = class Module {
|
|
|
384
390
|
let specifier
|
|
385
391
|
|
|
386
392
|
if (info.exports) {
|
|
387
|
-
specifier = this.
|
|
393
|
+
specifier = this._mapConditionalSpecifier('.', null, info.exports)
|
|
388
394
|
|
|
389
395
|
if (specifier) specifier = path.join(dirname, specifier)
|
|
390
396
|
else return
|
|
@@ -417,7 +423,7 @@ const Module = module.exports = exports = class Module {
|
|
|
417
423
|
|
|
418
424
|
if (info) {
|
|
419
425
|
if (info.exports) {
|
|
420
|
-
resolved = this.
|
|
426
|
+
resolved = this._mapConditionalSpecifier(expansion, null, info.exports)
|
|
421
427
|
|
|
422
428
|
if (resolved) resolved = path.join(name, resolved)
|
|
423
429
|
else return
|
|
@@ -445,33 +451,31 @@ const Module = module.exports = exports = class Module {
|
|
|
445
451
|
}
|
|
446
452
|
}
|
|
447
453
|
|
|
448
|
-
static
|
|
449
|
-
|
|
454
|
+
static _mapConditionalSpecifier (specifier, fallback, ...specifierMaps) {
|
|
455
|
+
const specifiers = this._flattenSpecifierMaps(specifierMaps)
|
|
450
456
|
|
|
451
|
-
if (specifier in
|
|
452
|
-
specifier = search(
|
|
457
|
+
if (specifier in specifiers) {
|
|
458
|
+
specifier = search(specifiers[specifier])
|
|
453
459
|
} else {
|
|
454
|
-
specifier = search(
|
|
460
|
+
specifier = search(specifiers)
|
|
455
461
|
}
|
|
456
462
|
|
|
457
|
-
|
|
463
|
+
return specifier || fallback
|
|
458
464
|
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
function search (specifier) {
|
|
465
|
+
function search (specifiers) {
|
|
462
466
|
while (true) {
|
|
463
|
-
if (typeof
|
|
464
|
-
if (
|
|
465
|
-
|
|
467
|
+
if (typeof specifiers === 'string') return specifiers
|
|
468
|
+
if (specifiers === null || typeof specifiers !== 'object') return specifiers
|
|
469
|
+
specifiers = first(specifiers)
|
|
466
470
|
}
|
|
467
471
|
}
|
|
468
472
|
|
|
469
|
-
function first (
|
|
470
|
-
for (const key in
|
|
473
|
+
function first (specifiers) {
|
|
474
|
+
for (const key in specifiers) {
|
|
471
475
|
switch (key) {
|
|
472
476
|
case 'require':
|
|
473
477
|
case 'import':
|
|
474
|
-
return
|
|
478
|
+
return specifiers[key]
|
|
475
479
|
}
|
|
476
480
|
}
|
|
477
481
|
|
|
@@ -479,6 +483,21 @@ const Module = module.exports = exports = class Module {
|
|
|
479
483
|
}
|
|
480
484
|
}
|
|
481
485
|
|
|
486
|
+
static _flattenSpecifierMaps (specifierMaps) {
|
|
487
|
+
const specifiers = Object.create(null)
|
|
488
|
+
|
|
489
|
+
for (const map of specifierMaps) {
|
|
490
|
+
this._mergeSpecifierMaps(typeof map === 'object' ? map : { '.': map }, specifiers)
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
return specifiers
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
static _mergeSpecifierMaps (source, destination) {
|
|
497
|
+
// TODO Do a deep merge
|
|
498
|
+
Object.assign(destination, source)
|
|
499
|
+
}
|
|
500
|
+
|
|
482
501
|
static _extensionFor (type) {
|
|
483
502
|
switch (type) {
|
|
484
503
|
case constants.types.SCRIPT:
|