bare-module 4.1.1 → 4.3.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 +17 -2
- package/index.js +66 -6
- package/lib/errors.js +4 -0
- package/lib/protocol.js +36 -22
- package/package.json +4 -4
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,
|
|
@@ -238,6 +239,20 @@ Options include:
|
|
|
238
239
|
}
|
|
239
240
|
```
|
|
240
241
|
|
|
242
|
+
#### `const url = Module.asset(specifier, parentURL[, options])`
|
|
243
|
+
|
|
244
|
+
Options include:
|
|
245
|
+
|
|
246
|
+
```js
|
|
247
|
+
{
|
|
248
|
+
referrer = null,
|
|
249
|
+
protocol,
|
|
250
|
+
imports,
|
|
251
|
+
resolutions,
|
|
252
|
+
conditions
|
|
253
|
+
}
|
|
254
|
+
```
|
|
255
|
+
|
|
241
256
|
#### `module.url`
|
|
242
257
|
|
|
243
258
|
#### `module.filename`
|
|
@@ -289,9 +304,9 @@ Options include:
|
|
|
289
304
|
|
|
290
305
|
### Protocols
|
|
291
306
|
|
|
292
|
-
#### `const protocol = new Module.Protocol(
|
|
307
|
+
#### `const protocol = new Module.Protocol(methods)`
|
|
293
308
|
|
|
294
|
-
|
|
309
|
+
Methods include:
|
|
295
310
|
|
|
296
311
|
```js
|
|
297
312
|
{
|
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')
|
|
@@ -324,6 +324,7 @@ const Module = module.exports = exports = class Module {
|
|
|
324
324
|
meta.cache = module._cache
|
|
325
325
|
meta.resolve = resolve
|
|
326
326
|
meta.addon = addon
|
|
327
|
+
meta.asset = asset
|
|
327
328
|
|
|
328
329
|
function resolve (specifier) {
|
|
329
330
|
const resolved = self.resolve(specifier, referrer._url, { referrer })
|
|
@@ -341,6 +342,10 @@ const Module = module.exports = exports = class Module {
|
|
|
341
342
|
|
|
342
343
|
return addon._exports
|
|
343
344
|
}
|
|
345
|
+
|
|
346
|
+
function asset (specifier) {
|
|
347
|
+
return self.asset(specifier, referrer._url, { referrer }).href
|
|
348
|
+
}
|
|
344
349
|
}
|
|
345
350
|
|
|
346
351
|
static _onrun (reason, promise, err = reason) {
|
|
@@ -446,7 +451,7 @@ const Module = module.exports = exports = class Module {
|
|
|
446
451
|
|
|
447
452
|
const [resolution] = protocol.resolve(specifier, parentURL, imports)
|
|
448
453
|
|
|
449
|
-
if (resolution) return protocol.postresolve(resolution
|
|
454
|
+
if (resolution) return protocol.postresolve(resolution)
|
|
450
455
|
|
|
451
456
|
for (const resolution of resolve(resolved, parentURL, {
|
|
452
457
|
conditions: isImport ? ['import', ...conditions] : ['require', ...conditions],
|
|
@@ -469,7 +474,7 @@ const Module = module.exports = exports = class Module {
|
|
|
469
474
|
case 'builtin:': return resolution
|
|
470
475
|
default:
|
|
471
476
|
if (protocol.exists(resolution)) {
|
|
472
|
-
return protocol.postresolve(resolution
|
|
477
|
+
return protocol.postresolve(resolution)
|
|
473
478
|
}
|
|
474
479
|
}
|
|
475
480
|
}
|
|
@@ -489,6 +494,46 @@ const Module = module.exports = exports = class Module {
|
|
|
489
494
|
}
|
|
490
495
|
}
|
|
491
496
|
|
|
497
|
+
static asset (specifier, parentURL, opts = {}) {
|
|
498
|
+
const self = Module
|
|
499
|
+
|
|
500
|
+
if (typeof specifier !== 'string') {
|
|
501
|
+
throw new TypeError(`Specifier must be a string. Received type ${typeof specifier} (${specifier})`)
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
const {
|
|
505
|
+
referrer = null,
|
|
506
|
+
protocol = referrer ? referrer._protocol : self._protocol,
|
|
507
|
+
imports = referrer ? referrer._imports : null,
|
|
508
|
+
resolutions = referrer ? referrer._resolutions : null,
|
|
509
|
+
conditions = referrer ? referrer._conditions : self._conditions
|
|
510
|
+
} = opts
|
|
511
|
+
|
|
512
|
+
const [resolution = null] = resolve(specifier, parentURL, {
|
|
513
|
+
conditions,
|
|
514
|
+
imports,
|
|
515
|
+
resolutions
|
|
516
|
+
}, readPackage)
|
|
517
|
+
|
|
518
|
+
if (resolution !== null && protocol.exists(resolution)) {
|
|
519
|
+
return protocol.asset(resolution)
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
let msg = `Cannot find asset '${specifier}'`
|
|
523
|
+
|
|
524
|
+
if (referrer) msg += ` imported from '${referrer._url.href}'`
|
|
525
|
+
|
|
526
|
+
throw errors.ASSET_NOT_FOUND(msg)
|
|
527
|
+
|
|
528
|
+
function readPackage (packageURL) {
|
|
529
|
+
if (protocol.exists(packageURL)) {
|
|
530
|
+
return Module.load(packageURL, { protocol })._exports
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
return null
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
492
537
|
static _extensionFor (type) {
|
|
493
538
|
switch (type) {
|
|
494
539
|
case constants.types.SCRIPT:
|
|
@@ -568,6 +613,7 @@ const createRequire = exports.createRequire = function createRequire (parentURL,
|
|
|
568
613
|
require.cache = module._cache
|
|
569
614
|
require.resolve = resolve
|
|
570
615
|
require.addon = addon
|
|
616
|
+
require.asset = asset
|
|
571
617
|
|
|
572
618
|
return require
|
|
573
619
|
|
|
@@ -595,6 +641,10 @@ const createRequire = exports.createRequire = function createRequire (parentURL,
|
|
|
595
641
|
|
|
596
642
|
return addon._exports
|
|
597
643
|
}
|
|
644
|
+
|
|
645
|
+
function asset (specifier) {
|
|
646
|
+
return urlToPath(self.asset(specifier, referrer._url, { referrer }))
|
|
647
|
+
}
|
|
598
648
|
}
|
|
599
649
|
|
|
600
650
|
if (Bare.simulator) Module._conditions.push('simulator')
|
|
@@ -716,12 +766,22 @@ Module._extensions['.bundle'] = function (module, source, referrer) {
|
|
|
716
766
|
module._imports = bundle.imports
|
|
717
767
|
module._resolutions = bundle.resolutions
|
|
718
768
|
|
|
719
|
-
module._protocol =
|
|
720
|
-
|
|
769
|
+
module._protocol = protocol.extend({
|
|
770
|
+
preresolve (context, specifier) {
|
|
771
|
+
return specifier
|
|
772
|
+
},
|
|
773
|
+
|
|
774
|
+
postresolve (context, url) {
|
|
775
|
+
return url
|
|
776
|
+
},
|
|
777
|
+
|
|
778
|
+
* resolve () {},
|
|
779
|
+
|
|
780
|
+
exists (context, url) {
|
|
721
781
|
return bundle.exists(url.href)
|
|
722
782
|
},
|
|
723
783
|
|
|
724
|
-
read (url) {
|
|
784
|
+
read (context, url) {
|
|
725
785
|
return bundle.read(url.href)
|
|
726
786
|
}
|
|
727
787
|
})
|
package/lib/errors.js
CHANGED
|
@@ -16,6 +16,10 @@ module.exports = class ModuleError extends Error {
|
|
|
16
16
|
return new ModuleError(msg, 'MODULE_NOT_FOUND', ModuleError.MODULE_NOT_FOUND)
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
+
static ASSET_NOT_FOUND (msg) {
|
|
20
|
+
return new ModuleError(msg, 'ASSET_NOT_FOUND', ModuleError.ASSET_NOT_FOUND)
|
|
21
|
+
}
|
|
22
|
+
|
|
19
23
|
static UNKNOWN_PROTOCOL (msg) {
|
|
20
24
|
return new ModuleError(msg, 'UNKNOWN_PROTOCOL', ModuleError.UNKNOWN_PROTOCOL)
|
|
21
25
|
}
|
package/lib/protocol.js
CHANGED
|
@@ -1,37 +1,51 @@
|
|
|
1
1
|
module.exports = class ModuleProtocol {
|
|
2
|
-
constructor (
|
|
3
|
-
const
|
|
4
|
-
preresolve
|
|
5
|
-
postresolve
|
|
6
|
-
resolve
|
|
7
|
-
exists
|
|
8
|
-
read
|
|
9
|
-
load
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
2
|
+
constructor (methods = {}, context = null) {
|
|
3
|
+
for (const name of [
|
|
4
|
+
'preresolve',
|
|
5
|
+
'postresolve',
|
|
6
|
+
'resolve',
|
|
7
|
+
'exists',
|
|
8
|
+
'read',
|
|
9
|
+
'load',
|
|
10
|
+
'asset'
|
|
11
|
+
]) {
|
|
12
|
+
const method = methods[name]
|
|
13
|
+
|
|
14
|
+
if (typeof method === 'function') {
|
|
15
|
+
this[name] = context ? method.bind(this, context) : method.bind(this)
|
|
16
|
+
} else if (context) {
|
|
17
|
+
const method = context[name]
|
|
18
|
+
|
|
19
|
+
if (typeof method === 'function') {
|
|
20
|
+
this[name] = method
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
18
24
|
}
|
|
19
25
|
|
|
20
|
-
preresolve (specifier,
|
|
26
|
+
preresolve (specifier, parentURL) {
|
|
21
27
|
return specifier
|
|
22
28
|
}
|
|
23
29
|
|
|
24
|
-
postresolve (
|
|
25
|
-
return
|
|
30
|
+
postresolve (url) {
|
|
31
|
+
return url
|
|
26
32
|
}
|
|
27
33
|
|
|
28
|
-
* resolve (specifier,
|
|
34
|
+
* resolve (specifier, parentURL, imports) {}
|
|
29
35
|
|
|
30
|
-
exists (
|
|
36
|
+
exists (url) {
|
|
31
37
|
return false
|
|
32
38
|
}
|
|
33
39
|
|
|
34
|
-
read (
|
|
40
|
+
read (url) {
|
|
35
41
|
return null
|
|
36
42
|
}
|
|
43
|
+
|
|
44
|
+
asset (url) {
|
|
45
|
+
return url
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
extend (methods) {
|
|
49
|
+
return new ModuleProtocol(methods, this)
|
|
50
|
+
}
|
|
37
51
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bare-module",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.3.0",
|
|
4
4
|
"description": "Module support for JavaScript",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -26,11 +26,11 @@
|
|
|
26
26
|
},
|
|
27
27
|
"homepage": "https://github.com/holepunchto/bare-module#readme",
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"bare-bundle": "^1.
|
|
29
|
+
"bare-bundle": "^1.3.0",
|
|
30
30
|
"bare-module-resolve": "^1.6.0",
|
|
31
31
|
"bare-path": "^3.0.0",
|
|
32
|
-
"
|
|
33
|
-
"
|
|
32
|
+
"bare-url": "^2.0.1",
|
|
33
|
+
"cjs-module-lexer": "^1.2.3"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"brittle": "^3.1.1",
|