bare-module 4.5.2 → 4.6.1

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
@@ -199,6 +199,8 @@ Constant | Description
199
199
  `JSON` |
200
200
  `BUNDLE` |
201
201
  `ADDON` |
202
+ `BINARY` |
203
+ `TEXT` |
202
204
 
203
205
  #### `Module.protocol`
204
206
 
@@ -320,6 +322,7 @@ Methods include:
320
322
  exists,
321
323
  read,
322
324
  load,
325
+ addon,
323
326
  asset
324
327
  }
325
328
  ```
package/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  /* global Bare */
2
2
  const path = require('bare-path')
3
3
  const resolve = require('bare-module-resolve')
4
+ const lex = require('bare-module-lexer')
4
5
  const { isURL, fileURLToPath, pathToFileURL } = require('bare-url')
5
6
  const Bundle = require('bare-bundle')
6
- const { parse } = require('cjs-module-lexer')
7
7
  const Protocol = require('./lib/protocol')
8
8
  const constants = require('./lib/constants')
9
9
  const errors = require('./lib/errors')
@@ -149,21 +149,23 @@ const Module = module.exports = exports = class Module {
149
149
 
150
150
  switch (module._type) {
151
151
  case constants.types.SCRIPT: {
152
- const result = parse(module._function.toString())
152
+ const result = lex(module._function.toString())
153
153
 
154
- for (const name of result.exports) names.add(name)
154
+ for (const { name } of result.exports) names.add(name)
155
155
 
156
156
  const referrer = module
157
157
 
158
- for (const specifier of result.reexports) {
159
- const resolved = Module.resolve(specifier, referrer._url, { isImport: true, referrer })
158
+ for (const { specifier, type } of result.imports) {
159
+ if (type & lex.constants.REEXPORT) {
160
+ const resolved = Module.resolve(specifier, referrer._url, { isImport: true, referrer })
160
161
 
161
- const module = Module.load(resolved, { isImport: true, referrer })
162
+ const module = Module.load(resolved, { isImport: true, referrer })
162
163
 
163
- if (module._names) {
164
- for (const name of module._names) names.add(name)
165
- } else {
166
- queue.push(module)
164
+ if (module._names) {
165
+ for (const name of module._names) names.add(name)
166
+ } else {
167
+ queue.push(module)
168
+ }
167
169
  }
168
170
  }
169
171
 
@@ -372,9 +374,17 @@ const Module = module.exports = exports = class Module {
372
374
  conditions = referrer ? referrer._conditions : self._conditions
373
375
  } = opts
374
376
 
375
- if (cache[url.href]) return cache[url.href]._transform(isImport, isDynamicImport)
377
+ let module = cache[url.href] || null
376
378
 
377
- const module = cache[url.href] = new Module(url)
379
+ if (module !== null) {
380
+ if (type !== 0 && type !== module._type) {
381
+ throw errors.TYPE_INCOMPATIBLE(`Module '${module.url.href}' is not of type '${nameOfType(type)}'`)
382
+ }
383
+
384
+ return module._transform(isImport, isDynamicImport)
385
+ }
386
+
387
+ module = cache[url.href] = new Module(url)
378
388
 
379
389
  try {
380
390
  switch (url.protocol) {
@@ -434,7 +444,7 @@ const Module = module.exports = exports = class Module {
434
444
 
435
445
  const resolved = protocol.preresolve(specifier, parentURL)
436
446
 
437
- const [resolution] = protocol.resolve(specifier, parentURL, imports)
447
+ const [resolution] = protocol.resolve(resolved, parentURL, imports)
438
448
 
439
449
  if (resolution) return protocol.postresolve(resolution)
440
450
 
@@ -487,14 +497,20 @@ const Module = module.exports = exports = class Module {
487
497
  conditions = referrer ? referrer._conditions : self._conditions
488
498
  } = opts
489
499
 
490
- const [resolution = null] = resolve(specifier, parentURL, {
500
+ const resolved = protocol.preresolve(specifier, parentURL)
501
+
502
+ const [resolution] = protocol.resolve(resolved, parentURL, imports)
503
+
504
+ if (resolution) return protocol.postresolve(resolution)
505
+
506
+ for (const resolution of resolve(resolved, parentURL, {
491
507
  conditions: ['asset', ...conditions],
492
508
  imports,
493
509
  resolutions
494
- }, readPackage)
495
-
496
- if (resolution !== null && protocol.exists(resolution)) {
497
- return protocol.asset(resolution)
510
+ }, readPackage)) {
511
+ if (protocol.exists(resolution)) {
512
+ return protocol.postresolve(protocol.asset ? protocol.asset(resolution) : resolution)
513
+ }
498
514
  }
499
515
 
500
516
  let msg = `Cannot find asset '${specifier}'`
@@ -555,6 +571,27 @@ function canonicalExtensionForType (type) {
555
571
  }
556
572
  }
557
573
 
574
+ function nameOfType (type) {
575
+ switch (type) {
576
+ case constants.types.SCRIPT:
577
+ return 'script'
578
+ case constants.types.MODULE:
579
+ return 'module'
580
+ case constants.types.JSON:
581
+ return 'json'
582
+ case constants.types.BUNDLE:
583
+ return 'bundle'
584
+ case constants.types.ADDON:
585
+ return 'bare'
586
+ case constants.types.BINARY:
587
+ return 'binary'
588
+ case constants.types.TEXT:
589
+ return 'text'
590
+ default:
591
+ return null
592
+ }
593
+ }
594
+
558
595
  function typeForAttributes (attributes) {
559
596
  if (typeof attributes !== 'object' || attributes === null) return 0
560
597
 
package/lib/errors.js CHANGED
@@ -31,4 +31,8 @@ module.exports = class ModuleError extends Error {
31
31
  static INVALID_URL_PATH (msg) {
32
32
  return new ModuleError(msg, 'INVALID_URL_PATH', ModuleError.INVALID_URL_PATH)
33
33
  }
34
+
35
+ static TYPE_INCOMPATIBLE (msg) {
36
+ return new ModuleError(msg, 'TYPE_INCOMPATIBLE', ModuleError.TYPE_INCOMPATIBLE)
37
+ }
34
38
  }
package/lib/protocol.js CHANGED
@@ -7,6 +7,7 @@ module.exports = class ModuleProtocol {
7
7
  'exists',
8
8
  'read',
9
9
  'load',
10
+ 'addon',
10
11
  'asset'
11
12
  ]) {
12
13
  const method = methods[name]
@@ -41,6 +42,10 @@ module.exports = class ModuleProtocol {
41
42
  return null
42
43
  }
43
44
 
45
+ addon (url) {
46
+ return url
47
+ }
48
+
44
49
  asset (url) {
45
50
  return url
46
51
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "4.5.2",
3
+ "version": "4.6.1",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -27,10 +27,10 @@
27
27
  "homepage": "https://github.com/holepunchto/bare-module#readme",
28
28
  "dependencies": {
29
29
  "bare-bundle": "^1.3.0",
30
- "bare-module-resolve": "^1.6.0",
30
+ "bare-module-lexer": "^1.0.0",
31
+ "bare-module-resolve": "^1.8.0",
31
32
  "bare-path": "^3.0.0",
32
- "bare-url": "^2.0.1",
33
- "cjs-module-lexer": "^1.2.3"
33
+ "bare-url": "^2.0.1"
34
34
  },
35
35
  "devDependencies": {
36
36
  "brittle": "^3.1.1",