bare-module 1.13.1 → 1.13.3
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 +41 -50
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
const path = require('path')
|
|
2
|
+
const os = require('os')
|
|
3
|
+
const Addon = require('addon')
|
|
2
4
|
const Bundle = require('bare-bundle')
|
|
3
5
|
const Protocol = require('./lib/protocol')
|
|
4
6
|
const constants = require('./lib/constants')
|
|
@@ -47,10 +49,6 @@ module.exports = exports = class Module {
|
|
|
47
49
|
this._exports = value
|
|
48
50
|
}
|
|
49
51
|
|
|
50
|
-
get imports () {
|
|
51
|
-
return this._imports
|
|
52
|
-
}
|
|
53
|
-
|
|
54
52
|
// For Node.js compatibility
|
|
55
53
|
get id () {
|
|
56
54
|
return this.filename
|
|
@@ -70,8 +68,7 @@ module.exports = exports = class Module {
|
|
|
70
68
|
filename: this.filename,
|
|
71
69
|
dirname: this.dirname,
|
|
72
70
|
main: this.main,
|
|
73
|
-
exports: this.exports
|
|
74
|
-
imports: this.imports
|
|
71
|
+
exports: this.exports
|
|
75
72
|
}
|
|
76
73
|
}
|
|
77
74
|
|
|
@@ -94,7 +91,7 @@ module.exports = exports = class Module {
|
|
|
94
91
|
|
|
95
92
|
imports = referrer._imports
|
|
96
93
|
|
|
97
|
-
specifier = this.resolve(specifier, referrer.
|
|
94
|
+
specifier = this.resolve(specifier, path.dirname(referrer._filename), {
|
|
98
95
|
protocol,
|
|
99
96
|
imports,
|
|
100
97
|
referrer
|
|
@@ -128,9 +125,9 @@ module.exports = exports = class Module {
|
|
|
128
125
|
static _onevaluate (specifier) {
|
|
129
126
|
const module = this._cache[specifier]
|
|
130
127
|
|
|
131
|
-
binding.setExport(module._handle, 'default', module.
|
|
128
|
+
binding.setExport(module._handle, 'default', module._exports)
|
|
132
129
|
|
|
133
|
-
for (const [key, value] of Object.entries(module.
|
|
130
|
+
for (const [key, value] of Object.entries(module._exports)) {
|
|
134
131
|
binding.setExport(module._handle, key, value)
|
|
135
132
|
}
|
|
136
133
|
}
|
|
@@ -139,15 +136,15 @@ module.exports = exports = class Module {
|
|
|
139
136
|
const module = this._cache[specifier]
|
|
140
137
|
|
|
141
138
|
const resolve = (specifier) => {
|
|
142
|
-
return this.resolve(specifier, module.
|
|
139
|
+
return this.resolve(specifier, path.dirname(module._filename), {
|
|
143
140
|
protocol: this._protocolFor(specifier, module._protocol),
|
|
144
141
|
imports: module._imports,
|
|
145
142
|
referrer: module
|
|
146
143
|
})
|
|
147
144
|
}
|
|
148
145
|
|
|
149
|
-
meta.url = module.
|
|
150
|
-
meta.main = module.
|
|
146
|
+
meta.url = module._filename
|
|
147
|
+
meta.main = module._main === module
|
|
151
148
|
meta.resolve = resolve
|
|
152
149
|
}
|
|
153
150
|
|
|
@@ -172,7 +169,7 @@ module.exports = exports = class Module {
|
|
|
172
169
|
const referrer = module
|
|
173
170
|
|
|
174
171
|
const resolve = (specifier) => {
|
|
175
|
-
return this.resolve(specifier, module.
|
|
172
|
+
return this.resolve(specifier, path.dirname(module._filename), {
|
|
176
173
|
referrer
|
|
177
174
|
})
|
|
178
175
|
}
|
|
@@ -182,11 +179,11 @@ module.exports = exports = class Module {
|
|
|
182
179
|
referrer
|
|
183
180
|
})
|
|
184
181
|
|
|
185
|
-
return module.
|
|
182
|
+
return module._exports
|
|
186
183
|
}
|
|
187
184
|
|
|
188
|
-
require.main = module.
|
|
189
|
-
require.cache = this.
|
|
185
|
+
require.main = module._main
|
|
186
|
+
require.cache = this._cache
|
|
190
187
|
require.resolve = resolve
|
|
191
188
|
|
|
192
189
|
return require
|
|
@@ -230,13 +227,13 @@ module.exports = exports = class Module {
|
|
|
230
227
|
|
|
231
228
|
module._defaultType = defaultType
|
|
232
229
|
|
|
233
|
-
let dirname = module.
|
|
230
|
+
let dirname = path.dirname(module._filename)
|
|
234
231
|
do {
|
|
235
232
|
const pkg = path.join(dirname, 'package.json')
|
|
236
233
|
|
|
237
234
|
if (protocol.exists(pkg)) {
|
|
238
235
|
try {
|
|
239
|
-
module._info = Module.load(pkg, { protocol }).
|
|
236
|
+
module._info = Module.load(pkg, { protocol })._exports
|
|
240
237
|
} catch {}
|
|
241
238
|
break
|
|
242
239
|
}
|
|
@@ -245,7 +242,7 @@ module.exports = exports = class Module {
|
|
|
245
242
|
} while (dirname !== '/' && dirname !== '.')
|
|
246
243
|
|
|
247
244
|
if (specifier in this._builtins) {
|
|
248
|
-
module.
|
|
245
|
+
module._exports = this._builtins[specifier]
|
|
249
246
|
} else {
|
|
250
247
|
let extension = this._extensionFor(type) || path.extname(specifier)
|
|
251
248
|
|
|
@@ -260,10 +257,10 @@ module.exports = exports = class Module {
|
|
|
260
257
|
return this._transform(module, referrer, dynamic)
|
|
261
258
|
}
|
|
262
259
|
|
|
263
|
-
static resolve (specifier, dirname =
|
|
260
|
+
static resolve (specifier, dirname = os.cwd(), opts = {}) {
|
|
264
261
|
if (typeof dirname !== 'string') {
|
|
265
262
|
opts = dirname
|
|
266
|
-
dirname =
|
|
263
|
+
dirname = os.cwd()
|
|
267
264
|
}
|
|
268
265
|
|
|
269
266
|
let {
|
|
@@ -293,7 +290,7 @@ module.exports = exports = class Module {
|
|
|
293
290
|
if (resolved === null) {
|
|
294
291
|
let msg = `Cannot find module '${specifier}'`
|
|
295
292
|
|
|
296
|
-
if (referrer) msg += ` imported from '${referrer.
|
|
293
|
+
if (referrer) msg += ` imported from '${referrer._filename}'`
|
|
297
294
|
|
|
298
295
|
throw errors.MODULE_NOT_FOUND(msg)
|
|
299
296
|
}
|
|
@@ -352,7 +349,7 @@ module.exports = exports = class Module {
|
|
|
352
349
|
if (protocol.exists(pkg)) {
|
|
353
350
|
let info
|
|
354
351
|
try {
|
|
355
|
-
info = this.load(pkg, { protocol }).
|
|
352
|
+
info = this.load(pkg, { protocol })._exports
|
|
356
353
|
} catch {
|
|
357
354
|
info = null
|
|
358
355
|
}
|
|
@@ -486,7 +483,7 @@ module.exports = exports = class Module {
|
|
|
486
483
|
binding.runModule(module._handle, this._context)
|
|
487
484
|
|
|
488
485
|
if (module._type === constants.types.MODULE) {
|
|
489
|
-
module.
|
|
486
|
+
module._exports = binding.getNamespace(module._handle)
|
|
490
487
|
}
|
|
491
488
|
|
|
492
489
|
module._state |= constants.states.EVALUATED
|
|
@@ -497,11 +494,11 @@ module.exports = exports = class Module {
|
|
|
497
494
|
|
|
498
495
|
const names = ['default']
|
|
499
496
|
|
|
500
|
-
for (const key of Object.keys(module.
|
|
497
|
+
for (const key of Object.keys(module._exports)) {
|
|
501
498
|
if (key !== 'default') names.push(key)
|
|
502
499
|
}
|
|
503
500
|
|
|
504
|
-
module._handle = binding.createSyntheticModule(module.
|
|
501
|
+
module._handle = binding.createSyntheticModule(module._filename, names, this._context)
|
|
505
502
|
|
|
506
503
|
module._state |= constants.states.SYNTHESIZED
|
|
507
504
|
}
|
|
@@ -525,14 +522,14 @@ exports._extensions['.js'] = function (module, source, referrer, protocol, impor
|
|
|
525
522
|
}
|
|
526
523
|
|
|
527
524
|
exports._extensions['.cjs'] = function (module, source, referrer, protocol, imports) {
|
|
528
|
-
if (source === null) source = protocol.read(module.
|
|
525
|
+
if (source === null) source = protocol.read(module._filename)
|
|
529
526
|
|
|
530
527
|
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
531
528
|
|
|
532
529
|
referrer = module
|
|
533
530
|
|
|
534
531
|
const resolve = (specifier) => {
|
|
535
|
-
return this.resolve(specifier, module.
|
|
532
|
+
return this.resolve(specifier, path.dirname(module._filename), {
|
|
536
533
|
protocol: this._protocolFor(specifier, protocol),
|
|
537
534
|
imports,
|
|
538
535
|
referrer
|
|
@@ -546,78 +543,72 @@ exports._extensions['.cjs'] = function (module, source, referrer, protocol, impo
|
|
|
546
543
|
referrer
|
|
547
544
|
})
|
|
548
545
|
|
|
549
|
-
return module.
|
|
546
|
+
return module._exports
|
|
550
547
|
}
|
|
551
548
|
|
|
552
549
|
module._type = constants.types.SCRIPT
|
|
553
550
|
module._protocol = protocol
|
|
554
551
|
module._imports = imports
|
|
552
|
+
module._exports = {}
|
|
555
553
|
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
require.main = module.main
|
|
559
|
-
require.cache = this.cache
|
|
554
|
+
require.main = module._main
|
|
555
|
+
require.cache = this._cache
|
|
560
556
|
require.resolve = resolve
|
|
561
557
|
|
|
562
|
-
binding.createFunction(module.
|
|
558
|
+
binding.createFunction(module._filename, ['require', 'module', 'exports', '__filename', '__dirname'], source, 0)(
|
|
563
559
|
require,
|
|
564
560
|
module,
|
|
565
|
-
module.
|
|
566
|
-
module.
|
|
567
|
-
module.
|
|
561
|
+
module._exports,
|
|
562
|
+
module._filename,
|
|
563
|
+
path.dirname(module._filename)
|
|
568
564
|
)
|
|
569
565
|
}
|
|
570
566
|
|
|
571
567
|
exports._extensions['.mjs'] = function (module, source, referrer, protocol, imports) {
|
|
572
|
-
if (source === null) source = protocol.read(module.
|
|
568
|
+
if (source === null) source = protocol.read(module._filename)
|
|
573
569
|
|
|
574
570
|
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
575
571
|
|
|
576
572
|
module._type = constants.types.MODULE
|
|
577
573
|
module._protocol = protocol
|
|
578
574
|
module._imports = imports
|
|
579
|
-
|
|
580
|
-
module._handle = binding.createModule(module.filename, source, 0, this._context)
|
|
575
|
+
module._handle = binding.createModule(module._filename, source, 0, this._context)
|
|
581
576
|
}
|
|
582
577
|
|
|
583
578
|
exports._extensions['.json'] = function (module, source, referrer, protocol, imports) {
|
|
584
|
-
if (source === null) source = protocol.read(module.
|
|
579
|
+
if (source === null) source = protocol.read(module._filename)
|
|
585
580
|
|
|
586
581
|
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
587
582
|
|
|
588
583
|
module._type = constants.types.JSON
|
|
589
584
|
module._protocol = protocol
|
|
590
585
|
module._imports = imports
|
|
591
|
-
|
|
592
|
-
module.exports = JSON.parse(source)
|
|
586
|
+
module._exports = JSON.parse(source)
|
|
593
587
|
}
|
|
594
588
|
|
|
595
589
|
exports._extensions['.bare'] = function (module, source, referrer, protocol, imports) {
|
|
596
590
|
module._type = constants.types.ADDON
|
|
597
591
|
module._protocol = protocol
|
|
598
592
|
module._imports = imports
|
|
599
|
-
|
|
600
|
-
module.exports = process.addon(module.filename)
|
|
593
|
+
module._exports = Addon.load(module._filename)
|
|
601
594
|
}
|
|
602
595
|
|
|
603
596
|
exports._extensions['.node'] = function (module, source, referrer, protocol, imports) {
|
|
604
597
|
module._type = constants.types.ADDON
|
|
605
598
|
module._protocol = protocol
|
|
606
599
|
module._imports = imports
|
|
607
|
-
|
|
608
|
-
module.exports = process.addon(module.filename)
|
|
600
|
+
module._exports = Addon.load(module._filename)
|
|
609
601
|
}
|
|
610
602
|
|
|
611
603
|
exports._extensions['.bundle'] = function (module, source, referrer, protocol, imports) {
|
|
612
604
|
if (typeof source === 'string') source = Buffer.from(source)
|
|
613
605
|
|
|
614
|
-
const bundle = this._bundleFor(module.
|
|
606
|
+
const bundle = this._bundleFor(module._filename, protocol, source)
|
|
615
607
|
|
|
616
608
|
module._type = constants.types.BUNDLE
|
|
617
609
|
module._protocol = protocol
|
|
618
610
|
module._imports = imports
|
|
619
|
-
|
|
620
|
-
module.exports = exports.load(bundle.main, bundle.read(bundle.main), { protocol, imports, referrer }).exports
|
|
611
|
+
module._exports = exports.load(bundle.main, bundle.read(bundle.main), { protocol, imports, referrer })._exports
|
|
621
612
|
}
|
|
622
613
|
|
|
623
614
|
exports._protocols['file:'] = new Protocol({
|