bare-module 1.13.0 → 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 +45 -52
- package/lib/constants.js +2 -1
- 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
|
}
|
|
@@ -465,16 +467,11 @@ module.exports = exports = class Module {
|
|
|
465
467
|
|
|
466
468
|
static _transform (module, referrer = null, dynamic = false) {
|
|
467
469
|
if (dynamic) {
|
|
468
|
-
if (module._type !== constants.types.MODULE
|
|
469
|
-
this._synthesize(module)
|
|
470
|
-
}
|
|
471
|
-
|
|
470
|
+
if (module._type !== constants.types.MODULE) this._synthesize(module)
|
|
472
471
|
this._evaluate(module)
|
|
473
472
|
} else if (referrer) {
|
|
474
473
|
if (referrer._type === constants.types.MODULE) {
|
|
475
|
-
if (module._type !== constants.types.MODULE
|
|
476
|
-
this._synthesize(module)
|
|
477
|
-
}
|
|
474
|
+
if (module._type !== constants.types.MODULE) this._synthesize(module)
|
|
478
475
|
} else if (module._type === constants.types.MODULE) {
|
|
479
476
|
this._evaluate(module)
|
|
480
477
|
}
|
|
@@ -491,22 +488,24 @@ module.exports = exports = class Module {
|
|
|
491
488
|
binding.runModule(module._handle, this._context)
|
|
492
489
|
|
|
493
490
|
if (module._type === constants.types.MODULE) {
|
|
494
|
-
module.
|
|
491
|
+
module._exports = binding.getNamespace(module._handle)
|
|
495
492
|
}
|
|
496
493
|
|
|
497
494
|
module._state |= constants.states.EVALUATED
|
|
498
495
|
}
|
|
499
496
|
|
|
500
497
|
static _synthesize (module) {
|
|
498
|
+
if ((module._state & constants.states.SYNTHESIZED) !== 0) return
|
|
499
|
+
|
|
501
500
|
const names = ['default']
|
|
502
501
|
|
|
503
|
-
for (const key of Object.keys(module.
|
|
502
|
+
for (const key of Object.keys(module._exports)) {
|
|
504
503
|
if (key !== 'default') names.push(key)
|
|
505
504
|
}
|
|
506
505
|
|
|
507
|
-
module._handle = binding.createSyntheticModule(module.
|
|
506
|
+
module._handle = binding.createSyntheticModule(module._filename, names, this._context)
|
|
508
507
|
|
|
509
|
-
module._state
|
|
508
|
+
module._state |= constants.states.SYNTHESIZED
|
|
510
509
|
}
|
|
511
510
|
}
|
|
512
511
|
|
|
@@ -528,14 +527,14 @@ exports._extensions['.js'] = function (module, source, referrer, protocol, impor
|
|
|
528
527
|
}
|
|
529
528
|
|
|
530
529
|
exports._extensions['.cjs'] = function (module, source, referrer, protocol, imports) {
|
|
531
|
-
if (source === null) source = protocol.read(module.
|
|
530
|
+
if (source === null) source = protocol.read(module._filename)
|
|
532
531
|
|
|
533
532
|
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
534
533
|
|
|
535
534
|
referrer = module
|
|
536
535
|
|
|
537
536
|
const resolve = (specifier) => {
|
|
538
|
-
return this.resolve(specifier, module.
|
|
537
|
+
return this.resolve(specifier, path.dirname(module._filename), {
|
|
539
538
|
protocol: this._protocolFor(specifier, protocol),
|
|
540
539
|
imports,
|
|
541
540
|
referrer
|
|
@@ -549,78 +548,72 @@ exports._extensions['.cjs'] = function (module, source, referrer, protocol, impo
|
|
|
549
548
|
referrer
|
|
550
549
|
})
|
|
551
550
|
|
|
552
|
-
return module.
|
|
551
|
+
return module._exports
|
|
553
552
|
}
|
|
554
553
|
|
|
555
554
|
module._type = constants.types.SCRIPT
|
|
556
555
|
module._protocol = protocol
|
|
557
556
|
module._imports = imports
|
|
557
|
+
module._exports = {}
|
|
558
558
|
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
require.main = module.main
|
|
562
|
-
require.cache = this.cache
|
|
559
|
+
require.main = module._main
|
|
560
|
+
require.cache = this._cache
|
|
563
561
|
require.resolve = resolve
|
|
564
562
|
|
|
565
|
-
binding.createFunction(module.
|
|
563
|
+
binding.createFunction(module._filename, ['require', 'module', 'exports', '__filename', '__dirname'], source, 0)(
|
|
566
564
|
require,
|
|
567
565
|
module,
|
|
568
|
-
module.
|
|
569
|
-
module.
|
|
570
|
-
module.
|
|
566
|
+
module._exports,
|
|
567
|
+
module._filename,
|
|
568
|
+
path.dirname(module._filename)
|
|
571
569
|
)
|
|
572
570
|
}
|
|
573
571
|
|
|
574
572
|
exports._extensions['.mjs'] = function (module, source, referrer, protocol, imports) {
|
|
575
|
-
if (source === null) source = protocol.read(module.
|
|
573
|
+
if (source === null) source = protocol.read(module._filename)
|
|
576
574
|
|
|
577
575
|
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
578
576
|
|
|
579
577
|
module._type = constants.types.MODULE
|
|
580
578
|
module._protocol = protocol
|
|
581
579
|
module._imports = imports
|
|
582
|
-
|
|
583
|
-
module._handle = binding.createModule(module.filename, source, 0, this._context)
|
|
580
|
+
module._handle = binding.createModule(module._filename, source, 0, this._context)
|
|
584
581
|
}
|
|
585
582
|
|
|
586
583
|
exports._extensions['.json'] = function (module, source, referrer, protocol, imports) {
|
|
587
|
-
if (source === null) source = protocol.read(module.
|
|
584
|
+
if (source === null) source = protocol.read(module._filename)
|
|
588
585
|
|
|
589
586
|
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
590
587
|
|
|
591
588
|
module._type = constants.types.JSON
|
|
592
589
|
module._protocol = protocol
|
|
593
590
|
module._imports = imports
|
|
594
|
-
|
|
595
|
-
module.exports = JSON.parse(source)
|
|
591
|
+
module._exports = JSON.parse(source)
|
|
596
592
|
}
|
|
597
593
|
|
|
598
594
|
exports._extensions['.bare'] = function (module, source, referrer, protocol, imports) {
|
|
599
595
|
module._type = constants.types.ADDON
|
|
600
596
|
module._protocol = protocol
|
|
601
597
|
module._imports = imports
|
|
602
|
-
|
|
603
|
-
module.exports = process.addon(module.filename)
|
|
598
|
+
module._exports = Addon.load(module._filename)
|
|
604
599
|
}
|
|
605
600
|
|
|
606
601
|
exports._extensions['.node'] = function (module, source, referrer, protocol, imports) {
|
|
607
602
|
module._type = constants.types.ADDON
|
|
608
603
|
module._protocol = protocol
|
|
609
604
|
module._imports = imports
|
|
610
|
-
|
|
611
|
-
module.exports = process.addon(module.filename)
|
|
605
|
+
module._exports = Addon.load(module._filename)
|
|
612
606
|
}
|
|
613
607
|
|
|
614
608
|
exports._extensions['.bundle'] = function (module, source, referrer, protocol, imports) {
|
|
615
609
|
if (typeof source === 'string') source = Buffer.from(source)
|
|
616
610
|
|
|
617
|
-
const bundle = this._bundleFor(module.
|
|
611
|
+
const bundle = this._bundleFor(module._filename, protocol, source)
|
|
618
612
|
|
|
619
613
|
module._type = constants.types.BUNDLE
|
|
620
614
|
module._protocol = protocol
|
|
621
615
|
module._imports = imports
|
|
622
|
-
|
|
623
|
-
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
|
|
624
617
|
}
|
|
625
618
|
|
|
626
619
|
exports._protocols['file:'] = new Protocol({
|
package/lib/constants.js
CHANGED