bare-module 2.0.2 → 2.1.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 (2) hide show
  1. package/index.js +53 -38
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -87,7 +87,6 @@ const Module = module.exports = exports = class Module {
87
87
 
88
88
  static _extensions = Object.create(null)
89
89
  static _protocols = Object.create(null)
90
- static _imports = Object.create(null)
91
90
  static _cache = Object.create(null)
92
91
  static _bundles = Object.create(null)
93
92
  static _modules = new Set()
@@ -171,7 +170,7 @@ const Module = module.exports = exports = class Module {
171
170
 
172
171
  static createRequire (filename, opts = {}) {
173
172
  const {
174
- imports = this._imports,
173
+ imports = null,
175
174
  protocol = this._protocolFor(filename, this._protocols['file:']),
176
175
  type = constants.types.SCRIPT,
177
176
  defaultType = constants.types.SCRIPT
@@ -226,7 +225,7 @@ const Module = module.exports = exports = class Module {
226
225
  }
227
226
 
228
227
  let {
229
- imports = this._imports,
228
+ imports = null,
230
229
  protocol = this._protocolFor(specifier, this._protocols['file:']),
231
230
  referrer = null,
232
231
  dynamic = false,
@@ -258,22 +257,8 @@ const Module = module.exports = exports = class Module {
258
257
  const module = this._cache[specifier] = new this(specifier)
259
258
 
260
259
  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
260
  module._main = main || module
261
+ module._info = this._loadPackageManifest(path.dirname(module._filename), protocol)
277
262
 
278
263
  let extension = this._extensionFor(type) || path.extname(specifier)
279
264
 
@@ -291,6 +276,22 @@ const Module = module.exports = exports = class Module {
291
276
  return this._transform(module, referrer, dynamic)
292
277
  }
293
278
 
279
+ static _loadPackageManifest (dirname, protocol) {
280
+ do {
281
+ const pkg = path.join(dirname, 'package.json')
282
+
283
+ if (protocol.exists(pkg)) {
284
+ try {
285
+ return this.load(pkg, { protocol })._exports
286
+ } catch {}
287
+ }
288
+
289
+ dirname = path.dirname(dirname)
290
+ } while (dirname !== path.sep && dirname !== '.')
291
+
292
+ return {}
293
+ }
294
+
294
295
  static resolve (specifier, dirname = os.cwd(), opts = {}) {
295
296
  if (typeof dirname !== 'string') {
296
297
  opts = dirname
@@ -298,7 +299,7 @@ const Module = module.exports = exports = class Module {
298
299
  }
299
300
 
300
301
  let {
301
- imports = this._imports,
302
+ imports = null,
302
303
  protocol = this._protocols['file:'],
303
304
  referrer = null
304
305
  } = opts
@@ -335,8 +336,9 @@ const Module = module.exports = exports = class Module {
335
336
  }
336
337
 
337
338
  static * _resolve (specifier, dirname, protocol, imports) {
338
- if (specifier in imports) specifier = imports[specifier]
339
- else if (specifier in protocol.imports) specifier = protocol.imports[specifier]
339
+ const info = this._loadPackageManifest(dirname, protocol)
340
+
341
+ specifier = this._mapConditionalSpecifier(specifier, specifier, imports, protocol.imports, info.imports)
340
342
 
341
343
  protocol = this._protocolFor(specifier, protocol)
342
344
 
@@ -384,7 +386,7 @@ const Module = module.exports = exports = class Module {
384
386
  let specifier
385
387
 
386
388
  if (info.exports) {
387
- specifier = this._mapConditionalExport('.', dirname, info.exports)
389
+ specifier = this._mapConditionalSpecifier('.', null, info.exports)
388
390
 
389
391
  if (specifier) specifier = path.join(dirname, specifier)
390
392
  else return
@@ -417,7 +419,7 @@ const Module = module.exports = exports = class Module {
417
419
 
418
420
  if (info) {
419
421
  if (info.exports) {
420
- resolved = this._mapConditionalExport(expansion, dirname, info.exports)
422
+ resolved = this._mapConditionalSpecifier(expansion, null, info.exports)
421
423
 
422
424
  if (resolved) resolved = path.join(name, resolved)
423
425
  else return
@@ -445,33 +447,31 @@ const Module = module.exports = exports = class Module {
445
447
  }
446
448
  }
447
449
 
448
- static _mapConditionalExport (specifier, dirname, exports) {
449
- if (typeof exports !== 'object') exports = { '.': exports }
450
+ static _mapConditionalSpecifier (specifier, fallback, ...specifierMaps) {
451
+ const specifiers = this._flattenSpecifierMaps(specifierMaps)
450
452
 
451
- if (specifier in exports) {
452
- specifier = search(exports[specifier])
453
+ if (specifier in specifiers) {
454
+ specifier = search(specifiers[specifier])
453
455
  } else {
454
- specifier = search(exports)
456
+ specifier = search(specifiers)
455
457
  }
456
458
 
457
- if (specifier) specifier = path.join(dirname, specifier)
459
+ return specifier || fallback
458
460
 
459
- return specifier
460
-
461
- function search (specifier) {
461
+ function search (specifiers) {
462
462
  while (true) {
463
- if (typeof specifier === 'string') return specifier
464
- if (specifier === null || typeof specifier !== 'object') return specifier
465
- specifier = first(specifier)
463
+ if (typeof specifiers === 'string') return specifiers
464
+ if (specifiers === null || typeof specifiers !== 'object') return specifiers
465
+ specifiers = first(specifiers)
466
466
  }
467
467
  }
468
468
 
469
- function first (exports) {
470
- for (const key in exports) {
469
+ function first (specifiers) {
470
+ for (const key in specifiers) {
471
471
  switch (key) {
472
472
  case 'require':
473
473
  case 'import':
474
- return exports[key]
474
+ return specifiers[key]
475
475
  }
476
476
  }
477
477
 
@@ -479,6 +479,21 @@ const Module = module.exports = exports = class Module {
479
479
  }
480
480
  }
481
481
 
482
+ static _flattenSpecifierMaps (specifierMaps) {
483
+ const specifiers = Object.create(null)
484
+
485
+ for (const map of specifierMaps) {
486
+ this._mergeSpecifierMaps(typeof map === 'object' ? map : { '.': map }, specifiers)
487
+ }
488
+
489
+ return specifiers
490
+ }
491
+
492
+ static _mergeSpecifierMaps (source, destination) {
493
+ // TODO Do a deep merge
494
+ Object.assign(destination, source)
495
+ }
496
+
482
497
  static _extensionFor (type) {
483
498
  switch (type) {
484
499
  case constants.types.SCRIPT:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "2.0.2",
3
+ "version": "2.1.0",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [