bare-module 2.2.0 → 2.3.1

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 +46 -42
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -151,17 +151,27 @@ const Module = module.exports = exports = class Module {
151
151
  static _onmeta (specifier, meta) {
152
152
  const module = this._cache[specifier]
153
153
 
154
+ const referrer = module
155
+ const dirname = path.dirname(module._filename)
156
+
154
157
  const resolve = (specifier) => {
155
- return this.resolve(specifier, path.dirname(module._filename), {
158
+ return this.resolve(specifier, dirname, {
156
159
  protocol: this._protocolFor(specifier, module._protocol),
157
160
  imports: module._imports,
158
- referrer: module
161
+ referrer
159
162
  })
160
163
  }
161
164
 
165
+ const addon = (specifier = '.') => {
166
+ return Bare.Addon.load(Bare.Addon.resolve(specifier, dirname, {
167
+ referrer
168
+ }))
169
+ }
170
+
162
171
  meta.url = module._filename
163
172
  meta.main = module._main === module
164
173
  meta.resolve = resolve
174
+ meta.addon = addon
165
175
  }
166
176
 
167
177
  static Protocol = Protocol
@@ -280,17 +290,24 @@ const Module = module.exports = exports = class Module {
280
290
  return this._transform(module, referrer, dynamic)
281
291
  }
282
292
 
283
- static _loadPackageManifest (dirname, protocol) {
293
+ static _loadPackageManifest (dirname, protocol, opts = {}) {
294
+ const {
295
+ traverse = true
296
+ } = opts
297
+
284
298
  do {
285
- const pkg = path.join(dirname, 'package.json')
299
+ const specifier = path.join(dirname, 'package.json')
300
+
301
+ if (this._cache[specifier]) return this._cache[specifier]._exports
286
302
 
287
- if (protocol.exists(pkg)) {
303
+ if (protocol.exists(specifier)) {
288
304
  try {
289
- return this.load(pkg, { protocol })._exports
305
+ return this.load(specifier, { protocol })._exports
290
306
  } catch {}
291
307
  }
292
308
 
293
- dirname = path.dirname(dirname)
309
+ if (traverse) dirname = path.dirname(dirname)
310
+ else break
294
311
  } while (dirname !== path.sep && dirname !== '.')
295
312
 
296
313
  return {}
@@ -378,29 +395,22 @@ const Module = module.exports = exports = class Module {
378
395
  }
379
396
 
380
397
  static * _resolveDirectory (dirname, protocol) {
381
- const pkg = path.join(dirname, 'package.json')
398
+ const info = this._loadPackageManifest(dirname, protocol, { traverse: false })
382
399
 
383
- if (protocol.exists(pkg)) {
384
- let info = null
385
- try {
386
- info = this.load(pkg, { protocol })._exports
387
- } catch {}
400
+ let specifier = null
388
401
 
389
- if (info) {
390
- let specifier
402
+ if (info.exports) {
403
+ specifier = this._mapConditionalSpecifier('.', null, info.exports)
391
404
 
392
- if (info.exports) {
393
- specifier = this._mapConditionalSpecifier('.', null, info.exports)
394
-
395
- if (specifier) specifier = path.join(dirname, specifier)
396
- else return
397
- } else if (info.main) {
398
- specifier = path.join(dirname, info.main)
399
- }
405
+ if (specifier) specifier = path.join(dirname, specifier)
406
+ else return // Unexported
407
+ } else if (info.main) {
408
+ specifier = path.join(dirname, info.main)
409
+ }
400
410
 
401
- yield * this._resolveFile(specifier, protocol)
402
- yield * this._resolveIndex(specifier, protocol)
403
- }
411
+ if (specifier) {
412
+ yield * this._resolveFile(specifier, protocol)
413
+ yield * this._resolveIndex(specifier, protocol)
404
414
  }
405
415
 
406
416
  yield * this._resolveIndex(dirname, protocol)
@@ -413,22 +423,13 @@ const Module = module.exports = exports = class Module {
413
423
  let resolved = specifier
414
424
 
415
425
  if (name) {
416
- const pkg = path.join(nodeModules, name, 'package.json')
417
-
418
- if (protocol.exists(pkg)) {
419
- let info = null
420
- try {
421
- info = this.load(pkg, { protocol })._exports
422
- } catch {}
423
-
424
- if (info) {
425
- if (info.exports) {
426
- resolved = this._mapConditionalSpecifier(expansion, null, info.exports)
427
-
428
- if (resolved) resolved = path.join(name, resolved)
429
- else return
430
- }
431
- }
426
+ const info = this._loadPackageManifest(path.join(nodeModules, name), protocol, { traverse: false })
427
+
428
+ if (info.exports) {
429
+ resolved = this._mapConditionalSpecifier(expansion, null, info.exports)
430
+
431
+ if (resolved) resolved = path.join(name, resolved)
432
+ else return // Unexported
432
433
  }
433
434
  }
434
435
 
@@ -475,6 +476,9 @@ const Module = module.exports = exports = class Module {
475
476
  switch (key) {
476
477
  case 'require':
477
478
  case 'import':
479
+ case 'bare':
480
+ case 'node':
481
+ case 'default':
478
482
  return specifiers[key]
479
483
  }
480
484
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "2.2.0",
3
+ "version": "2.3.1",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [