bare-module 2.4.0 → 2.5.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/index.js +57 -54
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -16,6 +16,7 @@ const Module = module.exports = exports = class Module {
|
|
|
16
16
|
this._main = null
|
|
17
17
|
this._exports = null
|
|
18
18
|
this._imports = null
|
|
19
|
+
this._builtins = null
|
|
19
20
|
this._info = null
|
|
20
21
|
this._protocol = null
|
|
21
22
|
this._handle = null
|
|
@@ -100,16 +101,13 @@ const Module = module.exports = exports = class Module {
|
|
|
100
101
|
static _onimport (specifier, assertions, referrerFilename, dynamic) {
|
|
101
102
|
const referrer = this._cache[referrerFilename]
|
|
102
103
|
|
|
103
|
-
let protocol
|
|
104
|
+
let protocol
|
|
104
105
|
|
|
105
106
|
if (referrer) {
|
|
106
107
|
protocol = this._protocolFor(specifier, referrer._protocol)
|
|
107
108
|
|
|
108
|
-
imports = referrer._imports
|
|
109
|
-
|
|
110
109
|
specifier = this.resolve(specifier, path.dirname(referrer._filename), {
|
|
111
110
|
protocol,
|
|
112
|
-
imports,
|
|
113
111
|
referrer
|
|
114
112
|
})
|
|
115
113
|
} else {
|
|
@@ -129,7 +127,6 @@ const Module = module.exports = exports = class Module {
|
|
|
129
127
|
|
|
130
128
|
const module = this.load(specifier, {
|
|
131
129
|
protocol: this._protocolFor(specifier, protocol),
|
|
132
|
-
imports,
|
|
133
130
|
referrer,
|
|
134
131
|
dynamic,
|
|
135
132
|
type
|
|
@@ -185,6 +182,7 @@ const Module = module.exports = exports = class Module {
|
|
|
185
182
|
static createRequire (filename, opts = {}) {
|
|
186
183
|
const {
|
|
187
184
|
imports = null,
|
|
185
|
+
builtins = null,
|
|
188
186
|
protocol = this._protocolFor(filename, this._protocols['file:']),
|
|
189
187
|
type = constants.types.SCRIPT,
|
|
190
188
|
defaultType = constants.types.SCRIPT
|
|
@@ -195,6 +193,7 @@ const Module = module.exports = exports = class Module {
|
|
|
195
193
|
module._type = type
|
|
196
194
|
module._defaultType = defaultType
|
|
197
195
|
module._imports = imports
|
|
196
|
+
module._builtins = builtins
|
|
198
197
|
module._protocol = protocol
|
|
199
198
|
|
|
200
199
|
const referrer = module
|
|
@@ -203,7 +202,6 @@ const Module = module.exports = exports = class Module {
|
|
|
203
202
|
const resolve = (specifier) => {
|
|
204
203
|
return this.resolve(specifier, dirname, {
|
|
205
204
|
protocol: this._protocolFor(specifier, protocol),
|
|
206
|
-
imports,
|
|
207
205
|
referrer
|
|
208
206
|
})
|
|
209
207
|
}
|
|
@@ -211,7 +209,6 @@ const Module = module.exports = exports = class Module {
|
|
|
211
209
|
const require = (specifier) => {
|
|
212
210
|
const module = this.load(resolve(specifier), {
|
|
213
211
|
protocol: this._protocolFor(specifier, protocol),
|
|
214
|
-
imports,
|
|
215
212
|
referrer
|
|
216
213
|
})
|
|
217
214
|
|
|
@@ -239,13 +236,14 @@ const Module = module.exports = exports = class Module {
|
|
|
239
236
|
}
|
|
240
237
|
|
|
241
238
|
let {
|
|
242
|
-
imports = null,
|
|
243
|
-
protocol = this._protocolFor(specifier, this._protocols['file:']),
|
|
244
|
-
referrer = null,
|
|
245
239
|
dynamic = false,
|
|
240
|
+
referrer = null,
|
|
241
|
+
protocol = this._protocolFor(specifier, referrer ? referrer._protocol : this._protocols['file:']),
|
|
242
|
+
imports = referrer ? referrer._imports : null,
|
|
243
|
+
builtins = referrer ? referrer._builtins : null,
|
|
246
244
|
main = referrer ? referrer._main : null,
|
|
247
|
-
|
|
248
|
-
|
|
245
|
+
defaultType = referrer ? referrer._defaultType : 0,
|
|
246
|
+
type = 0
|
|
249
247
|
} = opts
|
|
250
248
|
|
|
251
249
|
if (this._cache[specifier]) return this._transform(this._cache[specifier], referrer, dynamic)
|
|
@@ -270,23 +268,30 @@ const Module = module.exports = exports = class Module {
|
|
|
270
268
|
|
|
271
269
|
const module = this._cache[specifier] = new this(specifier)
|
|
272
270
|
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
271
|
+
if (builtins && specifier in builtins) {
|
|
272
|
+
module._exports = builtins[specifier]
|
|
273
|
+
} else {
|
|
274
|
+
module._main = main || module
|
|
275
|
+
module._defaultType = defaultType
|
|
276
|
+
module._protocol = protocol
|
|
277
|
+
module._imports = imports
|
|
278
|
+
module._builtins = builtins
|
|
279
|
+
module._info = this._loadPackageManifest(path.dirname(module._filename), protocol)
|
|
280
|
+
|
|
281
|
+
let extension = this._extensionFor(type) || path.extname(specifier)
|
|
282
|
+
|
|
283
|
+
if (extension in this._extensions === false) {
|
|
284
|
+
if (defaultType) extension = this._extensionFor(defaultType) || '.js'
|
|
285
|
+
else extension = '.js'
|
|
286
|
+
}
|
|
278
287
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
}
|
|
288
|
+
if (extension === '.bundle' && path.extname(specifier) !== extension) {
|
|
289
|
+
throw errors.INVALID_BUNDLE_EXTENSION(`Invalid extension for bundle '${specifier}'`)
|
|
290
|
+
}
|
|
283
291
|
|
|
284
|
-
|
|
285
|
-
throw errors.INVALID_BUNDLE_EXTENSION(`Invalid extension for bundle '${specifier}'`)
|
|
292
|
+
this._extensions[extension].call(this, module, source, referrer)
|
|
286
293
|
}
|
|
287
294
|
|
|
288
|
-
this._extensions[extension].call(this, module, source, referrer, protocol, imports)
|
|
289
|
-
|
|
290
295
|
return this._transform(module, referrer, dynamic)
|
|
291
296
|
}
|
|
292
297
|
|
|
@@ -320,9 +325,10 @@ const Module = module.exports = exports = class Module {
|
|
|
320
325
|
}
|
|
321
326
|
|
|
322
327
|
let {
|
|
323
|
-
imports = null,
|
|
324
|
-
protocol = this._protocols['file:'],
|
|
325
328
|
referrer = null,
|
|
329
|
+
protocol = referrer ? referrer._protocol : this._protocols['file:'],
|
|
330
|
+
imports = referrer ? referrer._imports : null,
|
|
331
|
+
builtins = referrer ? referrer._builtins : null,
|
|
326
332
|
conditions = ['import', 'require', 'bare', 'node']
|
|
327
333
|
} = opts
|
|
328
334
|
|
|
@@ -344,7 +350,7 @@ const Module = module.exports = exports = class Module {
|
|
|
344
350
|
})
|
|
345
351
|
}
|
|
346
352
|
|
|
347
|
-
const [resolved = null] = this._resolve(specifier, dirname, protocol, imports, conditions)
|
|
353
|
+
const [resolved = null] = this._resolve(specifier, dirname, protocol, imports, builtins, conditions)
|
|
348
354
|
|
|
349
355
|
if (resolved === null) {
|
|
350
356
|
let msg = `Cannot find module '${specifier}'`
|
|
@@ -357,7 +363,7 @@ const Module = module.exports = exports = class Module {
|
|
|
357
363
|
return protocol.postresolve(resolved, dirname)
|
|
358
364
|
}
|
|
359
365
|
|
|
360
|
-
static * _resolve (specifier, dirname, protocol, imports, conditions) {
|
|
366
|
+
static * _resolve (specifier, dirname, protocol, imports, builtins, conditions) {
|
|
361
367
|
const info = this._loadPackageManifest(dirname, protocol)
|
|
362
368
|
|
|
363
369
|
specifier = this._mapConditionalSpecifier(
|
|
@@ -372,6 +378,8 @@ const Module = module.exports = exports = class Module {
|
|
|
372
378
|
|
|
373
379
|
yield * protocol.resolve(specifier, dirname, imports)
|
|
374
380
|
|
|
381
|
+
if (builtins && specifier in builtins) yield specifier
|
|
382
|
+
|
|
375
383
|
if (path.isAbsolute(specifier)) {
|
|
376
384
|
yield * this._resolveFile(specifier, protocol)
|
|
377
385
|
yield * this._resolveDirectory(specifier, protocol, conditions)
|
|
@@ -636,7 +644,7 @@ const Module = module.exports = exports = class Module {
|
|
|
636
644
|
}
|
|
637
645
|
}
|
|
638
646
|
|
|
639
|
-
Module._extensions['.js'] = function (module, source, referrer
|
|
647
|
+
Module._extensions['.js'] = function (module, source, referrer) {
|
|
640
648
|
const isESM = (
|
|
641
649
|
// The default type is ES modules.
|
|
642
650
|
(module._defaultType === constants.types.MODULE) ||
|
|
@@ -645,18 +653,18 @@ Module._extensions['.js'] = function (module, source, referrer, protocol, import
|
|
|
645
653
|
(module._info && module._info.type === 'module') ||
|
|
646
654
|
|
|
647
655
|
// The source is a data: URI and the referrer is itself an ES module.
|
|
648
|
-
(
|
|
656
|
+
(module._protocol === this._protocols['data:'] && referrer && referrer._type === constants.types.MODULE)
|
|
649
657
|
)
|
|
650
658
|
|
|
651
659
|
const loader = this._extensions[isESM ? '.mjs' : '.cjs']
|
|
652
660
|
|
|
653
|
-
return loader.call(this, module, source, referrer
|
|
661
|
+
return loader.call(this, module, source, referrer)
|
|
654
662
|
}
|
|
655
663
|
|
|
656
|
-
Module._extensions['.cjs'] = function (module, source, referrer
|
|
664
|
+
Module._extensions['.cjs'] = function (module, source, referrer) {
|
|
665
|
+
const protocol = module._protocol
|
|
666
|
+
|
|
657
667
|
module._type = constants.types.SCRIPT
|
|
658
|
-
module._protocol = protocol
|
|
659
|
-
module._imports = imports
|
|
660
668
|
|
|
661
669
|
if (protocol.load) {
|
|
662
670
|
module._exports = protocol.load(module._filename)
|
|
@@ -672,7 +680,6 @@ Module._extensions['.cjs'] = function (module, source, referrer, protocol, impor
|
|
|
672
680
|
const resolve = (specifier) => {
|
|
673
681
|
return this.resolve(specifier, dirname, {
|
|
674
682
|
protocol: this._protocolFor(specifier, protocol),
|
|
675
|
-
imports,
|
|
676
683
|
referrer
|
|
677
684
|
})
|
|
678
685
|
}
|
|
@@ -680,7 +687,6 @@ Module._extensions['.cjs'] = function (module, source, referrer, protocol, impor
|
|
|
680
687
|
const require = (specifier) => {
|
|
681
688
|
const module = this.load(resolve(specifier), {
|
|
682
689
|
protocol: this._protocolFor(specifier, protocol),
|
|
683
|
-
imports,
|
|
684
690
|
referrer
|
|
685
691
|
})
|
|
686
692
|
|
|
@@ -710,10 +716,10 @@ Module._extensions['.cjs'] = function (module, source, referrer, protocol, impor
|
|
|
710
716
|
}
|
|
711
717
|
}
|
|
712
718
|
|
|
713
|
-
Module._extensions['.mjs'] = function (module, source, referrer
|
|
719
|
+
Module._extensions['.mjs'] = function (module, source, referrer) {
|
|
720
|
+
const protocol = module._protocol
|
|
721
|
+
|
|
714
722
|
module._type = constants.types.MODULE
|
|
715
|
-
module._protocol = protocol
|
|
716
|
-
module._imports = imports
|
|
717
723
|
|
|
718
724
|
if (protocol.load) {
|
|
719
725
|
module._exports = protocol.load(module._filename)
|
|
@@ -726,10 +732,10 @@ Module._extensions['.mjs'] = function (module, source, referrer, protocol, impor
|
|
|
726
732
|
}
|
|
727
733
|
}
|
|
728
734
|
|
|
729
|
-
Module._extensions['.json'] = function (module, source, referrer
|
|
735
|
+
Module._extensions['.json'] = function (module, source, referrer) {
|
|
736
|
+
const protocol = module._protocol
|
|
737
|
+
|
|
730
738
|
module._type = constants.types.JSON
|
|
731
|
-
module._protocol = protocol
|
|
732
|
-
module._imports = imports
|
|
733
739
|
|
|
734
740
|
if (protocol.load) {
|
|
735
741
|
module._exports = protocol.load(module._filename)
|
|
@@ -742,26 +748,22 @@ Module._extensions['.json'] = function (module, source, referrer, protocol, impo
|
|
|
742
748
|
}
|
|
743
749
|
}
|
|
744
750
|
|
|
745
|
-
Module._extensions['.bare'] = function (module, source, referrer
|
|
751
|
+
Module._extensions['.bare'] = function (module, source, referrer) {
|
|
746
752
|
module._type = constants.types.ADDON
|
|
747
|
-
module._protocol = protocol
|
|
748
|
-
module._imports = imports
|
|
749
753
|
|
|
750
754
|
module._exports = Bare.Addon.load(module._filename)
|
|
751
755
|
}
|
|
752
756
|
|
|
753
|
-
Module._extensions['.node'] = function (module, source, referrer
|
|
757
|
+
Module._extensions['.node'] = function (module, source, referrer) {
|
|
754
758
|
module._type = constants.types.ADDON
|
|
755
|
-
module._protocol = protocol
|
|
756
|
-
module._imports = imports
|
|
757
759
|
|
|
758
760
|
module._exports = Bare.Addon.load(module._filename)
|
|
759
761
|
}
|
|
760
762
|
|
|
761
|
-
Module._extensions['.bundle'] = function (module, source, referrer
|
|
763
|
+
Module._extensions['.bundle'] = function (module, source, referrer) {
|
|
764
|
+
const protocol = module._protocol
|
|
765
|
+
|
|
762
766
|
module._type = constants.types.BUNDLE
|
|
763
|
-
module._protocol = protocol
|
|
764
|
-
module._imports = imports
|
|
765
767
|
|
|
766
768
|
if (source === null) source = protocol.read(module._filename)
|
|
767
769
|
|
|
@@ -769,7 +771,7 @@ Module._extensions['.bundle'] = function (module, source, referrer, protocol, im
|
|
|
769
771
|
|
|
770
772
|
const bundle = this._bundleFor(module._filename, protocol, source)
|
|
771
773
|
|
|
772
|
-
module._exports = this.load(bundle.main, bundle.read(bundle.main), { protocol,
|
|
774
|
+
module._exports = this.load(bundle.main, bundle.read(bundle.main), { protocol, referrer })._exports
|
|
773
775
|
}
|
|
774
776
|
|
|
775
777
|
Module._protocols['file:'] = new Protocol({
|
|
@@ -783,7 +785,8 @@ Module._protocols['file:'] = new Protocol({
|
|
|
783
785
|
},
|
|
784
786
|
|
|
785
787
|
postresolve (specifier) {
|
|
786
|
-
return binding.realpath(specifier)
|
|
788
|
+
if (path.isAbsolute(specifier)) return binding.realpath(specifier)
|
|
789
|
+
return specifier
|
|
787
790
|
},
|
|
788
791
|
|
|
789
792
|
exists (filename) {
|