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