bare-module 2.8.1 → 3.0.0
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/README.md +5 -3
- package/index.js +114 -220
- package/package.json +3 -4
package/README.md
CHANGED
|
@@ -22,7 +22,7 @@ const Module = require('bare-module')
|
|
|
22
22
|
|
|
23
23
|
#### `Module.cache`
|
|
24
24
|
|
|
25
|
-
#### `const
|
|
25
|
+
#### `const url = Module.resolve(specifier, parentURL[, options])`
|
|
26
26
|
|
|
27
27
|
Options include:
|
|
28
28
|
|
|
@@ -31,7 +31,7 @@ Options include:
|
|
|
31
31
|
}
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
-
#### `const module = Module.load(
|
|
34
|
+
#### `const module = Module.load(url[, source][, options])`
|
|
35
35
|
|
|
36
36
|
Options include:
|
|
37
37
|
|
|
@@ -40,6 +40,8 @@ Options include:
|
|
|
40
40
|
}
|
|
41
41
|
```
|
|
42
42
|
|
|
43
|
+
#### `module.url`
|
|
44
|
+
|
|
43
45
|
#### `module.filename`
|
|
44
46
|
|
|
45
47
|
#### `module.dirname`
|
|
@@ -64,7 +66,7 @@ Options include:
|
|
|
64
66
|
|
|
65
67
|
### Custom `require()`
|
|
66
68
|
|
|
67
|
-
#### `const require = Module.createRequire(
|
|
69
|
+
#### `const require = Module.createRequire(url[, options])`
|
|
68
70
|
|
|
69
71
|
### Protocols
|
|
70
72
|
|
package/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/* global Bare */
|
|
2
2
|
const path = require('bare-path')
|
|
3
|
-
const os = require('bare-os')
|
|
4
3
|
const url = require('bare-url')
|
|
5
4
|
const resolve = require('bare-module-resolve')
|
|
6
5
|
const Bundle = require('bare-bundle')
|
|
@@ -10,8 +9,8 @@ const errors = require('./lib/errors')
|
|
|
10
9
|
const binding = require('./binding')
|
|
11
10
|
|
|
12
11
|
const Module = module.exports = exports = class Module {
|
|
13
|
-
constructor (
|
|
14
|
-
this.
|
|
12
|
+
constructor (url) {
|
|
13
|
+
this._url = url
|
|
15
14
|
this._state = 0
|
|
16
15
|
this._type = 0
|
|
17
16
|
this._defaultType = this._type
|
|
@@ -28,12 +27,16 @@ const Module = module.exports = exports = class Module {
|
|
|
28
27
|
Module._modules.add(this)
|
|
29
28
|
}
|
|
30
29
|
|
|
30
|
+
get url () {
|
|
31
|
+
return this._url
|
|
32
|
+
}
|
|
33
|
+
|
|
31
34
|
get filename () {
|
|
32
|
-
return this.
|
|
35
|
+
return url.fileURLToPath(this._url)
|
|
33
36
|
}
|
|
34
37
|
|
|
35
38
|
get dirname () {
|
|
36
|
-
return path.dirname(this.
|
|
39
|
+
return path.dirname(url.fileURLToPath(this._url))
|
|
37
40
|
}
|
|
38
41
|
|
|
39
42
|
get type () {
|
|
@@ -97,25 +100,13 @@ const Module = module.exports = exports = class Module {
|
|
|
97
100
|
Module._modules.delete(this)
|
|
98
101
|
}
|
|
99
102
|
|
|
100
|
-
_transform (
|
|
101
|
-
if (
|
|
103
|
+
_transform (isImport, isDynamicImport) {
|
|
104
|
+
if (isDynamicImport) {
|
|
102
105
|
this._synthesize()
|
|
103
106
|
this._evaluate()
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
if (referrer) {
|
|
109
|
-
if (referrer._type === constants.types.MODULE) {
|
|
110
|
-
this._synthesize()
|
|
111
|
-
} else if (this._type === constants.types.MODULE) {
|
|
112
|
-
this._evaluate()
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
return this
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
if (this._type === constants.types.MODULE) {
|
|
107
|
+
} else if (isImport) {
|
|
108
|
+
this._synthesize()
|
|
109
|
+
} else if (this._type === constants.types.MODULE) {
|
|
119
110
|
this._evaluate()
|
|
120
111
|
}
|
|
121
112
|
|
|
@@ -144,7 +135,7 @@ const Module = module.exports = exports = class Module {
|
|
|
144
135
|
if (key !== 'default') names.push(key)
|
|
145
136
|
}
|
|
146
137
|
|
|
147
|
-
this._handle = binding.createSyntheticModule(this.
|
|
138
|
+
this._handle = binding.createSyntheticModule(this._url.href, names, Module._handle)
|
|
148
139
|
}
|
|
149
140
|
|
|
150
141
|
this._state |= constants.states.SYNTHESIZED
|
|
@@ -154,8 +145,7 @@ const Module = module.exports = exports = class Module {
|
|
|
154
145
|
return {
|
|
155
146
|
__proto__: { constructor: Module },
|
|
156
147
|
|
|
157
|
-
|
|
158
|
-
dirname: this.dirname,
|
|
148
|
+
url: this.url,
|
|
159
149
|
type: this.type,
|
|
160
150
|
defaultType: this.defaultType,
|
|
161
151
|
main: this.main,
|
|
@@ -171,16 +161,17 @@ const Module = module.exports = exports = class Module {
|
|
|
171
161
|
static _protocols = Object.create(null)
|
|
172
162
|
static _cache = Object.create(null)
|
|
173
163
|
static _modules = new Set()
|
|
174
|
-
static _conditions = ['
|
|
164
|
+
static _conditions = ['bare', 'node']
|
|
175
165
|
|
|
176
166
|
static _handle = binding.init(this, this._onimport, this._onevaluate, this._onmeta)
|
|
177
167
|
|
|
178
|
-
static _onimport (specifier, assertions,
|
|
179
|
-
const referrer = this._cache[
|
|
180
|
-
|
|
181
|
-
const protocol = this._protocolFor(specifier, referrer._protocol)
|
|
168
|
+
static _onimport (specifier, assertions, referrerURL, isDynamicImport) {
|
|
169
|
+
const referrer = this._cache[referrerURL]
|
|
182
170
|
|
|
183
|
-
|
|
171
|
+
const url = this.resolve(specifier, referrer._url, {
|
|
172
|
+
isImport: true,
|
|
173
|
+
referrer
|
|
174
|
+
})
|
|
184
175
|
|
|
185
176
|
let type
|
|
186
177
|
|
|
@@ -193,10 +184,10 @@ const Module = module.exports = exports = class Module {
|
|
|
193
184
|
break
|
|
194
185
|
}
|
|
195
186
|
|
|
196
|
-
const module = this.load(
|
|
197
|
-
|
|
187
|
+
const module = this.load(url, {
|
|
188
|
+
isImport: true,
|
|
189
|
+
isDynamicImport,
|
|
198
190
|
referrer,
|
|
199
|
-
dynamic,
|
|
200
191
|
type
|
|
201
192
|
})
|
|
202
193
|
|
|
@@ -222,16 +213,13 @@ const Module = module.exports = exports = class Module {
|
|
|
222
213
|
|
|
223
214
|
addon.host = Bare.Addon.host
|
|
224
215
|
|
|
225
|
-
meta.url = module.
|
|
216
|
+
meta.url = module._url.href
|
|
226
217
|
meta.main = module._main === module
|
|
227
218
|
meta.resolve = resolve
|
|
228
219
|
meta.addon = addon
|
|
229
220
|
|
|
230
221
|
function resolve (specifier) {
|
|
231
|
-
return self.resolve(specifier, {
|
|
232
|
-
protocol: self._protocolFor(specifier, module._protocol),
|
|
233
|
-
referrer
|
|
234
|
-
})
|
|
222
|
+
return self.resolve(specifier, referrer._url, { referrer }).href
|
|
235
223
|
}
|
|
236
224
|
|
|
237
225
|
function addon (specifier = '.') {
|
|
@@ -257,12 +245,14 @@ const Module = module.exports = exports = class Module {
|
|
|
257
245
|
return false
|
|
258
246
|
}
|
|
259
247
|
|
|
260
|
-
static createRequire (
|
|
248
|
+
static createRequire (url, opts = {}) {
|
|
261
249
|
const self = Module
|
|
262
250
|
|
|
251
|
+
if (typeof url === 'string') url = new URL(url, 'file:///')
|
|
252
|
+
|
|
263
253
|
let {
|
|
264
254
|
referrer = null,
|
|
265
|
-
protocol =
|
|
255
|
+
protocol = referrer ? referrer._protocol : self._protocols['file:'],
|
|
266
256
|
imports = referrer ? referrer._imports : null,
|
|
267
257
|
resolutions = referrer ? referrer._resolutions : null,
|
|
268
258
|
builtins = referrer ? referrer._builtins : null,
|
|
@@ -272,7 +262,7 @@ const Module = module.exports = exports = class Module {
|
|
|
272
262
|
type = constants.types.SCRIPT
|
|
273
263
|
} = opts
|
|
274
264
|
|
|
275
|
-
const module = new Module(
|
|
265
|
+
const module = new Module(url)
|
|
276
266
|
|
|
277
267
|
module._main = main || module
|
|
278
268
|
module._type = type
|
|
@@ -295,19 +285,15 @@ const Module = module.exports = exports = class Module {
|
|
|
295
285
|
return require
|
|
296
286
|
|
|
297
287
|
function require (specifier) {
|
|
298
|
-
const
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
})
|
|
288
|
+
const url = self.resolve(specifier, referrer._url, { referrer })
|
|
289
|
+
|
|
290
|
+
const module = self.load(url, { referrer })
|
|
302
291
|
|
|
303
292
|
return module._exports
|
|
304
293
|
}
|
|
305
294
|
|
|
306
295
|
function resolve (specifier) {
|
|
307
|
-
return self.resolve(specifier, {
|
|
308
|
-
protocol: self._protocolFor(specifier, protocol),
|
|
309
|
-
referrer
|
|
310
|
-
})
|
|
296
|
+
return self.resolve(specifier, referrer._url, { referrer }).pathname
|
|
311
297
|
}
|
|
312
298
|
|
|
313
299
|
function addon (specifier = '.') {
|
|
@@ -315,80 +301,71 @@ const Module = module.exports = exports = class Module {
|
|
|
315
301
|
}
|
|
316
302
|
}
|
|
317
303
|
|
|
318
|
-
static load (
|
|
304
|
+
static load (url, source = null, opts = {}) {
|
|
319
305
|
const self = Module
|
|
320
306
|
|
|
321
|
-
if (typeof specifier !== 'string') {
|
|
322
|
-
throw new TypeError(`Specifier must be a string. Received type ${typeof specifier} (${specifier})`)
|
|
323
|
-
}
|
|
324
|
-
|
|
325
307
|
if (!ArrayBuffer.isView(source) && typeof source !== 'string' && source !== null) {
|
|
326
308
|
opts = source
|
|
327
309
|
source = null
|
|
328
310
|
}
|
|
329
311
|
|
|
330
|
-
|
|
331
|
-
|
|
312
|
+
const {
|
|
313
|
+
isImport = false,
|
|
314
|
+
isDynamicImport = false,
|
|
315
|
+
|
|
332
316
|
referrer = null,
|
|
333
317
|
type = 0,
|
|
334
318
|
defaultType = referrer ? referrer._defaultType : 0,
|
|
335
319
|
main = referrer ? referrer._main : null,
|
|
336
|
-
protocol =
|
|
320
|
+
protocol = referrer ? referrer._protocol : self._protocols['file:'],
|
|
337
321
|
imports = referrer ? referrer._imports : null,
|
|
338
322
|
resolutions = referrer ? referrer._resolutions : null,
|
|
339
323
|
builtins = referrer ? referrer._builtins : null,
|
|
340
324
|
conditions = referrer ? referrer._conditions : self._conditions
|
|
341
325
|
} = opts
|
|
342
326
|
|
|
343
|
-
if (self._cache[
|
|
344
|
-
|
|
345
|
-
const bundle = self._bundleFor(path.dirname(specifier), protocol)
|
|
327
|
+
if (self._cache[url.href]) return self._cache[url.href]._transform(isImport, isDynamicImport)
|
|
346
328
|
|
|
347
|
-
|
|
329
|
+
const module = self._cache[url.href] = new Module(url)
|
|
348
330
|
|
|
349
|
-
|
|
331
|
+
switch (url.protocol) {
|
|
332
|
+
case 'builtin:':
|
|
333
|
+
module._exports = builtins[url.pathname]
|
|
334
|
+
break
|
|
350
335
|
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
module._builtins = builtins
|
|
360
|
-
module._conditions = conditions
|
|
336
|
+
default: {
|
|
337
|
+
module._main = main || module
|
|
338
|
+
module._defaultType = defaultType
|
|
339
|
+
module._protocol = protocol
|
|
340
|
+
module._imports = imports
|
|
341
|
+
module._resolutions = resolutions
|
|
342
|
+
module._builtins = builtins
|
|
343
|
+
module._conditions = conditions
|
|
361
344
|
|
|
362
|
-
|
|
345
|
+
let extension = self._extensionFor(type) || path.extname(url.pathname)
|
|
363
346
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
347
|
+
if (extension in self._extensions === false) {
|
|
348
|
+
if (defaultType) extension = self._extensionFor(defaultType) || '.js'
|
|
349
|
+
else extension = '.js'
|
|
350
|
+
}
|
|
368
351
|
|
|
369
|
-
|
|
370
|
-
throw errors.INVALID_BUNDLE_EXTENSION(`Invalid extension for bundle '${specifier}'`)
|
|
352
|
+
self._extensions[extension](module, source, referrer)
|
|
371
353
|
}
|
|
372
|
-
|
|
373
|
-
self._extensions[extension](module, source, referrer)
|
|
374
354
|
}
|
|
375
355
|
|
|
376
|
-
return module._transform(
|
|
356
|
+
return module._transform(isImport, isDynamicImport)
|
|
377
357
|
}
|
|
378
358
|
|
|
379
|
-
static resolve (specifier,
|
|
359
|
+
static resolve (specifier, parentURL, opts = {}) {
|
|
380
360
|
const self = Module
|
|
381
361
|
|
|
382
362
|
if (typeof specifier !== 'string') {
|
|
383
363
|
throw new TypeError(`Specifier must be a string. Received type ${typeof specifier} (${specifier})`)
|
|
384
364
|
}
|
|
385
365
|
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
dirname = null
|
|
389
|
-
}
|
|
366
|
+
const {
|
|
367
|
+
isImport = false,
|
|
390
368
|
|
|
391
|
-
let {
|
|
392
369
|
referrer = null,
|
|
393
370
|
protocol = referrer ? referrer._protocol : self._protocols['file:'],
|
|
394
371
|
imports = referrer ? referrer._imports : null,
|
|
@@ -397,39 +374,14 @@ const Module = module.exports = exports = class Module {
|
|
|
397
374
|
conditions = referrer ? referrer._conditions : self._conditions
|
|
398
375
|
} = opts
|
|
399
376
|
|
|
400
|
-
|
|
401
|
-
else if (typeof dirname !== 'string') dirname = os.cwd()
|
|
402
|
-
|
|
403
|
-
const bundle = self._bundleFor(path.dirname(specifier), protocol)
|
|
377
|
+
const resolved = protocol.preresolve(specifier, parentURL)
|
|
404
378
|
|
|
405
|
-
|
|
379
|
+
const [resolution] = protocol.resolve(specifier, parentURL, imports)
|
|
406
380
|
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
const [resolution] = protocol.resolve(specifier, dirname, imports)
|
|
410
|
-
|
|
411
|
-
if (resolution) return protocol.postresolve(resolution, dirname)
|
|
412
|
-
|
|
413
|
-
const parentURL = url.pathToFileURL(
|
|
414
|
-
referrer
|
|
415
|
-
? referrer._filename
|
|
416
|
-
: dirname[dirname.length - 1] === path.sep
|
|
417
|
-
? dirname
|
|
418
|
-
: dirname + path.sep
|
|
419
|
-
)
|
|
420
|
-
|
|
421
|
-
if (resolutions) {
|
|
422
|
-
const entries = Object.entries(resolutions)
|
|
423
|
-
|
|
424
|
-
resolutions = Object.create(null)
|
|
425
|
-
|
|
426
|
-
for (const [path, imports] of entries) {
|
|
427
|
-
resolutions[url.pathToFileURL(path).href] = imports
|
|
428
|
-
}
|
|
429
|
-
}
|
|
381
|
+
if (resolution) return protocol.postresolve(resolution, parentURL)
|
|
430
382
|
|
|
431
383
|
for (const resolution of resolve(resolved, parentURL, {
|
|
432
|
-
conditions,
|
|
384
|
+
conditions: isImport ? ['import', ...conditions] : ['require', ...conditions],
|
|
433
385
|
imports,
|
|
434
386
|
resolutions,
|
|
435
387
|
builtins: builtins ? Object.keys(builtins) : [],
|
|
@@ -443,13 +395,11 @@ const Module = module.exports = exports = class Module {
|
|
|
443
395
|
]
|
|
444
396
|
}, readPackage)) {
|
|
445
397
|
switch (resolution.protocol) {
|
|
446
|
-
case 'builtin:': return resolution
|
|
398
|
+
case 'builtin:': return resolution
|
|
447
399
|
|
|
448
400
|
case 'file:': {
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
if (protocol.exists(path)) {
|
|
452
|
-
return protocol.postresolve(path, dirname)
|
|
401
|
+
if (protocol.exists(resolution)) {
|
|
402
|
+
return protocol.postresolve(resolution, parentURL)
|
|
453
403
|
}
|
|
454
404
|
}
|
|
455
405
|
}
|
|
@@ -457,15 +407,13 @@ const Module = module.exports = exports = class Module {
|
|
|
457
407
|
|
|
458
408
|
let msg = `Cannot find module '${specifier}'`
|
|
459
409
|
|
|
460
|
-
if (referrer) msg += ` imported from '${referrer.
|
|
410
|
+
if (referrer) msg += ` imported from '${referrer._url.href}'`
|
|
461
411
|
|
|
462
412
|
throw errors.MODULE_NOT_FOUND(msg)
|
|
463
413
|
|
|
464
414
|
function readPackage (packageURL) {
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
if (protocol.exists(path)) {
|
|
468
|
-
return Module.load(path, { protocol })._exports
|
|
415
|
+
if (protocol.exists(packageURL)) {
|
|
416
|
+
return Module.load(packageURL, { protocol })._exports
|
|
469
417
|
}
|
|
470
418
|
|
|
471
419
|
return null
|
|
@@ -488,39 +436,6 @@ const Module = module.exports = exports = class Module {
|
|
|
488
436
|
return null
|
|
489
437
|
}
|
|
490
438
|
}
|
|
491
|
-
|
|
492
|
-
static _protocolFor (specifier, fallback = null) {
|
|
493
|
-
let protocol = fallback
|
|
494
|
-
|
|
495
|
-
const i = specifier.indexOf(':')
|
|
496
|
-
|
|
497
|
-
if (i >= 2) { // Allow drive letters in Windows paths
|
|
498
|
-
const name = specifier.slice(0, i + 1)
|
|
499
|
-
|
|
500
|
-
protocol = this._protocols[name] || fallback
|
|
501
|
-
|
|
502
|
-
if (protocol === null) {
|
|
503
|
-
throw errors.UNKNOWN_PROTOCOL(`Unknown protocol '${name}' in specifier '${specifier}'`)
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
return protocol
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
static _bundleFor (specifier, protocol) {
|
|
511
|
-
let name = specifier
|
|
512
|
-
do {
|
|
513
|
-
if (path.extname(name) === '.bundle') {
|
|
514
|
-
break
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
name = path.dirname(name)
|
|
518
|
-
} while (name !== path.sep && name !== '.')
|
|
519
|
-
|
|
520
|
-
if (path.extname(name) !== '.bundle') return null
|
|
521
|
-
|
|
522
|
-
return Module.load(name, { protocol })
|
|
523
|
-
}
|
|
524
439
|
}
|
|
525
440
|
|
|
526
441
|
Module._extensions['.js'] = function (module, source, referrer) {
|
|
@@ -529,22 +444,18 @@ Module._extensions['.js'] = function (module, source, referrer) {
|
|
|
529
444
|
const protocol = module._protocol
|
|
530
445
|
|
|
531
446
|
let pkg
|
|
532
|
-
let dirname = path.dirname(module._filename)
|
|
533
|
-
do {
|
|
534
|
-
const specifier = path.join(dirname, 'package.json')
|
|
535
447
|
|
|
536
|
-
|
|
537
|
-
|
|
448
|
+
for (const packageURL of resolve.lookupPackageScope(module._url)) {
|
|
449
|
+
if (self._cache[packageURL.href]) {
|
|
450
|
+
pkg = self._cache[packageURL.href]
|
|
538
451
|
break
|
|
539
452
|
}
|
|
540
453
|
|
|
541
|
-
if (protocol.exists(
|
|
542
|
-
pkg = self.load(
|
|
454
|
+
if (protocol.exists(packageURL)) {
|
|
455
|
+
pkg = self.load(packageURL, { protocol })
|
|
543
456
|
break
|
|
544
457
|
}
|
|
545
|
-
|
|
546
|
-
dirname = path.dirname(dirname)
|
|
547
|
-
} while (dirname !== path.sep && dirname !== '.')
|
|
458
|
+
}
|
|
548
459
|
|
|
549
460
|
const info = (pkg && pkg._exports) || {}
|
|
550
461
|
|
|
@@ -553,10 +464,7 @@ Module._extensions['.js'] = function (module, source, referrer) {
|
|
|
553
464
|
(constants.types.MODULE === module._defaultType) ||
|
|
554
465
|
|
|
555
466
|
// The package is explicitly declared as an ES module.
|
|
556
|
-
(info && info.type === 'module')
|
|
557
|
-
|
|
558
|
-
// The source is a data: URI and the referrer is itself an ES module.
|
|
559
|
-
(protocol === self._protocols['data:'] && referrer && referrer._type === constants.types.MODULE)
|
|
467
|
+
(info && info.type === 'module')
|
|
560
468
|
)
|
|
561
469
|
|
|
562
470
|
return self._extensions[isESM ? '.mjs' : '.cjs'](module, source, referrer)
|
|
@@ -570,9 +478,9 @@ Module._extensions['.cjs'] = function (module, source, referrer) {
|
|
|
570
478
|
module._type = constants.types.SCRIPT
|
|
571
479
|
|
|
572
480
|
if (protocol.load) {
|
|
573
|
-
module._exports = protocol.load(module.
|
|
481
|
+
module._exports = protocol.load(module._url)
|
|
574
482
|
} else {
|
|
575
|
-
if (source === null) source = protocol.read(module.
|
|
483
|
+
if (source === null) source = protocol.read(module._url)
|
|
576
484
|
|
|
577
485
|
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
578
486
|
|
|
@@ -587,28 +495,26 @@ Module._extensions['.cjs'] = function (module, source, referrer) {
|
|
|
587
495
|
|
|
588
496
|
module._exports = {}
|
|
589
497
|
|
|
590
|
-
|
|
498
|
+
const filename = url.fileURLToPath(module._url)
|
|
499
|
+
|
|
500
|
+
binding.createFunction(module._url.href, ['require', 'module', 'exports', '__filename', '__dirname'], source, 0)(
|
|
591
501
|
require,
|
|
592
502
|
module,
|
|
593
503
|
module._exports,
|
|
594
|
-
|
|
595
|
-
path.dirname(
|
|
504
|
+
filename,
|
|
505
|
+
path.dirname(filename)
|
|
596
506
|
)
|
|
597
507
|
|
|
598
508
|
function require (specifier) {
|
|
599
|
-
const
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
})
|
|
509
|
+
const url = self.resolve(specifier, referrer._url, { referrer })
|
|
510
|
+
|
|
511
|
+
const module = self.load(url, { referrer })
|
|
603
512
|
|
|
604
513
|
return module._exports
|
|
605
514
|
}
|
|
606
515
|
|
|
607
516
|
function resolve (specifier) {
|
|
608
|
-
return self.resolve(specifier, {
|
|
609
|
-
protocol: self._protocolFor(specifier, protocol),
|
|
610
|
-
referrer
|
|
611
|
-
})
|
|
517
|
+
return self.resolve(specifier, referrer._url, { referrer }).pathname
|
|
612
518
|
}
|
|
613
519
|
|
|
614
520
|
function addon (specifier = '.') {
|
|
@@ -625,13 +531,13 @@ Module._extensions['.mjs'] = function (module, source, referrer) {
|
|
|
625
531
|
module._type = constants.types.MODULE
|
|
626
532
|
|
|
627
533
|
if (protocol.load) {
|
|
628
|
-
module._exports = protocol.load(module.
|
|
534
|
+
module._exports = protocol.load(module._url)
|
|
629
535
|
} else {
|
|
630
|
-
if (source === null) source = protocol.read(module.
|
|
536
|
+
if (source === null) source = protocol.read(module._url)
|
|
631
537
|
|
|
632
538
|
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
633
539
|
|
|
634
|
-
module._handle = binding.createModule(module.
|
|
540
|
+
module._handle = binding.createModule(module._url.href, source, 0, self._handle)
|
|
635
541
|
}
|
|
636
542
|
}
|
|
637
543
|
|
|
@@ -641,9 +547,9 @@ Module._extensions['.json'] = function (module, source, referrer) {
|
|
|
641
547
|
module._type = constants.types.JSON
|
|
642
548
|
|
|
643
549
|
if (protocol.load) {
|
|
644
|
-
module._exports = protocol.load(module.
|
|
550
|
+
module._exports = protocol.load(module._url)
|
|
645
551
|
} else {
|
|
646
|
-
if (source === null) source = protocol.read(module.
|
|
552
|
+
if (source === null) source = protocol.read(module._url)
|
|
647
553
|
|
|
648
554
|
if (typeof source !== 'string') source = Buffer.coerce(source).toString()
|
|
649
555
|
|
|
@@ -654,13 +560,13 @@ Module._extensions['.json'] = function (module, source, referrer) {
|
|
|
654
560
|
Module._extensions['.bare'] = function (module, source, referrer) {
|
|
655
561
|
module._type = constants.types.ADDON
|
|
656
562
|
|
|
657
|
-
module._exports = Bare.Addon.load(module.
|
|
563
|
+
module._exports = Bare.Addon.load(url.fileURLToPath(module._url))
|
|
658
564
|
}
|
|
659
565
|
|
|
660
566
|
Module._extensions['.node'] = function (module, source, referrer) {
|
|
661
567
|
module._type = constants.types.ADDON
|
|
662
568
|
|
|
663
|
-
module._exports = Bare.Addon.load(module.
|
|
569
|
+
module._exports = Bare.Addon.load(url.fileURLToPath(module._url))
|
|
664
570
|
}
|
|
665
571
|
|
|
666
572
|
Module._extensions['.bundle'] = function (module, source, referrer) {
|
|
@@ -670,55 +576,43 @@ Module._extensions['.bundle'] = function (module, source, referrer) {
|
|
|
670
576
|
|
|
671
577
|
module._type = constants.types.BUNDLE
|
|
672
578
|
|
|
673
|
-
if (source === null) source = protocol.read(module.
|
|
579
|
+
if (source === null) source = protocol.read(module._url)
|
|
674
580
|
|
|
675
581
|
if (typeof source === 'string') source = Buffer.from(source)
|
|
676
582
|
|
|
677
583
|
referrer = module
|
|
678
584
|
|
|
679
|
-
const bundle = module._bundle = Bundle.from(source).mount(module.
|
|
585
|
+
const bundle = module._bundle = Bundle.from(source).mount(module._url.href + '/')
|
|
680
586
|
|
|
681
587
|
module._imports = bundle.imports
|
|
682
588
|
module._resolutions = bundle.resolutions
|
|
683
589
|
|
|
684
590
|
module._protocol = new Protocol({
|
|
685
|
-
exists (
|
|
686
|
-
return bundle.exists(
|
|
591
|
+
exists (url) {
|
|
592
|
+
return bundle.exists(url.href)
|
|
687
593
|
},
|
|
688
594
|
|
|
689
|
-
read (
|
|
690
|
-
return bundle.read(
|
|
595
|
+
read (url) {
|
|
596
|
+
return bundle.read(url.href)
|
|
691
597
|
}
|
|
692
598
|
})
|
|
693
599
|
|
|
694
600
|
if (bundle.main) {
|
|
695
|
-
module._exports = self.load(bundle.main, bundle.read(bundle.main), { referrer })._exports
|
|
601
|
+
module._exports = self.load(new URL(bundle.main), bundle.read(bundle.main), { referrer })._exports
|
|
696
602
|
}
|
|
697
603
|
}
|
|
698
604
|
|
|
699
605
|
Module._protocols['file:'] = new Protocol({
|
|
700
|
-
postresolve (
|
|
701
|
-
return binding.realpath(
|
|
702
|
-
},
|
|
703
|
-
|
|
704
|
-
exists (filename) {
|
|
705
|
-
return binding.exists(filename)
|
|
606
|
+
postresolve (fileURL) {
|
|
607
|
+
return url.pathToFileURL(binding.realpath(url.fileURLToPath(fileURL)))
|
|
706
608
|
},
|
|
707
609
|
|
|
708
|
-
|
|
709
|
-
return
|
|
710
|
-
}
|
|
711
|
-
})
|
|
712
|
-
|
|
713
|
-
Module._protocols['data:'] = new Protocol({
|
|
714
|
-
* resolve (specifier) {
|
|
715
|
-
yield specifier
|
|
610
|
+
exists (fileURL) {
|
|
611
|
+
return binding.exists(url.fileURLToPath(fileURL))
|
|
716
612
|
},
|
|
717
613
|
|
|
718
|
-
read (
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
return Buffer.from(decodeURIComponent(data), base64 ? 'base64' : 'ascii')
|
|
614
|
+
read (fileURL) {
|
|
615
|
+
return Buffer.from(binding.read(url.fileURLToPath(fileURL)))
|
|
722
616
|
}
|
|
723
617
|
})
|
|
724
618
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bare-module",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Module support for JavaScript",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -25,9 +25,8 @@
|
|
|
25
25
|
},
|
|
26
26
|
"homepage": "https://github.com/holepunchto/bare-module#readme",
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"bare-bundle": "^0.
|
|
29
|
-
"bare-module-resolve": "^1.
|
|
30
|
-
"bare-os": "^2.0.0",
|
|
28
|
+
"bare-bundle": "^0.4.0",
|
|
29
|
+
"bare-module-resolve": "^1.4.1",
|
|
31
30
|
"bare-path": "^2.0.0",
|
|
32
31
|
"bare-url": "^0.3.4"
|
|
33
32
|
},
|