bare-module 1.10.1 → 1.10.3

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
@@ -6,8 +6,9 @@ const errors = require('./lib/errors')
6
6
  const binding = require('./binding')
7
7
 
8
8
  const Module = module.exports = class Module {
9
- constructor (filename) {
9
+ constructor (filename, main) {
10
10
  this.filename = filename
11
+ this.main = main || this
11
12
  this.exports = null
12
13
 
13
14
  this._type = null
@@ -82,12 +83,21 @@ const Module = module.exports = class Module {
82
83
  }
83
84
 
84
85
  meta.url = module.filename
86
+ meta.main = module.main === module
85
87
  meta.resolve = resolve
86
88
  }
87
89
 
88
90
  static Protocol = Protocol
89
91
  static Bundle = Bundle
90
92
 
93
+ static get cache () {
94
+ return this._cache
95
+ }
96
+
97
+ static get builtinModules () {
98
+ return Object.keys(this._builtins)
99
+ }
100
+
91
101
  static isBuiltin (name) {
92
102
  return name in this._builtins
93
103
  }
@@ -102,7 +112,8 @@ const Module = module.exports = class Module {
102
112
  imports = this._imports,
103
113
  protocol = this._protocolFor(specifier, this._protocols['file:']),
104
114
  referrer = null,
105
- dynamic = false
115
+ dynamic = false,
116
+ main = referrer ? referrer.main : null
106
117
  } = opts
107
118
 
108
119
  if (this._cache[specifier]) return this._transform(this._cache[specifier], referrer, dynamic)
@@ -123,7 +134,7 @@ const Module = module.exports = class Module {
123
134
  })
124
135
  }
125
136
 
126
- const module = this._cache[specifier] = new this(specifier)
137
+ const module = this._cache[specifier] = new this(specifier, main)
127
138
 
128
139
  let dirname = module.dirname
129
140
  do {
@@ -187,7 +198,7 @@ const Module = module.exports = class Module {
187
198
 
188
199
  if (referrer) msg += ` imported from '${referrer.filename}'`
189
200
 
190
- throw errors.NOT_FOUND(msg)
201
+ throw errors.MODULE_NOT_FOUND(msg)
191
202
  }
192
203
 
193
204
  return resolved
@@ -427,7 +438,8 @@ Module._extensions['.cjs'] = function (module, source, referrer, protocol, impor
427
438
 
428
439
  module.exports = {}
429
440
 
430
- require.cache = this._cache
441
+ require.main = module.main
442
+ require.cache = this.cache
431
443
  require.resolve = resolve
432
444
 
433
445
  binding.createFunction(module.filename, ['require', 'module', 'exports', '__filename', '__dirname'], source, 0)(
package/lib/errors.js CHANGED
@@ -12,8 +12,8 @@ module.exports = class ModuleError extends Error {
12
12
  return 'ModuleError'
13
13
  }
14
14
 
15
- static NOT_FOUND (msg) {
16
- return new ModuleError(msg, 'NOT_FOUND', ModuleError.NOT_FOUND)
15
+ static MODULE_NOT_FOUND (msg) {
16
+ return new ModuleError(msg, 'MODULE_NOT_FOUND', ModuleError.MODULE_NOT_FOUND)
17
17
  }
18
18
 
19
19
  static UNKNOWN_PROTOCOL (msg) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "1.10.1",
3
+ "version": "1.10.3",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [