bare-module 1.14.8 → 2.0.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/binding.js +1 -1
- package/index.js +101 -102
- package/lib/protocol.js +3 -1
- package/package.json +5 -3
package/binding.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
module.exports =
|
|
1
|
+
module.exports = require.addon()
|
package/index.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
const
|
|
3
|
-
const
|
|
1
|
+
/* global Bare */
|
|
2
|
+
const path = require('bare-path')
|
|
3
|
+
const os = require('bare-os')
|
|
4
4
|
const Bundle = require('bare-bundle')
|
|
5
5
|
const Protocol = require('./lib/protocol')
|
|
6
6
|
const constants = require('./lib/constants')
|
|
7
7
|
const errors = require('./lib/errors')
|
|
8
8
|
const binding = require('./binding')
|
|
9
|
+
const addon = require.addon.bind(require)
|
|
9
10
|
|
|
10
|
-
module.exports = exports = class Module {
|
|
11
|
+
const Module = module.exports = exports = class Module {
|
|
11
12
|
constructor (filename) {
|
|
12
13
|
this._filename = filename
|
|
13
14
|
this._state = 0
|
|
@@ -87,7 +88,6 @@ module.exports = exports = class Module {
|
|
|
87
88
|
|
|
88
89
|
static _extensions = Object.create(null)
|
|
89
90
|
static _protocols = Object.create(null)
|
|
90
|
-
static _builtins = Object.create(null)
|
|
91
91
|
static _imports = Object.create(null)
|
|
92
92
|
static _cache = Object.create(null)
|
|
93
93
|
static _bundles = Object.create(null)
|
|
@@ -170,14 +170,6 @@ module.exports = exports = class Module {
|
|
|
170
170
|
return this._cache
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
-
static get builtinModules () {
|
|
174
|
-
return Object.keys(this._builtins)
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
static isBuiltin (name) {
|
|
178
|
-
return name in this._builtins
|
|
179
|
-
}
|
|
180
|
-
|
|
181
173
|
static createRequire (filename, opts = {}) {
|
|
182
174
|
const {
|
|
183
175
|
imports = this._imports,
|
|
@@ -216,6 +208,7 @@ module.exports = exports = class Module {
|
|
|
216
208
|
require.main = module._main
|
|
217
209
|
require.cache = this._cache
|
|
218
210
|
require.resolve = resolve
|
|
211
|
+
require.addon = (specifier = path.dirname(module._filename)) => addon(specifier)
|
|
219
212
|
|
|
220
213
|
return require
|
|
221
214
|
}
|
|
@@ -274,25 +267,21 @@ module.exports = exports = class Module {
|
|
|
274
267
|
dirname = path.dirname(dirname)
|
|
275
268
|
} while (dirname !== path.sep && dirname !== '.')
|
|
276
269
|
|
|
277
|
-
|
|
278
|
-
module._exports = this._builtins[specifier]
|
|
279
|
-
} else {
|
|
280
|
-
module._main = main || module
|
|
281
|
-
|
|
282
|
-
let extension = this._extensionFor(type) || path.extname(specifier)
|
|
270
|
+
module._main = main || module
|
|
283
271
|
|
|
284
|
-
|
|
285
|
-
if (defaultType) extension = this._extensionFor(defaultType) || '.js'
|
|
286
|
-
else extension = '.js'
|
|
287
|
-
}
|
|
272
|
+
let extension = this._extensionFor(type) || path.extname(specifier)
|
|
288
273
|
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
274
|
+
if (extension in this._extensions === false) {
|
|
275
|
+
if (defaultType) extension = this._extensionFor(defaultType) || '.js'
|
|
276
|
+
else extension = '.js'
|
|
277
|
+
}
|
|
292
278
|
|
|
293
|
-
|
|
279
|
+
if (extension === '.bundle' && path.extname(specifier) !== extension) {
|
|
280
|
+
throw errors.INVALID_BUNDLE_EXTENSION(`Invalid extension for bundle '${specifier}'`)
|
|
294
281
|
}
|
|
295
282
|
|
|
283
|
+
this._extensions[extension].call(this, module, source, referrer, protocol, imports)
|
|
284
|
+
|
|
296
285
|
return this._transform(module, referrer, dynamic)
|
|
297
286
|
}
|
|
298
287
|
|
|
@@ -349,10 +338,6 @@ module.exports = exports = class Module {
|
|
|
349
338
|
|
|
350
339
|
yield * protocol.resolve(specifier, dirname, imports)
|
|
351
340
|
|
|
352
|
-
if (this.isBuiltin(specifier)) {
|
|
353
|
-
yield specifier
|
|
354
|
-
}
|
|
355
|
-
|
|
356
341
|
if (path.isAbsolute(specifier)) {
|
|
357
342
|
yield * this._resolveFile(specifier, protocol)
|
|
358
343
|
yield * this._resolveDirectory(specifier, protocol)
|
|
@@ -410,8 +395,10 @@ module.exports = exports = class Module {
|
|
|
410
395
|
}
|
|
411
396
|
|
|
412
397
|
static * _resolveNodeModules (specifier, dirname, protocol) {
|
|
398
|
+
const [, name, expansion = '.'] = /^((?:@[^/\\%]+\/)?[^./\\%][^/\\%]*)(\/.*)?$/.exec(specifier) || []
|
|
399
|
+
|
|
413
400
|
for (const nodeModules of this._resolveNodeModulesPaths(dirname)) {
|
|
414
|
-
|
|
401
|
+
let resolved = specifier
|
|
415
402
|
|
|
416
403
|
if (name) {
|
|
417
404
|
const pkg = path.join(nodeModules, name, 'package.json')
|
|
@@ -424,16 +411,16 @@ module.exports = exports = class Module {
|
|
|
424
411
|
|
|
425
412
|
if (info) {
|
|
426
413
|
if (info.exports) {
|
|
427
|
-
|
|
414
|
+
resolved = this._mapConditionalExport(expansion, dirname, info.exports)
|
|
428
415
|
|
|
429
|
-
if (
|
|
416
|
+
if (resolved) resolved = path.join(name, resolved)
|
|
430
417
|
else return
|
|
431
418
|
}
|
|
432
419
|
}
|
|
433
420
|
}
|
|
434
421
|
}
|
|
435
422
|
|
|
436
|
-
const filename = path.join(nodeModules,
|
|
423
|
+
const filename = path.join(nodeModules, resolved)
|
|
437
424
|
|
|
438
425
|
yield * this._resolveFile(filename, protocol)
|
|
439
426
|
yield * this._resolveDirectory(filename, protocol)
|
|
@@ -606,7 +593,7 @@ module.exports = exports = class Module {
|
|
|
606
593
|
}
|
|
607
594
|
}
|
|
608
595
|
|
|
609
|
-
|
|
596
|
+
Module._extensions['.js'] = function (module, source, referrer, protocol, imports) {
|
|
610
597
|
const isESM = (
|
|
611
598
|
// The default type is ES modules.
|
|
612
599
|
(module._defaultType === constants.types.MODULE) ||
|
|
@@ -623,99 +610,118 @@ exports._extensions['.js'] = function (module, source, referrer, protocol, impor
|
|
|
623
610
|
return loader.call(this, module, source, referrer, protocol, imports)
|
|
624
611
|
}
|
|
625
612
|
|
|
626
|
-
|
|
627
|
-
|
|
613
|
+
Module._extensions['.cjs'] = function (module, source, referrer, protocol, imports) {
|
|
614
|
+
module._type = constants.types.SCRIPT
|
|
615
|
+
module._protocol = protocol
|
|
616
|
+
module._imports = imports
|
|
628
617
|
|
|
629
|
-
if (
|
|
618
|
+
if (protocol.load) {
|
|
619
|
+
module._exports = protocol.load(module._filename)
|
|
620
|
+
} else {
|
|
621
|
+
if (source === null) source = protocol.read(module._filename)
|
|
630
622
|
|
|
631
|
-
|
|
623
|
+
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
632
624
|
|
|
633
|
-
|
|
634
|
-
return this.resolve(specifier, path.dirname(module._filename), {
|
|
635
|
-
protocol: this._protocolFor(specifier, protocol),
|
|
636
|
-
imports,
|
|
637
|
-
referrer
|
|
638
|
-
})
|
|
639
|
-
}
|
|
625
|
+
referrer = module
|
|
640
626
|
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
627
|
+
const resolve = (specifier) => {
|
|
628
|
+
return this.resolve(specifier, path.dirname(module._filename), {
|
|
629
|
+
protocol: this._protocolFor(specifier, protocol),
|
|
630
|
+
imports,
|
|
631
|
+
referrer
|
|
632
|
+
})
|
|
633
|
+
}
|
|
647
634
|
|
|
648
|
-
|
|
649
|
-
|
|
635
|
+
const require = (specifier) => {
|
|
636
|
+
const module = this.load(resolve(specifier), {
|
|
637
|
+
protocol: this._protocolFor(specifier, protocol),
|
|
638
|
+
imports,
|
|
639
|
+
referrer
|
|
640
|
+
})
|
|
650
641
|
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
module._imports = imports
|
|
654
|
-
module._exports = {}
|
|
655
|
-
|
|
656
|
-
require.main = module._main
|
|
657
|
-
require.cache = this._cache
|
|
658
|
-
require.resolve = resolve
|
|
659
|
-
|
|
660
|
-
binding.createFunction(module._filename, ['require', 'module', 'exports', '__filename', '__dirname'], source, 0)(
|
|
661
|
-
require,
|
|
662
|
-
module,
|
|
663
|
-
module._exports,
|
|
664
|
-
module._filename,
|
|
665
|
-
path.dirname(module._filename)
|
|
666
|
-
)
|
|
667
|
-
}
|
|
642
|
+
return module._exports
|
|
643
|
+
}
|
|
668
644
|
|
|
669
|
-
|
|
670
|
-
if (source === null) source = protocol.read(module._filename)
|
|
645
|
+
module._exports = {}
|
|
671
646
|
|
|
672
|
-
|
|
647
|
+
require.main = module._main
|
|
648
|
+
require.cache = this._cache
|
|
649
|
+
require.resolve = resolve
|
|
650
|
+
require.addon = (specifier = path.dirname(module._filename)) => addon(specifier)
|
|
673
651
|
|
|
652
|
+
binding.createFunction(module._filename, ['require', 'module', 'exports', '__filename', '__dirname'], source, 0)(
|
|
653
|
+
require,
|
|
654
|
+
module,
|
|
655
|
+
module._exports,
|
|
656
|
+
module._filename,
|
|
657
|
+
path.dirname(module._filename)
|
|
658
|
+
)
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
Module._extensions['.mjs'] = function (module, source, referrer, protocol, imports) {
|
|
674
663
|
module._type = constants.types.MODULE
|
|
675
664
|
module._protocol = protocol
|
|
676
665
|
module._imports = imports
|
|
677
|
-
module._handle = binding.createModule(module._filename, source, 0, this._handle)
|
|
678
|
-
}
|
|
679
666
|
|
|
680
|
-
|
|
681
|
-
|
|
667
|
+
if (protocol.load) {
|
|
668
|
+
module._exports = protocol.load(module._filename)
|
|
669
|
+
} else {
|
|
670
|
+
if (source === null) source = protocol.read(module._filename)
|
|
671
|
+
|
|
672
|
+
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
682
673
|
|
|
683
|
-
|
|
674
|
+
module._handle = binding.createModule(module._filename, source, 0, this._handle)
|
|
675
|
+
}
|
|
676
|
+
}
|
|
684
677
|
|
|
678
|
+
Module._extensions['.json'] = function (module, source, referrer, protocol, imports) {
|
|
685
679
|
module._type = constants.types.JSON
|
|
686
680
|
module._protocol = protocol
|
|
687
681
|
module._imports = imports
|
|
688
|
-
|
|
682
|
+
|
|
683
|
+
if (protocol.load) {
|
|
684
|
+
module._exports = protocol.load(module._filename)
|
|
685
|
+
} else {
|
|
686
|
+
if (source === null) source = protocol.read(module._filename)
|
|
687
|
+
|
|
688
|
+
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
689
|
+
|
|
690
|
+
module._exports = JSON.parse(source)
|
|
691
|
+
}
|
|
689
692
|
}
|
|
690
693
|
|
|
691
|
-
|
|
694
|
+
Module._extensions['.bare'] = function (module, source, referrer, protocol, imports) {
|
|
692
695
|
module._type = constants.types.ADDON
|
|
693
696
|
module._protocol = protocol
|
|
694
697
|
module._imports = imports
|
|
695
|
-
|
|
698
|
+
|
|
699
|
+
module._exports = addon(module._filename)
|
|
696
700
|
}
|
|
697
701
|
|
|
698
|
-
|
|
702
|
+
Module._extensions['.node'] = function (module, source, referrer, protocol, imports) {
|
|
699
703
|
module._type = constants.types.ADDON
|
|
700
704
|
module._protocol = protocol
|
|
701
705
|
module._imports = imports
|
|
702
|
-
|
|
706
|
+
|
|
707
|
+
module._exports = addon(module._filename)
|
|
703
708
|
}
|
|
704
709
|
|
|
705
|
-
|
|
710
|
+
Module._extensions['.bundle'] = function (module, source, referrer, protocol, imports) {
|
|
711
|
+
module._type = constants.types.BUNDLE
|
|
712
|
+
module._protocol = protocol
|
|
713
|
+
module._imports = imports
|
|
714
|
+
|
|
706
715
|
if (source === null) source = protocol.read(module._filename)
|
|
707
716
|
|
|
708
717
|
if (typeof source === 'string') source = Buffer.from(source)
|
|
709
718
|
|
|
710
719
|
const bundle = this._bundleFor(module._filename, protocol, source)
|
|
711
720
|
|
|
712
|
-
module.
|
|
713
|
-
module._protocol = protocol
|
|
714
|
-
module._imports = imports
|
|
715
|
-
module._exports = exports.load(bundle.main, bundle.read(bundle.main), { protocol, imports, referrer })._exports
|
|
721
|
+
module._exports = this.load(bundle.main, bundle.read(bundle.main), { protocol, imports, referrer })._exports
|
|
716
722
|
}
|
|
717
723
|
|
|
718
|
-
|
|
724
|
+
Module._protocols['file:'] = new Protocol({
|
|
719
725
|
preresolve (specifier, dirname) {
|
|
720
726
|
specifier = specifier.replace(/^file:/, '')
|
|
721
727
|
|
|
@@ -726,7 +732,6 @@ exports._protocols['file:'] = new Protocol({
|
|
|
726
732
|
},
|
|
727
733
|
|
|
728
734
|
postresolve (specifier) {
|
|
729
|
-
if (exports.isBuiltin(specifier)) return specifier
|
|
730
735
|
return binding.realpath(specifier)
|
|
731
736
|
},
|
|
732
737
|
|
|
@@ -739,13 +744,7 @@ exports._protocols['file:'] = new Protocol({
|
|
|
739
744
|
}
|
|
740
745
|
})
|
|
741
746
|
|
|
742
|
-
|
|
743
|
-
preresolve (specifier) {
|
|
744
|
-
return specifier.replace(/^node:/, '')
|
|
745
|
-
}
|
|
746
|
-
})
|
|
747
|
-
|
|
748
|
-
exports._protocols['data:'] = new Protocol({
|
|
747
|
+
Module._protocols['data:'] = new Protocol({
|
|
749
748
|
* resolve (specifier) {
|
|
750
749
|
yield specifier
|
|
751
750
|
},
|
|
@@ -757,11 +756,11 @@ exports._protocols['data:'] = new Protocol({
|
|
|
757
756
|
}
|
|
758
757
|
})
|
|
759
758
|
|
|
760
|
-
|
|
759
|
+
Bare
|
|
761
760
|
.prependListener('teardown', () => {
|
|
762
|
-
for (const module of
|
|
761
|
+
for (const module of Module._modules) {
|
|
763
762
|
module.destroy()
|
|
764
763
|
}
|
|
765
764
|
|
|
766
|
-
binding.destroy(
|
|
765
|
+
binding.destroy(Module._handle)
|
|
767
766
|
})
|
package/lib/protocol.js
CHANGED
|
@@ -6,7 +6,8 @@ module.exports = class ModuleProtocol {
|
|
|
6
6
|
postresolve = null,
|
|
7
7
|
resolve = null,
|
|
8
8
|
exists = null,
|
|
9
|
-
read = null
|
|
9
|
+
read = null,
|
|
10
|
+
load = null
|
|
10
11
|
} = opts
|
|
11
12
|
|
|
12
13
|
this.imports = imports
|
|
@@ -16,6 +17,7 @@ module.exports = class ModuleProtocol {
|
|
|
16
17
|
if (resolve) this.resolve = resolve.bind(this)
|
|
17
18
|
if (exists) this.exists = exists.bind(this)
|
|
18
19
|
if (read) this.read = read.bind(this)
|
|
20
|
+
if (load) this.load = load.bind(this)
|
|
19
21
|
}
|
|
20
22
|
|
|
21
23
|
preresolve (specifier, dirname) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bare-module",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Module support for JavaScript",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
},
|
|
17
17
|
"repository": {
|
|
18
18
|
"type": "git",
|
|
19
|
-
"url": "https://github.com/holepunchto/bare-module.git"
|
|
19
|
+
"url": "git+https://github.com/holepunchto/bare-module.git"
|
|
20
20
|
},
|
|
21
21
|
"author": "Holepunch",
|
|
22
22
|
"license": "Apache-2.0",
|
|
@@ -25,7 +25,9 @@
|
|
|
25
25
|
},
|
|
26
26
|
"homepage": "https://github.com/holepunchto/bare-module#readme",
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"bare-bundle": "^0.
|
|
28
|
+
"bare-bundle": "^0.3.0",
|
|
29
|
+
"bare-os": "^2.0.0",
|
|
30
|
+
"bare-path": "^2.0.0"
|
|
29
31
|
},
|
|
30
32
|
"devDependencies": {
|
|
31
33
|
"brittle": "^3.1.1",
|