bare-module 1.10.2 → 1.11.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 +20 -4
  2. package/package.json +1 -1
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
@@ -22,6 +23,10 @@ const Module = module.exports = class Module {
22
23
  return path.dirname(this.filename)
23
24
  }
24
25
 
26
+ get importMap () {
27
+ return this._imports
28
+ }
29
+
25
30
  static _context = binding.init(this, this._onimport, this._onevaluate, this._onmeta)
26
31
 
27
32
  static _extensions = Object.create(null)
@@ -82,12 +87,21 @@ const Module = module.exports = class Module {
82
87
  }
83
88
 
84
89
  meta.url = module.filename
90
+ meta.main = module.main === module
85
91
  meta.resolve = resolve
86
92
  }
87
93
 
88
94
  static Protocol = Protocol
89
95
  static Bundle = Bundle
90
96
 
97
+ static get cache () {
98
+ return this._cache
99
+ }
100
+
101
+ static get builtinModules () {
102
+ return Object.keys(this._builtins)
103
+ }
104
+
91
105
  static isBuiltin (name) {
92
106
  return name in this._builtins
93
107
  }
@@ -102,7 +116,8 @@ const Module = module.exports = class Module {
102
116
  imports = this._imports,
103
117
  protocol = this._protocolFor(specifier, this._protocols['file:']),
104
118
  referrer = null,
105
- dynamic = false
119
+ dynamic = false,
120
+ main = referrer ? referrer.main : null
106
121
  } = opts
107
122
 
108
123
  if (this._cache[specifier]) return this._transform(this._cache[specifier], referrer, dynamic)
@@ -123,7 +138,7 @@ const Module = module.exports = class Module {
123
138
  })
124
139
  }
125
140
 
126
- const module = this._cache[specifier] = new this(specifier)
141
+ const module = this._cache[specifier] = new this(specifier, main)
127
142
 
128
143
  let dirname = module.dirname
129
144
  do {
@@ -427,7 +442,8 @@ Module._extensions['.cjs'] = function (module, source, referrer, protocol, impor
427
442
 
428
443
  module.exports = {}
429
444
 
430
- require.cache = this._cache
445
+ require.main = module.main
446
+ require.cache = this.cache
431
447
  require.resolve = resolve
432
448
 
433
449
  binding.createFunction(module.filename, ['require', 'module', 'exports', '__filename', '__dirname'], source, 0)(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "1.10.2",
3
+ "version": "1.11.0",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [