bare-module 1.9.4 → 1.9.5

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 +67 -25
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -29,6 +29,7 @@ const Module = module.exports = class Module {
29
29
  static _builtins = Object.create(null)
30
30
  static _imports = Object.create(null)
31
31
  static _cache = Object.create(null)
32
+ static _bundles = Object.create(null)
32
33
 
33
34
  static _onimport (specifier, assertions, referrerFilename, dynamic) {
34
35
  const referrer = this._cache[referrerFilename]
@@ -82,7 +83,7 @@ const Module = module.exports = class Module {
82
83
  source = null
83
84
  }
84
85
 
85
- const {
86
+ let {
86
87
  imports = this._imports,
87
88
  protocol = this._protocolFor(specifier, this._protocols['file:']),
88
89
  referrer = null,
@@ -91,6 +92,22 @@ const Module = module.exports = class Module {
91
92
 
92
93
  if (this._cache[specifier]) return this._transform(this._cache[specifier], referrer, dynamic)
93
94
 
95
+ const bundle = this._bundleFor(specifier, protocol, source)
96
+
97
+ if (bundle) {
98
+ imports = { ...imports, ...bundle.imports }
99
+
100
+ protocol = new Protocol({
101
+ exists (filename) {
102
+ return bundle.exists(filename)
103
+ },
104
+
105
+ read (filename) {
106
+ return bundle.read(filename)
107
+ }
108
+ })
109
+ }
110
+
94
111
  const module = this._cache[specifier] = new this(specifier)
95
112
 
96
113
  let dirname = module.dirname
@@ -126,12 +143,28 @@ const Module = module.exports = class Module {
126
143
  dirname = process.cwd()
127
144
  }
128
145
 
129
- const {
146
+ let {
130
147
  imports = this._imports,
131
148
  protocol = this._protocols['file:'],
132
149
  referrer = null
133
150
  } = opts
134
151
 
152
+ const bundle = this._bundleFor(specifier, protocol)
153
+
154
+ if (bundle) {
155
+ imports = { ...imports, ...bundle.imports }
156
+
157
+ protocol = new Protocol({
158
+ exists (filename) {
159
+ return bundle.exists(filename)
160
+ },
161
+
162
+ read (filename) {
163
+ return bundle.read(filename)
164
+ }
165
+ })
166
+ }
167
+
135
168
  const [resolved = null] = this._resolve(specifier, dirname, protocol, imports)
136
169
 
137
170
  if (resolved === null) {
@@ -230,21 +263,44 @@ const Module = module.exports = class Module {
230
263
  }
231
264
 
232
265
  static _protocolFor (specifier, fallback = null) {
266
+ let protocol = fallback
267
+
233
268
  const i = specifier.indexOf(':')
234
269
 
235
- if (i < 2) return fallback // Allow drive letters in Windows paths
270
+ if (i >= 2) { // Allow drive letters in Windows paths
271
+ const name = specifier.slice(0, i + 1)
236
272
 
237
- const protocol = specifier.slice(0, i + 1)
273
+ protocol = this._protocols[name] || fallback
238
274
 
239
- if (!this._protocols[protocol]) {
240
- if (fallback === null) {
241
- throw errors.UNKNOWN_PROTOCOL(`Unknown protocol '${protocol}' in specifier '${specifier}'`)
275
+ if (protocol === null) {
276
+ throw errors.UNKNOWN_PROTOCOL(`Unknown protocol '${name}' in specifier '${specifier}'`)
242
277
  }
243
-
244
- return fallback
245
278
  }
246
279
 
247
- return this._protocols[protocol]
280
+ return protocol
281
+ }
282
+
283
+ static _bundleFor (specifier, protocol, source = null) {
284
+ let name = specifier
285
+ do {
286
+ if (path.extname(name) === '.bundle') {
287
+ break
288
+ }
289
+
290
+ name = path.dirname(name)
291
+ } while (name !== '/' && name !== '.')
292
+
293
+ if (path.extname(name) !== '.bundle') return null
294
+
295
+ let bundle = this._bundles[name]
296
+
297
+ if (bundle) return bundle
298
+
299
+ if (source === null || name !== specifier) source = protocol.read(name)
300
+
301
+ bundle = this._bundles[name] = Bundle.from(source).mount(name)
302
+
303
+ return bundle
248
304
  }
249
305
 
250
306
  static _transform (module, referrer = null, dynamic = false) {
@@ -392,28 +448,14 @@ Module._extensions['.node'] = function (module, source, referrer, protocol, impo
392
448
  }
393
449
 
394
450
  Module._extensions['.bundle'] = function (module, source, referrer, protocol, imports) {
395
- if (source === null) source = protocol.read(module.filename)
396
-
397
451
  if (typeof source === 'string') source = Buffer.from(source)
398
452
 
399
- const bundle = Bundle.from(source).mount(module.filename)
453
+ const bundle = this._bundleFor(module.filename, protocol, source)
400
454
 
401
455
  module._type = 'bundle'
402
456
  module._protocol = protocol
403
457
  module._imports = imports
404
458
 
405
- protocol = new Protocol({
406
- exists (filename) {
407
- return bundle.exists(filename)
408
- },
409
-
410
- read (filename) {
411
- return bundle.read(filename)
412
- }
413
- })
414
-
415
- imports = bundle.imports
416
-
417
459
  module.exports = Module.load(bundle.main, bundle.read(bundle.main), { protocol, imports, referrer }).exports
418
460
  }
419
461
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "1.9.4",
3
+ "version": "1.9.5",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [