bare-module 2.5.1 → 2.5.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/binding.c +20 -59
- package/index.js +231 -194
- package/package.json +1 -1
package/binding.c
CHANGED
|
@@ -596,66 +596,27 @@ err:
|
|
|
596
596
|
|
|
597
597
|
static js_value_t *
|
|
598
598
|
init (js_env_t *env, js_value_t *exports) {
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
{
|
|
605
|
-
js_value_t *fn;
|
|
606
|
-
js_create_function(env, "destroy", -1, bare_module_destroy, NULL, &fn);
|
|
607
|
-
js_set_named_property(env, exports, "destroy", fn);
|
|
608
|
-
}
|
|
609
|
-
{
|
|
610
|
-
js_value_t *fn;
|
|
611
|
-
js_create_function(env, "createFunction", -1, bare_module_create_function, NULL, &fn);
|
|
612
|
-
js_set_named_property(env, exports, "createFunction", fn);
|
|
613
|
-
}
|
|
614
|
-
{
|
|
615
|
-
js_value_t *fn;
|
|
616
|
-
js_create_function(env, "createModule", -1, bare_module_create_module, NULL, &fn);
|
|
617
|
-
js_set_named_property(env, exports, "createModule", fn);
|
|
618
|
-
}
|
|
619
|
-
{
|
|
620
|
-
js_value_t *fn;
|
|
621
|
-
js_create_function(env, "createSyntheticModule", -1, bare_module_create_synthetic_module, NULL, &fn);
|
|
622
|
-
js_set_named_property(env, exports, "createSyntheticModule", fn);
|
|
623
|
-
}
|
|
624
|
-
{
|
|
625
|
-
js_value_t *fn;
|
|
626
|
-
js_create_function(env, "deleteModule", -1, bare_module_delete_module, NULL, &fn);
|
|
627
|
-
js_set_named_property(env, exports, "deleteModule", fn);
|
|
628
|
-
}
|
|
629
|
-
{
|
|
630
|
-
js_value_t *fn;
|
|
631
|
-
js_create_function(env, "setExport", -1, bare_module_set_export, NULL, &fn);
|
|
632
|
-
js_set_named_property(env, exports, "setExport", fn);
|
|
633
|
-
}
|
|
634
|
-
{
|
|
635
|
-
js_value_t *fn;
|
|
636
|
-
js_create_function(env, "runModule", -1, bare_module_run_module, NULL, &fn);
|
|
637
|
-
js_set_named_property(env, exports, "runModule", fn);
|
|
638
|
-
}
|
|
639
|
-
{
|
|
640
|
-
js_value_t *fn;
|
|
641
|
-
js_create_function(env, "getNamespace", -1, bare_module_get_namespace, NULL, &fn);
|
|
642
|
-
js_set_named_property(env, exports, "getNamespace", fn);
|
|
643
|
-
}
|
|
644
|
-
{
|
|
645
|
-
js_value_t *fn;
|
|
646
|
-
js_create_function(env, "exists", -1, bare_module_exists, NULL, &fn);
|
|
647
|
-
js_set_named_property(env, exports, "exists", fn);
|
|
648
|
-
}
|
|
649
|
-
{
|
|
650
|
-
js_value_t *fn;
|
|
651
|
-
js_create_function(env, "realpath", -1, bare_module_realpath, NULL, &fn);
|
|
652
|
-
js_set_named_property(env, exports, "realpath", fn);
|
|
653
|
-
}
|
|
654
|
-
{
|
|
655
|
-
js_value_t *fn;
|
|
656
|
-
js_create_function(env, "read", -1, bare_module_read, NULL, &fn);
|
|
657
|
-
js_set_named_property(env, exports, "read", fn);
|
|
599
|
+
#define V(name, fn) \
|
|
600
|
+
{ \
|
|
601
|
+
js_value_t *val; \
|
|
602
|
+
js_create_function(env, name, -1, fn, NULL, &val); \
|
|
603
|
+
js_set_named_property(env, exports, name, val); \
|
|
658
604
|
}
|
|
605
|
+
V("init", bare_module_init)
|
|
606
|
+
V("destroy", bare_module_destroy)
|
|
607
|
+
|
|
608
|
+
V("createFunction", bare_module_create_function)
|
|
609
|
+
V("createModule", bare_module_create_module)
|
|
610
|
+
V("createSyntheticModule", bare_module_create_synthetic_module)
|
|
611
|
+
V("deleteModule", bare_module_delete_module)
|
|
612
|
+
V("setExport", bare_module_set_export)
|
|
613
|
+
V("runModule", bare_module_run_module)
|
|
614
|
+
V("getNamespace", bare_module_get_namespace)
|
|
615
|
+
|
|
616
|
+
V("exists", bare_module_exists)
|
|
617
|
+
V("realpath", bare_module_realpath)
|
|
618
|
+
V("read", bare_module_read)
|
|
619
|
+
#undef V
|
|
659
620
|
|
|
660
621
|
return exports;
|
|
661
622
|
}
|
package/index.js
CHANGED
|
@@ -17,21 +17,13 @@ const Module = module.exports = exports = class Module {
|
|
|
17
17
|
this._exports = null
|
|
18
18
|
this._imports = null
|
|
19
19
|
this._builtins = null
|
|
20
|
-
this.
|
|
20
|
+
this._conditions = null
|
|
21
21
|
this._protocol = null
|
|
22
22
|
this._handle = null
|
|
23
23
|
|
|
24
24
|
Module._modules.add(this)
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
get type () {
|
|
28
|
-
return this._type
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
get defaultType () {
|
|
32
|
-
return this._defaultType
|
|
33
|
-
}
|
|
34
|
-
|
|
35
27
|
get filename () {
|
|
36
28
|
return this._filename
|
|
37
29
|
}
|
|
@@ -40,6 +32,14 @@ const Module = module.exports = exports = class Module {
|
|
|
40
32
|
return path.dirname(this._filename)
|
|
41
33
|
}
|
|
42
34
|
|
|
35
|
+
get type () {
|
|
36
|
+
return this._type
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
get defaultType () {
|
|
40
|
+
return this._defaultType
|
|
41
|
+
}
|
|
42
|
+
|
|
43
43
|
get main () {
|
|
44
44
|
return this._main
|
|
45
45
|
}
|
|
@@ -52,6 +52,18 @@ const Module = module.exports = exports = class Module {
|
|
|
52
52
|
this._exports = value
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
+
get imports () {
|
|
56
|
+
return this._imports
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
get builtins () {
|
|
60
|
+
return this._builtins
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
get conditions () {
|
|
64
|
+
return Array.from(this._conditions)
|
|
65
|
+
}
|
|
66
|
+
|
|
55
67
|
get protocol () {
|
|
56
68
|
return this._protocol
|
|
57
69
|
}
|
|
@@ -81,12 +93,15 @@ const Module = module.exports = exports = class Module {
|
|
|
81
93
|
return {
|
|
82
94
|
__proto__: { constructor: Module },
|
|
83
95
|
|
|
84
|
-
type: this.type,
|
|
85
|
-
defaultType: this.defaultType,
|
|
86
96
|
filename: this.filename,
|
|
87
97
|
dirname: this.dirname,
|
|
98
|
+
type: this.type,
|
|
99
|
+
defaultType: this.defaultType,
|
|
88
100
|
main: this.main,
|
|
89
|
-
exports: this.exports
|
|
101
|
+
exports: this.exports,
|
|
102
|
+
imports: this.imports,
|
|
103
|
+
builtins: this.builtins,
|
|
104
|
+
conditions: this.conditions
|
|
90
105
|
}
|
|
91
106
|
}
|
|
92
107
|
|
|
@@ -95,24 +110,19 @@ const Module = module.exports = exports = class Module {
|
|
|
95
110
|
static _cache = Object.create(null)
|
|
96
111
|
static _bundles = Object.create(null)
|
|
97
112
|
static _modules = new Set()
|
|
113
|
+
static _conditions = ['import', 'require', 'bare', 'node']
|
|
98
114
|
|
|
99
115
|
static _handle = binding.init(this, this._onimport, this._onevaluate, this._onmeta)
|
|
100
116
|
|
|
101
117
|
static _onimport (specifier, assertions, referrerFilename, dynamic) {
|
|
102
118
|
const referrer = this._cache[referrerFilename]
|
|
103
119
|
|
|
104
|
-
|
|
120
|
+
const protocol = this._protocolFor(specifier, referrer._protocol)
|
|
105
121
|
|
|
106
|
-
|
|
107
|
-
protocol
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
protocol,
|
|
111
|
-
referrer
|
|
112
|
-
})
|
|
113
|
-
} else {
|
|
114
|
-
specifier = this.resolve(specifier)
|
|
115
|
-
}
|
|
122
|
+
specifier = this.resolve(specifier, path.dirname(referrer._filename), {
|
|
123
|
+
protocol,
|
|
124
|
+
referrer
|
|
125
|
+
})
|
|
116
126
|
|
|
117
127
|
let type
|
|
118
128
|
|
|
@@ -146,29 +156,31 @@ const Module = module.exports = exports = class Module {
|
|
|
146
156
|
}
|
|
147
157
|
|
|
148
158
|
static _onmeta (specifier, meta) {
|
|
159
|
+
const self = Module
|
|
160
|
+
|
|
149
161
|
const module = this._cache[specifier]
|
|
150
162
|
|
|
151
163
|
const referrer = module
|
|
152
164
|
const dirname = path.dirname(module._filename)
|
|
153
165
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
166
|
+
meta.url = module._filename
|
|
167
|
+
meta.main = module._main === module
|
|
168
|
+
meta.resolve = resolve
|
|
169
|
+
meta.addon = addon
|
|
170
|
+
|
|
171
|
+
function resolve (specifier) {
|
|
172
|
+
return self.resolve(specifier, dirname, {
|
|
173
|
+
protocol: self._protocolFor(specifier, module._protocol),
|
|
157
174
|
imports: module._imports,
|
|
158
175
|
referrer
|
|
159
176
|
})
|
|
160
177
|
}
|
|
161
178
|
|
|
162
|
-
|
|
179
|
+
function addon (specifier = '.') {
|
|
163
180
|
return Bare.Addon.load(Bare.Addon.resolve(specifier, dirname, {
|
|
164
181
|
referrer
|
|
165
182
|
}))
|
|
166
183
|
}
|
|
167
|
-
|
|
168
|
-
meta.url = module._filename
|
|
169
|
-
meta.main = module._main === module
|
|
170
|
-
meta.resolve = resolve
|
|
171
|
-
meta.addon = addon
|
|
172
184
|
}
|
|
173
185
|
|
|
174
186
|
static Protocol = Protocol
|
|
@@ -179,57 +191,77 @@ const Module = module.exports = exports = class Module {
|
|
|
179
191
|
return this._cache
|
|
180
192
|
}
|
|
181
193
|
|
|
194
|
+
// For Node.js compatibility
|
|
195
|
+
static get builtinModules () {
|
|
196
|
+
return []
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// For Node.js compatibility
|
|
200
|
+
static isBuiltin () {
|
|
201
|
+
return false
|
|
202
|
+
}
|
|
203
|
+
|
|
182
204
|
static createRequire (filename, opts = {}) {
|
|
183
|
-
const
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
205
|
+
const self = Module
|
|
206
|
+
|
|
207
|
+
let {
|
|
208
|
+
referrer = null,
|
|
209
|
+
protocol = self._protocolFor(filename, referrer ? referrer._protocol : self._protocols['file:']),
|
|
210
|
+
imports = referrer ? referrer._imports : null,
|
|
211
|
+
builtins = referrer ? referrer._builtins : null,
|
|
212
|
+
conditions = referrer ? referrer._conditions : self._conditions,
|
|
213
|
+
main = referrer ? referrer._main : null,
|
|
214
|
+
defaultType = referrer ? referrer._defaultType : constants.types.SCRIPT,
|
|
215
|
+
type = constants.types.SCRIPT
|
|
189
216
|
} = opts
|
|
190
217
|
|
|
191
218
|
const module = new Module(filename)
|
|
192
219
|
|
|
220
|
+
module._main = main || module
|
|
193
221
|
module._type = type
|
|
194
222
|
module._defaultType = defaultType
|
|
223
|
+
module._protocol = protocol
|
|
195
224
|
module._imports = imports
|
|
196
225
|
module._builtins = builtins
|
|
197
|
-
module.
|
|
226
|
+
module._conditions = conditions
|
|
227
|
+
|
|
228
|
+
referrer = module
|
|
198
229
|
|
|
199
|
-
const referrer = module
|
|
200
230
|
const dirname = path.dirname(module._filename)
|
|
201
231
|
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
232
|
+
require.main = module._main
|
|
233
|
+
require.cache = self._cache
|
|
234
|
+
require.resolve = resolve
|
|
235
|
+
require.addon = addon
|
|
236
|
+
|
|
237
|
+
return require
|
|
238
|
+
|
|
239
|
+
function require (specifier) {
|
|
240
|
+
const module = self.load(resolve(specifier), {
|
|
241
|
+
protocol: self._protocolFor(specifier, protocol),
|
|
205
242
|
referrer
|
|
206
243
|
})
|
|
244
|
+
|
|
245
|
+
return module._exports
|
|
207
246
|
}
|
|
208
247
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
protocol:
|
|
248
|
+
function resolve (specifier) {
|
|
249
|
+
return self.resolve(specifier, dirname, {
|
|
250
|
+
protocol: self._protocolFor(specifier, protocol),
|
|
212
251
|
referrer
|
|
213
252
|
})
|
|
214
|
-
|
|
215
|
-
return module._exports
|
|
216
253
|
}
|
|
217
254
|
|
|
218
|
-
|
|
255
|
+
function addon (specifier = '.') {
|
|
219
256
|
return Bare.Addon.load(Bare.Addon.resolve(specifier, dirname, {
|
|
220
257
|
referrer
|
|
221
258
|
}))
|
|
222
259
|
}
|
|
223
|
-
|
|
224
|
-
require.main = module._main
|
|
225
|
-
require.cache = this._cache
|
|
226
|
-
require.resolve = resolve
|
|
227
|
-
require.addon = addon
|
|
228
|
-
|
|
229
|
-
return require
|
|
230
260
|
}
|
|
231
261
|
|
|
232
262
|
static load (specifier, source = null, opts = {}) {
|
|
263
|
+
const self = Module
|
|
264
|
+
|
|
233
265
|
if (!ArrayBuffer.isView(source) && typeof source !== 'string' && source !== null) {
|
|
234
266
|
opts = source
|
|
235
267
|
source = null
|
|
@@ -238,23 +270,24 @@ const Module = module.exports = exports = class Module {
|
|
|
238
270
|
let {
|
|
239
271
|
dynamic = false,
|
|
240
272
|
referrer = null,
|
|
241
|
-
protocol =
|
|
273
|
+
protocol = self._protocolFor(specifier, referrer ? referrer._protocol : self._protocols['file:']),
|
|
242
274
|
imports = referrer ? referrer._imports : null,
|
|
243
275
|
builtins = referrer ? referrer._builtins : null,
|
|
276
|
+
conditions = referrer ? referrer._conditions : self._conditions,
|
|
244
277
|
main = referrer ? referrer._main : null,
|
|
245
278
|
defaultType = referrer ? referrer._defaultType : 0,
|
|
246
279
|
type = 0
|
|
247
280
|
} = opts
|
|
248
281
|
|
|
249
|
-
if (
|
|
282
|
+
if (self._cache[specifier]) return self._transform(self._cache[specifier], referrer, dynamic)
|
|
250
283
|
|
|
251
|
-
const bundle =
|
|
284
|
+
const bundle = self._bundleFor(path.dirname(specifier), protocol)
|
|
252
285
|
|
|
253
286
|
if (bundle) {
|
|
254
287
|
protocol = new Protocol({
|
|
255
288
|
imports: bundle.imports,
|
|
256
289
|
|
|
257
|
-
preresolve:
|
|
290
|
+
preresolve: self._protocols['file:'].preresolve,
|
|
258
291
|
|
|
259
292
|
exists (filename) {
|
|
260
293
|
return bundle.exists(filename)
|
|
@@ -266,7 +299,7 @@ const Module = module.exports = exports = class Module {
|
|
|
266
299
|
})
|
|
267
300
|
}
|
|
268
301
|
|
|
269
|
-
const module =
|
|
302
|
+
const module = self._cache[specifier] = new Module(specifier)
|
|
270
303
|
|
|
271
304
|
if (builtins && specifier in builtins) {
|
|
272
305
|
module._exports = builtins[specifier]
|
|
@@ -276,12 +309,12 @@ const Module = module.exports = exports = class Module {
|
|
|
276
309
|
module._protocol = protocol
|
|
277
310
|
module._imports = imports
|
|
278
311
|
module._builtins = builtins
|
|
279
|
-
module.
|
|
312
|
+
module._conditions = conditions
|
|
280
313
|
|
|
281
|
-
let extension =
|
|
314
|
+
let extension = self._extensionFor(type) || path.extname(specifier)
|
|
282
315
|
|
|
283
|
-
if (extension in
|
|
284
|
-
if (defaultType) extension =
|
|
316
|
+
if (extension in self._extensions === false) {
|
|
317
|
+
if (defaultType) extension = self._extensionFor(defaultType) || '.js'
|
|
285
318
|
else extension = '.js'
|
|
286
319
|
}
|
|
287
320
|
|
|
@@ -289,36 +322,15 @@ const Module = module.exports = exports = class Module {
|
|
|
289
322
|
throw errors.INVALID_BUNDLE_EXTENSION(`Invalid extension for bundle '${specifier}'`)
|
|
290
323
|
}
|
|
291
324
|
|
|
292
|
-
|
|
325
|
+
self._extensions[extension](module, source, referrer)
|
|
293
326
|
}
|
|
294
327
|
|
|
295
|
-
return
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
static _loadPackageManifest (dirname, protocol, opts = {}) {
|
|
299
|
-
const {
|
|
300
|
-
traverse = true
|
|
301
|
-
} = opts
|
|
302
|
-
|
|
303
|
-
do {
|
|
304
|
-
const specifier = path.join(dirname, 'package.json')
|
|
305
|
-
|
|
306
|
-
if (this._cache[specifier]) return this._cache[specifier]._exports
|
|
307
|
-
|
|
308
|
-
if (protocol.exists(specifier)) {
|
|
309
|
-
try {
|
|
310
|
-
return this.load(specifier, { protocol })._exports
|
|
311
|
-
} catch {}
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
if (traverse) dirname = path.dirname(dirname)
|
|
315
|
-
else break
|
|
316
|
-
} while (dirname !== path.sep && dirname !== '.')
|
|
317
|
-
|
|
318
|
-
return {}
|
|
328
|
+
return self._transform(module, referrer, dynamic)
|
|
319
329
|
}
|
|
320
330
|
|
|
321
331
|
static resolve (specifier, dirname = os.cwd(), opts = {}) {
|
|
332
|
+
const self = Module
|
|
333
|
+
|
|
322
334
|
if (typeof dirname !== 'string') {
|
|
323
335
|
opts = dirname
|
|
324
336
|
dirname = os.cwd()
|
|
@@ -326,19 +338,19 @@ const Module = module.exports = exports = class Module {
|
|
|
326
338
|
|
|
327
339
|
let {
|
|
328
340
|
referrer = null,
|
|
329
|
-
protocol = referrer ? referrer._protocol :
|
|
341
|
+
protocol = referrer ? referrer._protocol : self._protocols['file:'],
|
|
330
342
|
imports = referrer ? referrer._imports : null,
|
|
331
343
|
builtins = referrer ? referrer._builtins : null,
|
|
332
|
-
conditions =
|
|
344
|
+
conditions = referrer ? referrer._conditions : self._conditions
|
|
333
345
|
} = opts
|
|
334
346
|
|
|
335
|
-
const bundle =
|
|
347
|
+
const bundle = self._bundleFor(path.dirname(specifier), protocol)
|
|
336
348
|
|
|
337
349
|
if (bundle) {
|
|
338
350
|
protocol = new Protocol({
|
|
339
351
|
imports: bundle.imports,
|
|
340
352
|
|
|
341
|
-
preresolve:
|
|
353
|
+
preresolve: self._protocols['file:'].preresolve,
|
|
342
354
|
|
|
343
355
|
exists (filename) {
|
|
344
356
|
return bundle.exists(filename)
|
|
@@ -350,7 +362,7 @@ const Module = module.exports = exports = class Module {
|
|
|
350
362
|
})
|
|
351
363
|
}
|
|
352
364
|
|
|
353
|
-
const [resolved = null] =
|
|
365
|
+
const [resolved = null] = self._resolve(specifier, dirname, protocol, imports, builtins, conditions)
|
|
354
366
|
|
|
355
367
|
if (resolved === null) {
|
|
356
368
|
let msg = `Cannot find module '${specifier}'`
|
|
@@ -363,29 +375,64 @@ const Module = module.exports = exports = class Module {
|
|
|
363
375
|
return protocol.postresolve(resolved, dirname)
|
|
364
376
|
}
|
|
365
377
|
|
|
378
|
+
static _loadPackageManifest (dirname, protocol, opts = {}) {
|
|
379
|
+
const {
|
|
380
|
+
traverse = true
|
|
381
|
+
} = opts
|
|
382
|
+
|
|
383
|
+
do {
|
|
384
|
+
const specifier = path.join(dirname, 'package.json')
|
|
385
|
+
|
|
386
|
+
if (this._cache[specifier]) return this._cache[specifier]
|
|
387
|
+
|
|
388
|
+
if (protocol.exists(specifier)) {
|
|
389
|
+
try {
|
|
390
|
+
return this.load(specifier, { protocol })
|
|
391
|
+
} catch {}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if (traverse) dirname = path.dirname(dirname)
|
|
395
|
+
else break
|
|
396
|
+
} while (dirname !== path.sep && dirname !== '.')
|
|
397
|
+
|
|
398
|
+
return null
|
|
399
|
+
}
|
|
400
|
+
|
|
366
401
|
static * _resolve (specifier, dirname, protocol, imports, builtins, conditions) {
|
|
367
|
-
const
|
|
402
|
+
const pkg = this._loadPackageManifest(dirname, protocol)
|
|
368
403
|
|
|
369
|
-
|
|
370
|
-
specifier,
|
|
371
|
-
conditions,
|
|
372
|
-
[imports, protocol.imports, info.imports]
|
|
373
|
-
)
|
|
404
|
+
const info = (pkg && pkg._exports) || {}
|
|
374
405
|
|
|
375
|
-
|
|
406
|
+
let resolved = specifier
|
|
376
407
|
|
|
377
|
-
|
|
408
|
+
if (info.imports) {
|
|
409
|
+
resolved = this._mapConditionalSpecifier(resolved, conditions, info.imports)
|
|
378
410
|
|
|
379
|
-
|
|
411
|
+
if (resolved) dirname = path.dirname(pkg._filename)
|
|
412
|
+
}
|
|
380
413
|
|
|
381
|
-
if (
|
|
414
|
+
if (protocol.imports) {
|
|
415
|
+
resolved = this._mapConditionalSpecifier(resolved, conditions, protocol.imports) || resolved
|
|
416
|
+
}
|
|
382
417
|
|
|
383
|
-
if (
|
|
384
|
-
|
|
385
|
-
yield * this._resolveDirectory(specifier, protocol, conditions)
|
|
418
|
+
if (imports) {
|
|
419
|
+
resolved = this._mapConditionalSpecifier(resolved, conditions, imports) || resolved
|
|
386
420
|
}
|
|
387
421
|
|
|
388
|
-
|
|
422
|
+
protocol = this._protocolFor(resolved, protocol)
|
|
423
|
+
|
|
424
|
+
resolved = protocol.preresolve(resolved, dirname)
|
|
425
|
+
|
|
426
|
+
yield * protocol.resolve(resolved, dirname, imports)
|
|
427
|
+
|
|
428
|
+
if (builtins && resolved in builtins) yield resolved
|
|
429
|
+
|
|
430
|
+
if (path.isAbsolute(resolved)) {
|
|
431
|
+
yield * this._resolveFile(resolved, protocol)
|
|
432
|
+
yield * this._resolveDirectory(resolved, protocol, conditions)
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
yield * this._resolveNodeModules(resolved, dirname, protocol, conditions)
|
|
389
436
|
}
|
|
390
437
|
|
|
391
438
|
static * _resolveFile (filename, protocol) {
|
|
@@ -408,27 +455,24 @@ const Module = module.exports = exports = class Module {
|
|
|
408
455
|
}
|
|
409
456
|
|
|
410
457
|
static * _resolveDirectory (dirname, protocol, conditions) {
|
|
411
|
-
const
|
|
458
|
+
const pkg = this._loadPackageManifest(dirname, protocol, { traverse: false })
|
|
412
459
|
|
|
413
|
-
|
|
460
|
+
const info = (pkg && pkg._exports) || {}
|
|
461
|
+
|
|
462
|
+
let resolved = null
|
|
414
463
|
|
|
415
464
|
if (info.exports) {
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
[info.exports],
|
|
420
|
-
null // Disable fallback
|
|
421
|
-
)
|
|
422
|
-
|
|
423
|
-
if (specifier) specifier = path.join(dirname, specifier)
|
|
465
|
+
resolved = this._mapConditionalSpecifier('.', conditions, info.exports)
|
|
466
|
+
|
|
467
|
+
if (resolved) resolved = path.join(dirname, resolved)
|
|
424
468
|
else return // Unexported
|
|
425
469
|
} else if (info.main) {
|
|
426
|
-
|
|
470
|
+
resolved = path.join(dirname, info.main)
|
|
427
471
|
}
|
|
428
472
|
|
|
429
|
-
if (
|
|
430
|
-
yield * this._resolveFile(
|
|
431
|
-
yield * this._resolveIndex(
|
|
473
|
+
if (resolved) {
|
|
474
|
+
yield * this._resolveFile(resolved, protocol)
|
|
475
|
+
yield * this._resolveIndex(resolved, protocol)
|
|
432
476
|
}
|
|
433
477
|
|
|
434
478
|
yield * this._resolveIndex(dirname, protocol)
|
|
@@ -441,15 +485,12 @@ const Module = module.exports = exports = class Module {
|
|
|
441
485
|
let resolved = specifier
|
|
442
486
|
|
|
443
487
|
if (name) {
|
|
444
|
-
const
|
|
488
|
+
const pkg = this._loadPackageManifest(path.join(nodeModules, name), protocol, { traverse: false })
|
|
489
|
+
|
|
490
|
+
const info = (pkg && pkg._exports) || {}
|
|
445
491
|
|
|
446
492
|
if (info.exports) {
|
|
447
|
-
resolved = this._mapConditionalSpecifier(
|
|
448
|
-
expansion,
|
|
449
|
-
conditions,
|
|
450
|
-
[info.exports],
|
|
451
|
-
null // Disable fallback
|
|
452
|
-
)
|
|
493
|
+
resolved = this._mapConditionalSpecifier(expansion, conditions, info.exports)
|
|
453
494
|
|
|
454
495
|
if (resolved) resolved = path.join(name, resolved)
|
|
455
496
|
else return // Unexported
|
|
@@ -475,16 +516,16 @@ const Module = module.exports = exports = class Module {
|
|
|
475
516
|
}
|
|
476
517
|
}
|
|
477
518
|
|
|
478
|
-
static _mapConditionalSpecifier (specifier, conditions,
|
|
479
|
-
|
|
519
|
+
static _mapConditionalSpecifier (specifier, conditions, specifierMap) {
|
|
520
|
+
if (typeof specifierMap === 'string') specifierMap = { '.': specifierMap }
|
|
480
521
|
|
|
481
|
-
if (specifier in
|
|
482
|
-
specifier = search(
|
|
522
|
+
if (specifier in specifierMap) {
|
|
523
|
+
specifier = search(specifierMap[specifier])
|
|
483
524
|
} else {
|
|
484
|
-
specifier = search(
|
|
525
|
+
specifier = search(specifierMap)
|
|
485
526
|
}
|
|
486
527
|
|
|
487
|
-
return specifier
|
|
528
|
+
return specifier
|
|
488
529
|
|
|
489
530
|
function search (specifiers) {
|
|
490
531
|
while (true) {
|
|
@@ -506,24 +547,6 @@ const Module = module.exports = exports = class Module {
|
|
|
506
547
|
}
|
|
507
548
|
}
|
|
508
549
|
|
|
509
|
-
static _flattenSpecifierMaps (specifierMaps) {
|
|
510
|
-
const specifiers = Object.create(null)
|
|
511
|
-
|
|
512
|
-
for (let map of specifierMaps) {
|
|
513
|
-
if (typeof map === 'string') map = { '.': map }
|
|
514
|
-
if (map === null || typeof map !== 'object') continue
|
|
515
|
-
|
|
516
|
-
this._mergeSpecifierMaps(specifiers, map)
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
return specifiers
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
static _mergeSpecifierMaps (destination, source) {
|
|
523
|
-
// TODO Do a deep merge
|
|
524
|
-
Object.assign(destination, source)
|
|
525
|
-
}
|
|
526
|
-
|
|
527
550
|
static _extensionFor (type) {
|
|
528
551
|
switch (type) {
|
|
529
552
|
case constants.types.SCRIPT:
|
|
@@ -602,11 +625,11 @@ const Module = module.exports = exports = class Module {
|
|
|
602
625
|
|
|
603
626
|
static _transform (module, referrer = null, dynamic = false) {
|
|
604
627
|
if (dynamic) {
|
|
605
|
-
|
|
628
|
+
this._synthesize(module)
|
|
606
629
|
this._evaluate(module)
|
|
607
630
|
} else if (referrer) {
|
|
608
631
|
if (referrer._type === constants.types.MODULE) {
|
|
609
|
-
|
|
632
|
+
this._synthesize(module)
|
|
610
633
|
} else if (module._type === constants.types.MODULE) {
|
|
611
634
|
this._evaluate(module)
|
|
612
635
|
}
|
|
@@ -632,36 +655,46 @@ const Module = module.exports = exports = class Module {
|
|
|
632
655
|
static _synthesize (module) {
|
|
633
656
|
if ((module._state & constants.states.SYNTHESIZED) !== 0) return
|
|
634
657
|
|
|
635
|
-
|
|
658
|
+
if (module._type !== constants.types.MODULE) {
|
|
659
|
+
const names = ['default']
|
|
636
660
|
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
661
|
+
for (const key of Object.keys(module._exports)) {
|
|
662
|
+
if (key !== 'default') names.push(key)
|
|
663
|
+
}
|
|
640
664
|
|
|
641
|
-
|
|
665
|
+
module._handle = binding.createSyntheticModule(module._filename, names, this._handle)
|
|
666
|
+
}
|
|
642
667
|
|
|
643
668
|
module._state |= constants.states.SYNTHESIZED
|
|
644
669
|
}
|
|
645
670
|
}
|
|
646
671
|
|
|
647
672
|
Module._extensions['.js'] = function (module, source, referrer) {
|
|
673
|
+
const self = Module
|
|
674
|
+
|
|
675
|
+
const protocol = module._protocol
|
|
676
|
+
|
|
677
|
+
const pkg = self._loadPackageManifest(path.dirname(module._filename), protocol)
|
|
678
|
+
|
|
679
|
+
const info = (pkg && pkg._exports) || {}
|
|
680
|
+
|
|
648
681
|
const isESM = (
|
|
649
682
|
// The default type is ES modules.
|
|
650
|
-
(
|
|
683
|
+
(constants.types.MODULE === module._defaultType) ||
|
|
651
684
|
|
|
652
685
|
// The package is explicitly declared as an ES module.
|
|
653
|
-
(
|
|
686
|
+
(info && info.type === 'module') ||
|
|
654
687
|
|
|
655
688
|
// The source is a data: URI and the referrer is itself an ES module.
|
|
656
|
-
(
|
|
689
|
+
(protocol === self._protocols['data:'] && referrer && referrer._type === constants.types.MODULE)
|
|
657
690
|
)
|
|
658
691
|
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
return loader.call(this, module, source, referrer)
|
|
692
|
+
return self._extensions[isESM ? '.mjs' : '.cjs'](module, source, referrer)
|
|
662
693
|
}
|
|
663
694
|
|
|
664
695
|
Module._extensions['.cjs'] = function (module, source, referrer) {
|
|
696
|
+
const self = Module
|
|
697
|
+
|
|
665
698
|
const protocol = module._protocol
|
|
666
699
|
|
|
667
700
|
module._type = constants.types.SCRIPT
|
|
@@ -677,30 +710,8 @@ Module._extensions['.cjs'] = function (module, source, referrer) {
|
|
|
677
710
|
|
|
678
711
|
const dirname = path.dirname(module._filename)
|
|
679
712
|
|
|
680
|
-
const resolve = (specifier) => {
|
|
681
|
-
return this.resolve(specifier, dirname, {
|
|
682
|
-
protocol: this._protocolFor(specifier, protocol),
|
|
683
|
-
referrer
|
|
684
|
-
})
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
const require = (specifier) => {
|
|
688
|
-
const module = this.load(resolve(specifier), {
|
|
689
|
-
protocol: this._protocolFor(specifier, protocol),
|
|
690
|
-
referrer
|
|
691
|
-
})
|
|
692
|
-
|
|
693
|
-
return module._exports
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
const addon = (specifier = '.') => {
|
|
697
|
-
return Bare.Addon.load(Bare.Addon.resolve(specifier, dirname, {
|
|
698
|
-
referrer
|
|
699
|
-
}))
|
|
700
|
-
}
|
|
701
|
-
|
|
702
713
|
require.main = module._main
|
|
703
|
-
require.cache =
|
|
714
|
+
require.cache = self._cache
|
|
704
715
|
require.resolve = resolve
|
|
705
716
|
require.addon = addon
|
|
706
717
|
|
|
@@ -713,10 +724,34 @@ Module._extensions['.cjs'] = function (module, source, referrer) {
|
|
|
713
724
|
module._filename,
|
|
714
725
|
path.dirname(module._filename)
|
|
715
726
|
)
|
|
727
|
+
|
|
728
|
+
function require (specifier) {
|
|
729
|
+
const module = self.load(resolve(specifier), {
|
|
730
|
+
protocol: self._protocolFor(specifier, protocol),
|
|
731
|
+
referrer
|
|
732
|
+
})
|
|
733
|
+
|
|
734
|
+
return module._exports
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
function resolve (specifier) {
|
|
738
|
+
return self.resolve(specifier, dirname, {
|
|
739
|
+
protocol: self._protocolFor(specifier, protocol),
|
|
740
|
+
referrer
|
|
741
|
+
})
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
function addon (specifier = '.') {
|
|
745
|
+
return Bare.Addon.load(Bare.Addon.resolve(specifier, dirname, {
|
|
746
|
+
referrer
|
|
747
|
+
}))
|
|
748
|
+
}
|
|
716
749
|
}
|
|
717
750
|
}
|
|
718
751
|
|
|
719
752
|
Module._extensions['.mjs'] = function (module, source, referrer) {
|
|
753
|
+
const self = Module
|
|
754
|
+
|
|
720
755
|
const protocol = module._protocol
|
|
721
756
|
|
|
722
757
|
module._type = constants.types.MODULE
|
|
@@ -728,7 +763,7 @@ Module._extensions['.mjs'] = function (module, source, referrer) {
|
|
|
728
763
|
|
|
729
764
|
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
730
765
|
|
|
731
|
-
module._handle = binding.createModule(module._filename, source, 0,
|
|
766
|
+
module._handle = binding.createModule(module._filename, source, 0, self._handle)
|
|
732
767
|
}
|
|
733
768
|
}
|
|
734
769
|
|
|
@@ -761,6 +796,8 @@ Module._extensions['.node'] = function (module, source, referrer) {
|
|
|
761
796
|
}
|
|
762
797
|
|
|
763
798
|
Module._extensions['.bundle'] = function (module, source, referrer) {
|
|
799
|
+
const self = Module
|
|
800
|
+
|
|
764
801
|
const protocol = module._protocol
|
|
765
802
|
|
|
766
803
|
module._type = constants.types.BUNDLE
|
|
@@ -769,16 +806,16 @@ Module._extensions['.bundle'] = function (module, source, referrer) {
|
|
|
769
806
|
|
|
770
807
|
if (typeof source === 'string') source = Buffer.from(source)
|
|
771
808
|
|
|
772
|
-
const bundle =
|
|
809
|
+
const bundle = self._bundleFor(module._filename, protocol, source)
|
|
773
810
|
|
|
774
|
-
module._exports =
|
|
811
|
+
module._exports = self.load(bundle.main, bundle.read(bundle.main), { protocol, referrer })._exports
|
|
775
812
|
}
|
|
776
813
|
|
|
777
814
|
Module._protocols['file:'] = new Protocol({
|
|
778
815
|
preresolve (specifier, dirname) {
|
|
779
816
|
specifier = specifier.replace(/^file:/, '')
|
|
780
817
|
|
|
781
|
-
if (specifier
|
|
818
|
+
if (/^\.(\/|\\)/.test(specifier)) specifier = path.join(dirname, specifier)
|
|
782
819
|
else if (path.isAbsolute(specifier)) specifier = path.normalize(specifier)
|
|
783
820
|
|
|
784
821
|
return specifier
|