bare-module 4.2.0 → 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 +14 -0
- package/index.js +50 -0
- package/lib/errors.js +4 -0
- package/lib/protocol.js +6 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -239,6 +239,20 @@ Options include:
|
|
|
239
239
|
}
|
|
240
240
|
```
|
|
241
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
|
+
|
|
242
256
|
#### `module.url`
|
|
243
257
|
|
|
244
258
|
#### `module.filename`
|
package/index.js
CHANGED
|
@@ -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) {
|
|
@@ -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')
|
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
|
@@ -6,7 +6,8 @@ module.exports = class ModuleProtocol {
|
|
|
6
6
|
'resolve',
|
|
7
7
|
'exists',
|
|
8
8
|
'read',
|
|
9
|
-
'load'
|
|
9
|
+
'load',
|
|
10
|
+
'asset'
|
|
10
11
|
]) {
|
|
11
12
|
const method = methods[name]
|
|
12
13
|
|
|
@@ -40,6 +41,10 @@ module.exports = class ModuleProtocol {
|
|
|
40
41
|
return null
|
|
41
42
|
}
|
|
42
43
|
|
|
44
|
+
asset (url) {
|
|
45
|
+
return url
|
|
46
|
+
}
|
|
47
|
+
|
|
43
48
|
extend (methods) {
|
|
44
49
|
return new ModuleProtocol(methods, this)
|
|
45
50
|
}
|
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,7 +26,7 @@
|
|
|
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
32
|
"bare-url": "^2.0.1",
|