bare-module 2.7.0 → 2.8.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 +75 -0
- package/index.js +63 -59
- package/lib/protocol.js +0 -3
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -6,6 +6,81 @@ Module support for JavaScript.
|
|
|
6
6
|
npm i bare-module
|
|
7
7
|
```
|
|
8
8
|
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
const Module = require('bare-module')
|
|
13
|
+
````
|
|
14
|
+
|
|
15
|
+
## API
|
|
16
|
+
|
|
17
|
+
#### `Module.constants`
|
|
18
|
+
|
|
19
|
+
#### `Module.constants.states`
|
|
20
|
+
|
|
21
|
+
#### `Module.constants.types`
|
|
22
|
+
|
|
23
|
+
#### `Module.cache`
|
|
24
|
+
|
|
25
|
+
#### `const resolved = Module.resolve(specifier[, dirname][, options])`
|
|
26
|
+
|
|
27
|
+
Options include:
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
{
|
|
31
|
+
}
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
#### `const module = Module.load(specifier[, source][, options])`
|
|
35
|
+
|
|
36
|
+
Options include:
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
{
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
#### `module.filename`
|
|
44
|
+
|
|
45
|
+
#### `module.dirname`
|
|
46
|
+
|
|
47
|
+
#### `module.type`
|
|
48
|
+
|
|
49
|
+
#### `module.defaultType`
|
|
50
|
+
|
|
51
|
+
#### `module.main`
|
|
52
|
+
|
|
53
|
+
#### `module.exports`
|
|
54
|
+
|
|
55
|
+
#### `module.imports`
|
|
56
|
+
|
|
57
|
+
#### `module.builtins`
|
|
58
|
+
|
|
59
|
+
#### `module.conditions`
|
|
60
|
+
|
|
61
|
+
#### `module.protocol`
|
|
62
|
+
|
|
63
|
+
#### `module.destroy()`
|
|
64
|
+
|
|
65
|
+
### Custom `require()`
|
|
66
|
+
|
|
67
|
+
#### `const require = Module.createRequire(filename[, options])`
|
|
68
|
+
|
|
69
|
+
### Protocols
|
|
70
|
+
|
|
71
|
+
#### `const protocol = new Module.Protocol(options)`
|
|
72
|
+
|
|
73
|
+
Options include:
|
|
74
|
+
|
|
75
|
+
```js
|
|
76
|
+
{
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Bundles
|
|
81
|
+
|
|
82
|
+
#### `const bundle = new Module.Bundle()`
|
|
83
|
+
|
|
9
84
|
## License
|
|
10
85
|
|
|
11
86
|
Apache-2.0
|
package/index.js
CHANGED
|
@@ -18,6 +18,7 @@ const Module = module.exports = exports = class Module {
|
|
|
18
18
|
this._main = null
|
|
19
19
|
this._exports = null
|
|
20
20
|
this._imports = null
|
|
21
|
+
this._resolutions = null
|
|
21
22
|
this._builtins = null
|
|
22
23
|
this._conditions = null
|
|
23
24
|
this._protocol = null
|
|
@@ -59,6 +60,10 @@ const Module = module.exports = exports = class Module {
|
|
|
59
60
|
return this._imports
|
|
60
61
|
}
|
|
61
62
|
|
|
63
|
+
get resolutions () {
|
|
64
|
+
return this._resolutions
|
|
65
|
+
}
|
|
66
|
+
|
|
62
67
|
get builtins () {
|
|
63
68
|
return this._builtins
|
|
64
69
|
}
|
|
@@ -156,6 +161,7 @@ const Module = module.exports = exports = class Module {
|
|
|
156
161
|
main: this.main,
|
|
157
162
|
exports: this.exports,
|
|
158
163
|
imports: this.imports,
|
|
164
|
+
resolutions: this.resolutions,
|
|
159
165
|
builtins: this.builtins,
|
|
160
166
|
conditions: this.conditions
|
|
161
167
|
}
|
|
@@ -174,10 +180,7 @@ const Module = module.exports = exports = class Module {
|
|
|
174
180
|
|
|
175
181
|
const protocol = this._protocolFor(specifier, referrer._protocol)
|
|
176
182
|
|
|
177
|
-
specifier = this.resolve(specifier,
|
|
178
|
-
protocol,
|
|
179
|
-
referrer
|
|
180
|
-
})
|
|
183
|
+
specifier = this.resolve(specifier, { protocol, referrer })
|
|
181
184
|
|
|
182
185
|
let type
|
|
183
186
|
|
|
@@ -216,7 +219,6 @@ const Module = module.exports = exports = class Module {
|
|
|
216
219
|
const module = this._cache[specifier]
|
|
217
220
|
|
|
218
221
|
const referrer = module
|
|
219
|
-
const dirname = path.dirname(module._filename)
|
|
220
222
|
|
|
221
223
|
addon.host = Bare.Addon.host
|
|
222
224
|
|
|
@@ -226,16 +228,14 @@ const Module = module.exports = exports = class Module {
|
|
|
226
228
|
meta.addon = addon
|
|
227
229
|
|
|
228
230
|
function resolve (specifier) {
|
|
229
|
-
return self.resolve(specifier,
|
|
231
|
+
return self.resolve(specifier, {
|
|
230
232
|
protocol: self._protocolFor(specifier, module._protocol),
|
|
231
233
|
referrer
|
|
232
234
|
})
|
|
233
235
|
}
|
|
234
236
|
|
|
235
237
|
function addon (specifier = '.') {
|
|
236
|
-
return Bare.Addon.load(Bare.Addon.resolve(specifier,
|
|
237
|
-
referrer
|
|
238
|
-
}))
|
|
238
|
+
return Bare.Addon.load(Bare.Addon.resolve(specifier, { referrer }))
|
|
239
239
|
}
|
|
240
240
|
}
|
|
241
241
|
|
|
@@ -264,6 +264,7 @@ const Module = module.exports = exports = class Module {
|
|
|
264
264
|
referrer = null,
|
|
265
265
|
protocol = self._protocolFor(filename, referrer ? referrer._protocol : self._protocols['file:']),
|
|
266
266
|
imports = referrer ? referrer._imports : null,
|
|
267
|
+
resolutions = referrer ? referrer._resolutions : null,
|
|
267
268
|
builtins = referrer ? referrer._builtins : null,
|
|
268
269
|
conditions = referrer ? referrer._conditions : self._conditions,
|
|
269
270
|
main = referrer ? referrer._main : null,
|
|
@@ -278,13 +279,12 @@ const Module = module.exports = exports = class Module {
|
|
|
278
279
|
module._defaultType = defaultType
|
|
279
280
|
module._protocol = protocol
|
|
280
281
|
module._imports = imports
|
|
282
|
+
module._resolutions = resolutions
|
|
281
283
|
module._builtins = builtins
|
|
282
284
|
module._conditions = conditions
|
|
283
285
|
|
|
284
286
|
referrer = module
|
|
285
287
|
|
|
286
|
-
const dirname = path.dirname(module._filename)
|
|
287
|
-
|
|
288
288
|
addon.host = Bare.Addon.host
|
|
289
289
|
|
|
290
290
|
require.main = module._main
|
|
@@ -304,16 +304,14 @@ const Module = module.exports = exports = class Module {
|
|
|
304
304
|
}
|
|
305
305
|
|
|
306
306
|
function resolve (specifier) {
|
|
307
|
-
return self.resolve(specifier,
|
|
307
|
+
return self.resolve(specifier, {
|
|
308
308
|
protocol: self._protocolFor(specifier, protocol),
|
|
309
309
|
referrer
|
|
310
310
|
})
|
|
311
311
|
}
|
|
312
312
|
|
|
313
313
|
function addon (specifier = '.') {
|
|
314
|
-
return Bare.Addon.load(Bare.Addon.resolve(specifier,
|
|
315
|
-
referrer
|
|
316
|
-
}))
|
|
314
|
+
return Bare.Addon.load(Bare.Addon.resolve(specifier, { referrer }))
|
|
317
315
|
}
|
|
318
316
|
}
|
|
319
317
|
|
|
@@ -332,32 +330,21 @@ const Module = module.exports = exports = class Module {
|
|
|
332
330
|
let {
|
|
333
331
|
dynamic = false,
|
|
334
332
|
referrer = null,
|
|
333
|
+
type = 0,
|
|
334
|
+
defaultType = referrer ? referrer._defaultType : 0,
|
|
335
|
+
main = referrer ? referrer._main : null,
|
|
335
336
|
protocol = self._protocolFor(specifier, referrer ? referrer._protocol : self._protocols['file:']),
|
|
336
337
|
imports = referrer ? referrer._imports : null,
|
|
338
|
+
resolutions = referrer ? referrer._resolutions : null,
|
|
337
339
|
builtins = referrer ? referrer._builtins : null,
|
|
338
|
-
conditions = referrer ? referrer._conditions : self._conditions
|
|
339
|
-
main = referrer ? referrer._main : null,
|
|
340
|
-
defaultType = referrer ? referrer._defaultType : 0,
|
|
341
|
-
type = 0
|
|
340
|
+
conditions = referrer ? referrer._conditions : self._conditions
|
|
342
341
|
} = opts
|
|
343
342
|
|
|
344
343
|
if (self._cache[specifier]) return self._cache[specifier]._transform(referrer, dynamic)
|
|
345
344
|
|
|
346
345
|
const bundle = self._bundleFor(path.dirname(specifier), protocol)
|
|
347
346
|
|
|
348
|
-
if (bundle)
|
|
349
|
-
protocol = new Protocol({
|
|
350
|
-
imports: bundle.imports,
|
|
351
|
-
|
|
352
|
-
exists (filename) {
|
|
353
|
-
return bundle.exists(filename)
|
|
354
|
-
},
|
|
355
|
-
|
|
356
|
-
read (filename) {
|
|
357
|
-
return bundle.read(filename)
|
|
358
|
-
}
|
|
359
|
-
})
|
|
360
|
-
}
|
|
347
|
+
if (bundle) protocol = bundle._protocol
|
|
361
348
|
|
|
362
349
|
const module = self._cache[specifier] = new Module(specifier)
|
|
363
350
|
|
|
@@ -368,6 +355,7 @@ const Module = module.exports = exports = class Module {
|
|
|
368
355
|
module._defaultType = defaultType
|
|
369
356
|
module._protocol = protocol
|
|
370
357
|
module._imports = imports
|
|
358
|
+
module._resolutions = resolutions
|
|
371
359
|
module._builtins = builtins
|
|
372
360
|
module._conditions = conditions
|
|
373
361
|
|
|
@@ -388,7 +376,7 @@ const Module = module.exports = exports = class Module {
|
|
|
388
376
|
return module._transform(referrer, dynamic)
|
|
389
377
|
}
|
|
390
378
|
|
|
391
|
-
static resolve (specifier, dirname =
|
|
379
|
+
static resolve (specifier, dirname = null, opts = {}) {
|
|
392
380
|
const self = Module
|
|
393
381
|
|
|
394
382
|
if (typeof specifier !== 'string') {
|
|
@@ -397,32 +385,24 @@ const Module = module.exports = exports = class Module {
|
|
|
397
385
|
|
|
398
386
|
if (typeof dirname !== 'string') {
|
|
399
387
|
opts = dirname
|
|
400
|
-
dirname =
|
|
388
|
+
dirname = null
|
|
401
389
|
}
|
|
402
390
|
|
|
403
391
|
let {
|
|
404
392
|
referrer = null,
|
|
405
393
|
protocol = referrer ? referrer._protocol : self._protocols['file:'],
|
|
406
394
|
imports = referrer ? referrer._imports : null,
|
|
395
|
+
resolutions = referrer ? referrer._resolutions : null,
|
|
407
396
|
builtins = referrer ? referrer._builtins : null,
|
|
408
397
|
conditions = referrer ? referrer._conditions : self._conditions
|
|
409
398
|
} = opts
|
|
410
399
|
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
if (bundle) {
|
|
414
|
-
protocol = new Protocol({
|
|
415
|
-
imports: bundle.imports,
|
|
400
|
+
if (referrer) dirname = path.dirname(referrer._filename)
|
|
401
|
+
else if (typeof dirname !== 'string') dirname = os.cwd()
|
|
416
402
|
|
|
417
|
-
|
|
418
|
-
return bundle.exists(filename)
|
|
419
|
-
},
|
|
403
|
+
const bundle = self._bundleFor(path.dirname(specifier), protocol)
|
|
420
404
|
|
|
421
|
-
|
|
422
|
-
return bundle.read(filename)
|
|
423
|
-
}
|
|
424
|
-
})
|
|
425
|
-
}
|
|
405
|
+
if (bundle) protocol = bundle._protocol
|
|
426
406
|
|
|
427
407
|
const resolved = protocol.preresolve(specifier, dirname)
|
|
428
408
|
|
|
@@ -430,15 +410,28 @@ const Module = module.exports = exports = class Module {
|
|
|
430
410
|
|
|
431
411
|
if (resolution) return protocol.postresolve(resolution, dirname)
|
|
432
412
|
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
413
|
+
const parentURL = url.pathToFileURL(
|
|
414
|
+
referrer
|
|
415
|
+
? referrer._filename
|
|
416
|
+
: dirname[dirname.length - 1] === path.sep
|
|
417
|
+
? dirname
|
|
418
|
+
: dirname + path.sep
|
|
419
|
+
)
|
|
420
|
+
|
|
421
|
+
if (resolutions) {
|
|
422
|
+
const entries = Object.entries(resolutions)
|
|
436
423
|
|
|
437
|
-
|
|
424
|
+
resolutions = Object.create(null)
|
|
425
|
+
|
|
426
|
+
for (const [path, imports] of entries) {
|
|
427
|
+
resolutions[url.pathToFileURL(path).href] = imports
|
|
428
|
+
}
|
|
429
|
+
}
|
|
438
430
|
|
|
439
431
|
for (const resolution of resolve(resolved, parentURL, {
|
|
440
432
|
conditions,
|
|
441
433
|
imports,
|
|
434
|
+
resolutions,
|
|
442
435
|
builtins: builtins ? Object.keys(builtins) : [],
|
|
443
436
|
extensions: [
|
|
444
437
|
'.js',
|
|
@@ -526,7 +519,7 @@ const Module = module.exports = exports = class Module {
|
|
|
526
519
|
|
|
527
520
|
if (path.extname(name) !== '.bundle') return null
|
|
528
521
|
|
|
529
|
-
return Module.load(name, { protocol })
|
|
522
|
+
return Module.load(name, { protocol })
|
|
530
523
|
}
|
|
531
524
|
}
|
|
532
525
|
|
|
@@ -585,8 +578,6 @@ Module._extensions['.cjs'] = function (module, source, referrer) {
|
|
|
585
578
|
|
|
586
579
|
referrer = module
|
|
587
580
|
|
|
588
|
-
const dirname = path.dirname(module._filename)
|
|
589
|
-
|
|
590
581
|
addon.host = Bare.Addon.host
|
|
591
582
|
|
|
592
583
|
require.main = module._main
|
|
@@ -614,16 +605,14 @@ Module._extensions['.cjs'] = function (module, source, referrer) {
|
|
|
614
605
|
}
|
|
615
606
|
|
|
616
607
|
function resolve (specifier) {
|
|
617
|
-
return self.resolve(specifier,
|
|
608
|
+
return self.resolve(specifier, {
|
|
618
609
|
protocol: self._protocolFor(specifier, protocol),
|
|
619
610
|
referrer
|
|
620
611
|
})
|
|
621
612
|
}
|
|
622
613
|
|
|
623
614
|
function addon (specifier = '.') {
|
|
624
|
-
return Bare.Addon.load(Bare.Addon.resolve(specifier,
|
|
625
|
-
referrer
|
|
626
|
-
}))
|
|
615
|
+
return Bare.Addon.load(Bare.Addon.resolve(specifier, { referrer }))
|
|
627
616
|
}
|
|
628
617
|
}
|
|
629
618
|
}
|
|
@@ -685,10 +674,25 @@ Module._extensions['.bundle'] = function (module, source, referrer) {
|
|
|
685
674
|
|
|
686
675
|
if (typeof source === 'string') source = Buffer.from(source)
|
|
687
676
|
|
|
677
|
+
referrer = module
|
|
678
|
+
|
|
688
679
|
const bundle = module._bundle = Bundle.from(source).mount(module._filename)
|
|
689
680
|
|
|
681
|
+
module._imports = bundle.imports
|
|
682
|
+
module._resolutions = bundle.resolutions
|
|
683
|
+
|
|
684
|
+
module._protocol = new Protocol({
|
|
685
|
+
exists (filename) {
|
|
686
|
+
return bundle.exists(filename)
|
|
687
|
+
},
|
|
688
|
+
|
|
689
|
+
read (filename) {
|
|
690
|
+
return bundle.read(filename)
|
|
691
|
+
}
|
|
692
|
+
})
|
|
693
|
+
|
|
690
694
|
if (bundle.main) {
|
|
691
|
-
module._exports = self.load(bundle.main, bundle.read(bundle.main), {
|
|
695
|
+
module._exports = self.load(bundle.main, bundle.read(bundle.main), { referrer })._exports
|
|
692
696
|
}
|
|
693
697
|
}
|
|
694
698
|
|
package/lib/protocol.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
module.exports = class ModuleProtocol {
|
|
2
2
|
constructor (opts = {}) {
|
|
3
3
|
const {
|
|
4
|
-
imports = null,
|
|
5
4
|
preresolve = null,
|
|
6
5
|
postresolve = null,
|
|
7
6
|
resolve = null,
|
|
@@ -10,8 +9,6 @@ module.exports = class ModuleProtocol {
|
|
|
10
9
|
load = null
|
|
11
10
|
} = opts
|
|
12
11
|
|
|
13
|
-
this.imports = imports
|
|
14
|
-
|
|
15
12
|
if (preresolve) this.preresolve = preresolve.bind(this)
|
|
16
13
|
if (postresolve) this.postresolve = postresolve.bind(this)
|
|
17
14
|
if (resolve) this.resolve = resolve.bind(this)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bare-module",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.8.0",
|
|
4
4
|
"description": "Module support for JavaScript",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
},
|
|
26
26
|
"homepage": "https://github.com/holepunchto/bare-module#readme",
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"bare-bundle": "^0.3.
|
|
29
|
-
"bare-module-resolve": "^1.
|
|
28
|
+
"bare-bundle": "^0.3.4",
|
|
29
|
+
"bare-module-resolve": "^1.3.0",
|
|
30
30
|
"bare-os": "^2.0.0",
|
|
31
31
|
"bare-path": "^2.0.0",
|
|
32
32
|
"bare-url": "^0.3.4"
|