bare-module 4.1.0 → 4.2.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
@@ -210,6 +210,7 @@ Options include:
210
210
 
211
211
  ```js
212
212
  {
213
+ isImport = false,
213
214
  referrer = null,
214
215
  protocol,
215
216
  imports,
@@ -289,9 +290,9 @@ Options include:
289
290
 
290
291
  ### Protocols
291
292
 
292
- #### `const protocol = new Module.Protocol(options)`
293
+ #### `const protocol = new Module.Protocol(methods)`
293
294
 
294
- Options include:
295
+ Methods include:
295
296
 
296
297
  ```js
297
298
  {
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 { fileURLToPath, pathToFileURL } = require('bare-url')
4
5
  const Bundle = require('bare-bundle')
5
6
  const { parse } = require('cjs-module-lexer')
6
- const { fileURLToPath, pathToFileURL } = require('url-file-url')
7
7
  const Protocol = require('./lib/protocol')
8
8
  const constants = require('./lib/constants')
9
9
  const errors = require('./lib/errors')
@@ -446,7 +446,7 @@ const Module = module.exports = exports = class Module {
446
446
 
447
447
  const [resolution] = protocol.resolve(specifier, parentURL, imports)
448
448
 
449
- if (resolution) return protocol.postresolve(resolution, parentURL)
449
+ if (resolution) return protocol.postresolve(resolution)
450
450
 
451
451
  for (const resolution of resolve(resolved, parentURL, {
452
452
  conditions: isImport ? ['import', ...conditions] : ['require', ...conditions],
@@ -469,7 +469,7 @@ const Module = module.exports = exports = class Module {
469
469
  case 'builtin:': return resolution
470
470
  default:
471
471
  if (protocol.exists(resolution)) {
472
- return protocol.postresolve(resolution, parentURL)
472
+ return protocol.postresolve(resolution)
473
473
  }
474
474
  }
475
475
  }
@@ -716,12 +716,22 @@ Module._extensions['.bundle'] = function (module, source, referrer) {
716
716
  module._imports = bundle.imports
717
717
  module._resolutions = bundle.resolutions
718
718
 
719
- module._protocol = new Protocol({
720
- exists (url) {
719
+ module._protocol = protocol.extend({
720
+ preresolve (context, specifier) {
721
+ return specifier
722
+ },
723
+
724
+ postresolve (context, url) {
725
+ return url
726
+ },
727
+
728
+ * resolve () {},
729
+
730
+ exists (context, url) {
721
731
  return bundle.exists(url.href)
722
732
  },
723
733
 
724
- read (url) {
734
+ read (context, url) {
725
735
  return bundle.read(url.href)
726
736
  }
727
737
  })
package/lib/protocol.js CHANGED
@@ -1,37 +1,46 @@
1
1
  module.exports = class ModuleProtocol {
2
- constructor (opts = {}) {
3
- const {
4
- preresolve = null,
5
- postresolve = null,
6
- resolve = null,
7
- exists = null,
8
- read = null,
9
- load = null
10
- } = opts
11
-
12
- if (preresolve) this.preresolve = preresolve.bind(this)
13
- if (postresolve) this.postresolve = postresolve.bind(this)
14
- if (resolve) this.resolve = resolve.bind(this)
15
- if (exists) this.exists = exists.bind(this)
16
- if (read) this.read = read.bind(this)
17
- if (load) this.load = load.bind(this)
2
+ constructor (methods = {}, context = null) {
3
+ for (const name of [
4
+ 'preresolve',
5
+ 'postresolve',
6
+ 'resolve',
7
+ 'exists',
8
+ 'read',
9
+ 'load'
10
+ ]) {
11
+ const method = methods[name]
12
+
13
+ if (typeof method === 'function') {
14
+ this[name] = context ? method.bind(this, context) : method.bind(this)
15
+ } else if (context) {
16
+ const method = context[name]
17
+
18
+ if (typeof method === 'function') {
19
+ this[name] = method
20
+ }
21
+ }
22
+ }
18
23
  }
19
24
 
20
- preresolve (specifier, dirname) {
25
+ preresolve (specifier, parentURL) {
21
26
  return specifier
22
27
  }
23
28
 
24
- postresolve (specifier, dirname) {
25
- return specifier
29
+ postresolve (url) {
30
+ return url
26
31
  }
27
32
 
28
- * resolve (specifier, dirname, imports) {}
33
+ * resolve (specifier, parentURL, imports) {}
29
34
 
30
- exists (filename) {
35
+ exists (url) {
31
36
  return false
32
37
  }
33
38
 
34
- read (filename) {
39
+ read (url) {
35
40
  return null
36
41
  }
42
+
43
+ extend (methods) {
44
+ return new ModuleProtocol(methods, this)
45
+ }
37
46
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bare-module",
3
- "version": "4.1.0",
3
+ "version": "4.2.0",
4
4
  "description": "Module support for JavaScript",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -29,8 +29,8 @@
29
29
  "bare-bundle": "^1.0.0",
30
30
  "bare-module-resolve": "^1.6.0",
31
31
  "bare-path": "^3.0.0",
32
- "cjs-module-lexer": "^1.2.3",
33
- "url-file-url": "^1.0.2"
32
+ "bare-url": "^2.0.1",
33
+ "cjs-module-lexer": "^1.2.3"
34
34
  },
35
35
  "devDependencies": {
36
36
  "brittle": "^3.1.1",