bare-module 4.3.2 → 4.5.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/README.md CHANGED
@@ -212,11 +212,14 @@ Options include:
212
212
  {
213
213
  isImport = false,
214
214
  referrer = null,
215
+ type,
216
+ extensions,
215
217
  protocol,
216
218
  imports,
217
219
  resolutions,
218
220
  builtins,
219
- conditions
221
+ conditions,
222
+ attributes
220
223
  }
221
224
  ```
222
225
 
@@ -235,7 +238,8 @@ Options include:
235
238
  imports,
236
239
  resolutions,
237
240
  builtins,
238
- conditions
241
+ conditions,
242
+ attributes
239
243
  }
240
244
  ```
241
245
 
@@ -315,7 +319,8 @@ Methods include:
315
319
  resolve,
316
320
  exists,
317
321
  read,
318
- load
322
+ load,
323
+ asset
319
324
  }
320
325
  ```
321
326
 
package/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /* global Bare */
2
2
  const path = require('bare-path')
3
3
  const resolve = require('bare-module-resolve')
4
- const { fileURLToPath, pathToFileURL } = require('bare-url')
4
+ const { isURL, fileURLToPath, pathToFileURL } = require('bare-url')
5
5
  const Bundle = require('bare-bundle')
6
6
  const { parse } = require('cjs-module-lexer')
7
7
  const Protocol = require('./lib/protocol')
@@ -112,6 +112,10 @@ const Module = module.exports = exports = class Module {
112
112
  Module._modules.delete(this)
113
113
  }
114
114
 
115
+ _run () {
116
+ binding.runModule(this._handle, Module._handle, Module._onrun)
117
+ }
118
+
115
119
  _transform (isImport, isDynamicImport) {
116
120
  if (isDynamicImport) {
117
121
  this._synthesize()
@@ -156,7 +160,11 @@ const Module = module.exports = exports = class Module {
156
160
 
157
161
  const module = Module.load(resolved, { isImport: true, referrer })
158
162
 
159
- queue.push(module)
163
+ if (module._names) {
164
+ for (const name of module._names) names.add(name)
165
+ } else {
166
+ queue.push(module)
167
+ }
160
168
  }
161
169
 
162
170
  break
@@ -179,10 +187,6 @@ const Module = module.exports = exports = class Module {
179
187
  this._handle = binding.createSyntheticModule(this._url.href, this._names, Module._handle)
180
188
  }
181
189
 
182
- _run () {
183
- binding.runModule(this._handle, Module._handle, Module._onrun)
184
- }
185
-
186
190
  _evaluate (eagerRun = false) {
187
191
  if ((this._state & constants.states.EVALUATED) !== 0) return
188
192
 
@@ -204,16 +208,12 @@ const Module = module.exports = exports = class Module {
204
208
  )
205
209
 
206
210
  if (eagerRun) this._run()
207
- }
208
-
209
- if (this._type === constants.types.MODULE) {
211
+ } else if (this._type === constants.types.MODULE) {
210
212
  this._run()
211
213
 
212
214
  this._exports = binding.getNamespace(this._handle)
213
- }
214
-
215
- if (this._type === constants.types.ADDON) {
216
- if (eagerRun) this._run()
215
+ } else if (eagerRun) {
216
+ this._run()
217
217
  }
218
218
  }
219
219
 
@@ -241,7 +241,7 @@ const Module = module.exports = exports = class Module {
241
241
 
242
242
  static _handle = binding.init(this, this._onimport, this._onevaluate, this._onmeta)
243
243
 
244
- static _onimport (href, assertions, referrerHref, isDynamicImport) {
244
+ static _onimport (href, attributes, referrerHref, isDynamicImport) {
245
245
  const referrer = this._cache[referrerHref] || null
246
246
 
247
247
  if (referrer === null) {
@@ -252,28 +252,9 @@ const Module = module.exports = exports = class Module {
252
252
  throw errors.MODULE_NOT_FOUND(msg)
253
253
  }
254
254
 
255
- const url = this.resolve(href, referrer._url, {
256
- isImport: true,
257
- referrer
258
- })
255
+ const url = this.resolve(href, referrer._url, { isImport: true, referrer, attributes })
259
256
 
260
- let type
261
-
262
- switch (assertions.type) {
263
- case 'module':
264
- type = constants.types.MODULE
265
- break
266
- case 'json':
267
- type = constants.types.JSON
268
- break
269
- }
270
-
271
- const module = this.load(url, {
272
- isImport: true,
273
- isDynamicImport,
274
- referrer,
275
- type
276
- })
257
+ const module = this.load(url, { isImport: true, isDynamicImport, referrer, attributes })
277
258
 
278
259
  return module._handle
279
260
  }
@@ -326,8 +307,8 @@ const Module = module.exports = exports = class Module {
326
307
  meta.addon = addon
327
308
  meta.asset = asset
328
309
 
329
- function resolve (specifier) {
330
- const resolved = self.resolve(specifier, referrer._url, { referrer })
310
+ function resolve (specifier, parentURL = referrer._url) {
311
+ const resolved = self.resolve(specifier, toURL(parentURL), { referrer })
331
312
 
332
313
  switch (resolved.protocol) {
333
314
  case 'builtin:': return resolved.pathname
@@ -335,16 +316,16 @@ const Module = module.exports = exports = class Module {
335
316
  }
336
317
  }
337
318
 
338
- function addon (specifier = '.') {
339
- const resolved = Bare.Addon.resolve(specifier, referrer._url, { referrer })
319
+ function addon (specifier = '.', parentURL = referrer._url) {
320
+ const resolved = Bare.Addon.resolve(specifier, toURL(parentURL), { referrer })
340
321
 
341
322
  const addon = Bare.Addon.load(resolved, { referrer })
342
323
 
343
324
  return addon._exports
344
325
  }
345
326
 
346
- function asset (specifier) {
347
- return self.asset(specifier, referrer._url, { referrer }).href
327
+ function asset (specifier, parentURL = referrer._url) {
328
+ return self.asset(specifier, toURL(parentURL), { referrer }).href
348
329
  }
349
330
  }
350
331
 
@@ -379,7 +360,8 @@ const Module = module.exports = exports = class Module {
379
360
  isDynamicImport = false,
380
361
 
381
362
  referrer = null,
382
- type = 0,
363
+ attributes,
364
+ type = typeForAttributes(attributes),
383
365
  defaultType = referrer ? referrer._defaultType : 0,
384
366
  cache = referrer ? referrer._cache : self._cache,
385
367
  main = referrer ? referrer._main : null,
@@ -410,10 +392,10 @@ const Module = module.exports = exports = class Module {
410
392
  module._builtins = builtins
411
393
  module._conditions = conditions
412
394
 
413
- let extension = self._extensionFor(type) || path.extname(url.pathname)
395
+ let extension = canonicalExtensionForType(type) || path.extname(url.pathname)
414
396
 
415
397
  if (extension in self._extensions === false) {
416
- if (defaultType) extension = self._extensionFor(defaultType) || '.js'
398
+ if (defaultType) extension = canonicalExtensionForType(defaultType) || '.js'
417
399
  else extension = '.js'
418
400
  }
419
401
 
@@ -440,6 +422,9 @@ const Module = module.exports = exports = class Module {
440
422
  isImport = false,
441
423
 
442
424
  referrer = null,
425
+ attributes,
426
+ type = typeForAttributes(attributes),
427
+ extensions = extensionsForType(type),
443
428
  protocol = referrer ? referrer._protocol : self._protocol,
444
429
  imports = referrer ? referrer._imports : null,
445
430
  resolutions = referrer ? referrer._resolutions : null,
@@ -457,18 +442,11 @@ const Module = module.exports = exports = class Module {
457
442
  conditions: isImport ? ['import', ...conditions] : ['require', ...conditions],
458
443
  imports,
459
444
  resolutions,
445
+ extensions,
460
446
  builtins: builtins ? Object.keys(builtins) : [],
461
447
  engines: {
462
448
  ...Bare.versions
463
- },
464
- extensions: [
465
- '.js',
466
- '.cjs',
467
- '.mjs',
468
- '.json',
469
- '.bare',
470
- '.node'
471
- ]
449
+ }
472
450
  }, readPackage)) {
473
451
  switch (resolution.protocol) {
474
452
  case 'builtin:': return resolution
@@ -481,7 +459,7 @@ const Module = module.exports = exports = class Module {
481
459
 
482
460
  let msg = `Cannot find module '${specifier}'`
483
461
 
484
- if (referrer) msg += ` imported from '${referrer._url.href}'`
462
+ if (referrer) msg += ` imported from '${parentURL.href}'`
485
463
 
486
464
  throw errors.MODULE_NOT_FOUND(msg)
487
465
 
@@ -521,7 +499,7 @@ const Module = module.exports = exports = class Module {
521
499
 
522
500
  let msg = `Cannot find asset '${specifier}'`
523
501
 
524
- if (referrer) msg += ` imported from '${referrer._url.href}'`
502
+ if (referrer) msg += ` imported from '${parentURL.href}'`
525
503
 
526
504
  throw errors.ASSET_NOT_FOUND(msg)
527
505
 
@@ -533,22 +511,70 @@ const Module = module.exports = exports = class Module {
533
511
  return null
534
512
  }
535
513
  }
514
+ }
536
515
 
537
- static _extensionFor (type) {
538
- switch (type) {
539
- case constants.types.SCRIPT:
540
- return '.cjs'
541
- case constants.types.MODULE:
542
- return '.esm'
543
- case constants.types.JSON:
544
- return '.json'
545
- case constants.types.BUNDLE:
546
- return '.bundle'
547
- case constants.types.ADDON:
548
- return '.bare'
549
- default:
550
- return null
551
- }
516
+ function extensionsForType (type) {
517
+ switch (type) {
518
+ case constants.types.SCRIPT:
519
+ return ['.js', '.cjs']
520
+ case constants.types.MODULE:
521
+ return ['.js', '.mjs']
522
+ case constants.types.JSON:
523
+ return ['.json']
524
+ case constants.types.BUNDLE:
525
+ return ['.json']
526
+ case constants.types.ADDON:
527
+ return ['.bare', '.node']
528
+ case constants.types.BINARY:
529
+ return ['.bin']
530
+ case constants.types.TEXT:
531
+ return ['.txt']
532
+ default:
533
+ return ['.js', '.cjs', '.mjs', '.json', '.bare', '.node']
534
+ }
535
+ }
536
+
537
+ function canonicalExtensionForType (type) {
538
+ switch (type) {
539
+ case constants.types.SCRIPT:
540
+ return '.cjs'
541
+ case constants.types.MODULE:
542
+ return '.esm'
543
+ case constants.types.JSON:
544
+ return '.json'
545
+ case constants.types.BUNDLE:
546
+ return '.bundle'
547
+ case constants.types.ADDON:
548
+ return '.bare'
549
+ case constants.types.BINARY:
550
+ return '.bin'
551
+ case constants.types.TEXT:
552
+ return '.txt'
553
+ default:
554
+ return null
555
+ }
556
+ }
557
+
558
+ function typeForAttributes (attributes) {
559
+ if (typeof attributes !== 'object' || attributes === null) return 0
560
+
561
+ switch (attributes.type) {
562
+ case 'script':
563
+ return constants.types.SCRIPT
564
+ case 'module':
565
+ return constants.types.MODULE
566
+ case 'json':
567
+ return constants.types.JSON
568
+ case 'bundle':
569
+ return constants.types.BUNDLE
570
+ case 'addon':
571
+ return constants.types.ADDON
572
+ case 'binary':
573
+ return constants.types.BINARY
574
+ case 'text':
575
+ return constants.types.TEXT
576
+ default:
577
+ return 0
552
578
  }
553
579
  }
554
580
 
@@ -568,14 +594,6 @@ exports.isBuiltin = function isBuiltin () {
568
594
  const createRequire = exports.createRequire = function createRequire (parentURL, opts = {}) {
569
595
  const self = Module
570
596
 
571
- if (typeof parentURL === 'string') {
572
- if (startsWithWindowsDriveLetter(parentURL)) {
573
- parentURL = '/' + parentURL
574
- }
575
-
576
- parentURL = new URL(parentURL, 'file:')
577
- }
578
-
579
597
  let {
580
598
  module = null,
581
599
 
@@ -592,7 +610,7 @@ const createRequire = exports.createRequire = function createRequire (parentURL,
592
610
  } = opts
593
611
 
594
612
  if (module === null) {
595
- module = new Module(parentURL)
613
+ module = new Module(toURL(parentURL))
596
614
 
597
615
  module._type = type
598
616
  module._defaultType = defaultType
@@ -617,16 +635,18 @@ const createRequire = exports.createRequire = function createRequire (parentURL,
617
635
 
618
636
  return require
619
637
 
620
- function require (specifier) {
621
- const resolved = self.resolve(specifier, referrer._url, { referrer })
638
+ function require (specifier, opts = {}) {
639
+ const attributes = opts && opts.with
622
640
 
623
- const module = self.load(resolved, { referrer })
641
+ const resolved = self.resolve(specifier, referrer._url, { referrer, attributes })
642
+
643
+ const module = self.load(resolved, { referrer, attributes })
624
644
 
625
645
  return module._exports
626
646
  }
627
647
 
628
- function resolve (specifier) {
629
- const resolved = self.resolve(specifier, referrer._url, { referrer })
648
+ function resolve (specifier, parentURL = referrer._url) {
649
+ const resolved = self.resolve(specifier, toURL(parentURL), { referrer })
630
650
 
631
651
  switch (resolved.protocol) {
632
652
  case 'builtin:': return resolved.pathname
@@ -634,16 +654,16 @@ const createRequire = exports.createRequire = function createRequire (parentURL,
634
654
  }
635
655
  }
636
656
 
637
- function addon (specifier = '.') {
638
- const resolved = Bare.Addon.resolve(specifier, referrer._url, { referrer })
657
+ function addon (specifier = '.', parentURL = referrer._url) {
658
+ const resolved = Bare.Addon.resolve(specifier, toURL(parentURL), { referrer })
639
659
 
640
660
  const addon = Bare.Addon.load(resolved, { referrer })
641
661
 
642
662
  return addon._exports
643
663
  }
644
664
 
645
- function asset (specifier) {
646
- return urlToPath(self.asset(specifier, referrer._url, { referrer }))
665
+ function asset (specifier, parentURL = referrer._url) {
666
+ return urlToPath(self.asset(specifier, toURL(parentURL), { referrer }))
647
667
  }
648
668
  }
649
669
 
@@ -785,6 +805,30 @@ Module._extensions['.bundle'] = function (module, source, referrer) {
785
805
  }
786
806
  }
787
807
 
808
+ Module._extensions['.bin'] = function (module, source, referrer) {
809
+ const protocol = module._protocol
810
+
811
+ module._type = constants.types.BINARY
812
+
813
+ if (source === null) source = protocol.read(module._url)
814
+
815
+ if (typeof source === 'string') source = Buffer.from(source)
816
+
817
+ module._exports = source
818
+ }
819
+
820
+ Module._extensions['.txt'] = function (module, source, referrer) {
821
+ const protocol = module._protocol
822
+
823
+ module._type = constants.types.TEXT
824
+
825
+ if (source === null) source = protocol.read(module._url)
826
+
827
+ if (typeof source !== 'string') source = Buffer.coerce(source).toString()
828
+
829
+ module._exports = source
830
+ }
831
+
788
832
  Module._protocol = new Protocol({
789
833
  postresolve (url) {
790
834
  switch (url.protocol) {
@@ -823,6 +867,16 @@ Bare
823
867
  binding.destroy(Module._handle)
824
868
  })
825
869
 
870
+ function toURL (value) {
871
+ if (isURL(value)) return value
872
+
873
+ if (startsWithWindowsDriveLetter(value)) {
874
+ return pathToFileURL(value)
875
+ }
876
+
877
+ return URL.parse(value) || pathToFileURL(value)
878
+ }
879
+
826
880
  function urlToPath (url) {
827
881
  if (url.protocol === 'file:') return fileURLToPath(url)
828
882
 
package/lib/constants.js CHANGED
@@ -10,6 +10,8 @@ module.exports = {
10
10
  MODULE: 2,
11
11
  JSON: 3,
12
12
  BUNDLE: 4,
13
- ADDON: 5
13
+ ADDON: 5,
14
+ BINARY: 6,
15
+ TEXT: 7
14
16
  }
15
17
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "4.3.2",
3
+ "version": "4.5.0",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [