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