bare-module 4.7.0 → 4.8.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 +371 -47
- package/binding.c +38 -21
- package/index.js +259 -134
- package/lib/constants.js +2 -1
- package/lib/errors.js +33 -13
- package/lib/protocol.js +9 -9
- package/package.json +4 -3
- package/prebuilds/android-arm/bare-module.bare +0 -0
- package/prebuilds/android-arm64/bare-module.bare +0 -0
- package/prebuilds/android-ia32/bare-module.bare +0 -0
- package/prebuilds/android-x64/bare-module.bare +0 -0
- package/prebuilds/darwin-arm64/bare-module.bare +0 -0
- package/prebuilds/darwin-x64/bare-module.bare +0 -0
- package/prebuilds/ios-arm64/bare-module.bare +0 -0
- package/prebuilds/ios-arm64-simulator/bare-module.bare +0 -0
- package/prebuilds/ios-x64-simulator/bare-module.bare +0 -0
- package/prebuilds/linux-arm64/bare-module.bare +0 -0
- package/prebuilds/linux-x64/bare-module.bare +0 -0
- package/prebuilds/win32-arm64/bare-module.bare +0 -0
- package/prebuilds/win32-x64/bare-module.bare +0 -0
package/README.md
CHANGED
|
@@ -10,11 +10,11 @@ npm i bare-module
|
|
|
10
10
|
|
|
11
11
|
```js
|
|
12
12
|
const Module = require('bare-module')
|
|
13
|
-
|
|
13
|
+
```
|
|
14
14
|
|
|
15
15
|
## Packages
|
|
16
16
|
|
|
17
|
-
A package is directory with a `package.json` file.
|
|
17
|
+
A package is a directory with a `package.json` file.
|
|
18
18
|
|
|
19
19
|
### Fields
|
|
20
20
|
|
|
@@ -77,7 +77,7 @@ When importing the package by name, `require('my-package')` will resolve to `<mo
|
|
|
77
77
|
|
|
78
78
|
##### Conditional exports
|
|
79
79
|
|
|
80
|
-
Conditional exports allow packages to provide different exports for different conditions, such as the
|
|
80
|
+
Conditional exports allow packages to provide different exports for different conditions, such as the loading method the importing module uses (e.g. `require()` vs `import`):
|
|
81
81
|
|
|
82
82
|
```json
|
|
83
83
|
{
|
|
@@ -121,16 +121,33 @@ To provide a fallback for when no other conditions match, the `"default"` condit
|
|
|
121
121
|
|
|
122
122
|
The following conditions are supported, listed in order from most specific to least specific as conditions should be defined:
|
|
123
123
|
|
|
124
|
-
Condition
|
|
125
|
-
|
|
126
|
-
`"
|
|
127
|
-
`"
|
|
128
|
-
`"
|
|
129
|
-
`"
|
|
130
|
-
`"
|
|
131
|
-
`"
|
|
132
|
-
`"
|
|
133
|
-
`"
|
|
124
|
+
| Condition | Description |
|
|
125
|
+
| :------------- | :---------------------------------------------------------------------------------------------------------------------------------- |
|
|
126
|
+
| `"import"` | Matches when the package is loaded via `import` or `import()`. |
|
|
127
|
+
| `"require"` | Matches when the package is loaded via `require()`. |
|
|
128
|
+
| `"asset"` | Matches when the package is loaded via `require.asset()`. |
|
|
129
|
+
| `"addon"` | Matches when the package is loaded via `require.addon()`. |
|
|
130
|
+
| `"bare"` | Matches for any [Bare](https://github.com/holepunchto/bare) environment. |
|
|
131
|
+
| `"node"` | Matches for any Node.js environment. |
|
|
132
|
+
| `"<platform>"` | Matches when equal to `Bare.platform`. See [`Bare.platform`](https://github.com/holepunchto/bare#bareplatform) for possible values. |
|
|
133
|
+
| `"<arch>"` | Matches when equal to `Bare.arch`. See [`Bare.arch`](https://github.com/holepunchto/bare#barearch) for possible values. |
|
|
134
|
+
| `"simulator"` | Matches when Bare was compiled for a simulator, i.e. when `Bare.simulator` is `true`. |
|
|
135
|
+
| `"default"` | The fallback that always matches. This condition should always be last. |
|
|
136
|
+
|
|
137
|
+
Export conditions are evaluated in the order they are defined in the `"exports"` field. This means that less specific conditionals defined first will override more specific conditions define later. For example, the following will always call `./fallback.js` because `"default"` always matches and is defined first.
|
|
138
|
+
|
|
139
|
+
```json
|
|
140
|
+
{
|
|
141
|
+
"exports": {
|
|
142
|
+
".": {
|
|
143
|
+
"default": "./fallback.js",
|
|
144
|
+
"bare": "./bare.js"
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
This is why the general rule is that conditions should be from most specific to least specific when defined.
|
|
134
151
|
|
|
135
152
|
##### Self-referencing
|
|
136
153
|
|
|
@@ -160,11 +177,74 @@ If a package defines only a single export, `"."`, it may leave out the subpath e
|
|
|
160
177
|
|
|
161
178
|
#### `"imports"`
|
|
162
179
|
|
|
180
|
+
A private mapping for import specifiers within the package itself. Similar to `"exports"`, the `"imports"` field can be used to conditional import other packages within the package. But unlike `"exports"`, `"imports"` permits mapping to external packages.
|
|
181
|
+
|
|
182
|
+
The rules are otherwise analogous to the [`"exports"`](#conditional-exports) field.
|
|
183
|
+
|
|
163
184
|
##### Subpath imports
|
|
164
185
|
|
|
186
|
+
Just like exports, subpaths can be used when importing a module internally.
|
|
187
|
+
|
|
188
|
+
```json
|
|
189
|
+
{
|
|
190
|
+
"imports": {
|
|
191
|
+
".": "./index.js",
|
|
192
|
+
"./submodule": "./lib/submodule.js"
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
```
|
|
196
|
+
|
|
165
197
|
##### Conditional imports
|
|
166
198
|
|
|
167
|
-
|
|
199
|
+
Adding conditional imports allows importing different packages based on the configured conditions. As an example:
|
|
200
|
+
|
|
201
|
+
```json
|
|
202
|
+
{
|
|
203
|
+
"imports": {
|
|
204
|
+
"bar": {
|
|
205
|
+
"require": "./baz.cjs",
|
|
206
|
+
"import": "./baz.mjs"
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
When importing the package `bar` as `require('bar')` will resolve to `./baz.cjs`, but when importing with `import('bar')` will resolve to `./baz.mjs`.
|
|
213
|
+
|
|
214
|
+
To provide a fallback for when no other conditions are met, the `"default"` condition can be configured like so:
|
|
215
|
+
|
|
216
|
+
```json
|
|
217
|
+
{
|
|
218
|
+
"imports": {
|
|
219
|
+
"bar": {
|
|
220
|
+
"require": "./baz.cjs",
|
|
221
|
+
"asset": "./baz.txt",
|
|
222
|
+
"default": "./baz.mjs"
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
The following conditions are supported, listed in order from most specific to least specific as conditions should be defined:
|
|
229
|
+
|
|
230
|
+
| Condition | Description |
|
|
231
|
+
| :------------- | :---------------------------------------------------------------------------------------------------------------------------------- |
|
|
232
|
+
| `"import"` | Matches when the package is loaded via `import` or `import()`. |
|
|
233
|
+
| `"require"` | Matches when the package is loaded via `require()`. |
|
|
234
|
+
| `"asset"` | Matches when the package is loaded via `require.asset()`. |
|
|
235
|
+
| `"addon"` | Matches when the package is loaded via `require.addon()`. |
|
|
236
|
+
| `"bare"` | Matches for any [Bare](https://github.com/holepunchto/bare) environment. |
|
|
237
|
+
| `"node"` | Matches for any Node.js environment. |
|
|
238
|
+
| `"<platform>"` | Matches when equal to `Bare.platform`. See [`Bare.platform`](https://github.com/holepunchto/bare#bareplatform) for possible values. |
|
|
239
|
+
| `"<arch>"` | Matches when equal to `Bare.arch`. See [`Bare.arch`](https://github.com/holepunchto/bare#barearch) for possible values. |
|
|
240
|
+
| `"simulator"` | Matches when Bare was compiled for a simulator, ie when `Bare.simulator` is `true`. |
|
|
241
|
+
| `"default"` | The fallback that always matches. This condition should always be last. |
|
|
242
|
+
|
|
243
|
+
The general rule is that conditions should be from most specific to least specific when defined.
|
|
244
|
+
|
|
245
|
+
##### `#` Prefix
|
|
246
|
+
|
|
247
|
+
All import maps are private to the package and allow mapping to external packages. Entries in `"imports"` may start with `#` to disambiguate from external packages, but it is not required unlike in Node.js.
|
|
168
248
|
|
|
169
249
|
#### `"engines"`
|
|
170
250
|
|
|
@@ -176,153 +256,397 @@ If a package defines only a single export, `"."`, it may leave out the subpath e
|
|
|
176
256
|
}
|
|
177
257
|
```
|
|
178
258
|
|
|
179
|
-
The engine requirements of the package. During module resolution, the versions declared by `Bare.versions` will be tested against the requirements declared by the package and resolution fail if they're not satisfied.
|
|
259
|
+
The `"engines"` field defines the engine requirements of the package. During module resolution, the versions declared by `Bare.versions` will be tested against the requirements declared by the package and resolution fail if they're not satisfied.
|
|
180
260
|
|
|
181
261
|
## API
|
|
182
262
|
|
|
183
|
-
#### `Module.constants`
|
|
184
|
-
|
|
185
263
|
#### `Module.constants.states`
|
|
186
264
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
`
|
|
265
|
+
The flags for the current state of a module.
|
|
266
|
+
|
|
267
|
+
| Constant | Description |
|
|
268
|
+
| :------------ | :------------------------------------------- |
|
|
269
|
+
| `EVALUATED` | The module has been evaluated. |
|
|
270
|
+
| `SYNTHESIZED` | The module named exports have been detected. |
|
|
271
|
+
| `DESTROYED` | The module has been unloaded. |
|
|
192
272
|
|
|
193
273
|
#### `Module.constants.types`
|
|
194
274
|
|
|
195
|
-
Constant | Description
|
|
196
|
-
|
|
197
|
-
`SCRIPT` |
|
|
198
|
-
`MODULE` |
|
|
199
|
-
`JSON` |
|
|
200
|
-
`BUNDLE` |
|
|
201
|
-
`ADDON` |
|
|
202
|
-
`BINARY` |
|
|
203
|
-
`TEXT` |
|
|
275
|
+
| Constant | Description |
|
|
276
|
+
| :------- | :--------------------------------------------------------------------------- |
|
|
277
|
+
| `SCRIPT` | The module is a CommonJS module. |
|
|
278
|
+
| `MODULE` | The module is a ECMAScript module. |
|
|
279
|
+
| `JSON` | The module is a JSON file. |
|
|
280
|
+
| `BUNDLE` | The module is a [`bare-bundle`](https://github.com/holepunchto/bare-bundle). |
|
|
281
|
+
| `ADDON` | The module is a native addon. |
|
|
282
|
+
| `BINARY` | The module is a binary file. |
|
|
283
|
+
| `TEXT` | The module is a text file. |
|
|
204
284
|
|
|
205
285
|
#### `Module.protocol`
|
|
206
286
|
|
|
287
|
+
The default `ModuleProtocol` class for resolving, reading and loading modules. See [Protocols](#protocols) for usage.
|
|
288
|
+
|
|
207
289
|
#### `Module.cache`
|
|
208
290
|
|
|
291
|
+
The global cache of loaded modules.
|
|
292
|
+
|
|
209
293
|
#### `const url = Module.resolve(specifier, parentURL[, options])`
|
|
210
294
|
|
|
295
|
+
Resolve the module `specifier` relative to the `parentURL`. `specifier` is a string and `parentURL` is a WHATWG `URL`.
|
|
296
|
+
|
|
211
297
|
Options include:
|
|
212
298
|
|
|
213
299
|
```js
|
|
214
|
-
{
|
|
215
|
-
|
|
216
|
-
|
|
300
|
+
options = {
|
|
301
|
+
// Whether the module is called via `import` or `import()`.
|
|
302
|
+
isImport: false,
|
|
303
|
+
// The referring module.
|
|
304
|
+
referrer: null,
|
|
305
|
+
// The type of the module. See Module.constants.types for possible values. The
|
|
306
|
+
// default is the equivalent constant of the `attributes`'s `type` property.
|
|
217
307
|
type,
|
|
218
|
-
extensions
|
|
308
|
+
// A list of file extensions to look for. The default is based on the `type`
|
|
309
|
+
// option.
|
|
310
|
+
extensions: [],
|
|
311
|
+
// The ModuleProtocol to resolve the specifier. Defaults to referrer's
|
|
312
|
+
// protocol if defined, otherwise defaults to Module.protocol
|
|
219
313
|
protocol,
|
|
314
|
+
// A default "imports" map to apply to all specifiers. Follows the same
|
|
315
|
+
// syntax and rules as the "imports" property defined in `package.json`.
|
|
220
316
|
imports,
|
|
317
|
+
// A map of preresolved imports with keys being serialized parent URLs and
|
|
318
|
+
// values being "imports" maps.
|
|
221
319
|
resolutions,
|
|
320
|
+
// A map of builtin module specifiers to loaded modules. If matched by the
|
|
321
|
+
// default resolver, the protocol of the resolved URL will be `builtin:`.
|
|
222
322
|
builtins,
|
|
223
|
-
conditions
|
|
323
|
+
// The supported import conditions. "default" is always recognized.
|
|
324
|
+
conditions: [],
|
|
325
|
+
// The import attributes, e.g. the `{ type: "json" }` in:
|
|
326
|
+
// `import foo from 'foo' with { type: "json" }`
|
|
327
|
+
// or in:
|
|
328
|
+
// `require('foo', { with: { type: "json" } })`
|
|
224
329
|
attributes
|
|
225
330
|
}
|
|
226
331
|
```
|
|
227
332
|
|
|
228
333
|
#### `const module = Module.load(url[, source][, options])`
|
|
229
334
|
|
|
335
|
+
Load a module with the provided `url`. `url` is a WHATWG `URL`. If provided, the `source` will be passed to the matching `extension` for the `url`.
|
|
336
|
+
|
|
230
337
|
Options include:
|
|
231
338
|
|
|
232
339
|
```js
|
|
233
|
-
{
|
|
234
|
-
|
|
340
|
+
options = {
|
|
341
|
+
// Whether the module is called via `import` or `import()`.
|
|
342
|
+
isImport: false,
|
|
343
|
+
// Whether the module is called via `import()`.
|
|
344
|
+
isDynamicImport: false,
|
|
345
|
+
// The referring module.
|
|
346
|
+
referrer: null,
|
|
347
|
+
// The type of the module. See Module.constants.types for possible values. The
|
|
348
|
+
// default is the equivalent constant of the `attributes`'s `type` property.
|
|
235
349
|
type,
|
|
236
|
-
|
|
350
|
+
// The assumed type of a module without a type using an ambiguous extension
|
|
351
|
+
// such as `.js`. See Module.constants.types. Inherited from `referrer` if it
|
|
352
|
+
// is defined.
|
|
353
|
+
defaultType: Module.constants.types.SCRIPT,
|
|
354
|
+
// Cache to use to load the Module. Defaults to `Module.cache`.
|
|
237
355
|
cache,
|
|
356
|
+
// The module representing the entry script where the program was launched.
|
|
238
357
|
main,
|
|
358
|
+
// The ModuleProtocol to use resolve the specifier. Defaults to referrer's
|
|
359
|
+
// `protocol` if defined, otherwise defaults to `Module.protocol`.
|
|
239
360
|
protocol,
|
|
361
|
+
// A default "imports" map to apply to all specifiers. Follows the same
|
|
362
|
+
// syntax and rules as the "imports" property defined in `package.json`.
|
|
240
363
|
imports,
|
|
364
|
+
// A map of preresolved imports with keys being serialized parent URLs and
|
|
365
|
+
// values being "imports" maps.
|
|
241
366
|
resolutions,
|
|
367
|
+
// A map of builtin module specifiers to loaded modules. If the `url`'s
|
|
368
|
+
// protocol is `builtin:`, the module's exports will be set to the matching
|
|
369
|
+
// value in the map for `url.pathname`.
|
|
242
370
|
builtins,
|
|
371
|
+
// The supported import conditions. "default" is always recognized.
|
|
243
372
|
conditions,
|
|
373
|
+
// The import attributes, e.g. the `{ type: "json" }` in:
|
|
374
|
+
// `import foo from 'foo' with { type: "json" }`
|
|
375
|
+
// or in:
|
|
376
|
+
// `require('foo', { with: { type: "json" } })`
|
|
244
377
|
attributes
|
|
245
378
|
}
|
|
246
379
|
```
|
|
247
380
|
|
|
248
381
|
#### `const url = Module.asset(specifier, parentURL[, options])`
|
|
249
382
|
|
|
383
|
+
Get the asset URL by resolving `specifier` relative to `parentURL`. `specifier` is a string and `parentURL` is a WHATWG `URL`.
|
|
384
|
+
|
|
250
385
|
Options include:
|
|
251
386
|
|
|
252
387
|
```js
|
|
253
|
-
{
|
|
254
|
-
|
|
388
|
+
options = {
|
|
389
|
+
// The referring module.
|
|
390
|
+
referrer: null,
|
|
391
|
+
// The ModuleProtocol to use resolve the specifier. Defaults to referrer's
|
|
392
|
+
// protocol if defined, otherwise defaults to Module.protocol
|
|
255
393
|
protocol,
|
|
394
|
+
// A default "imports" map to apply to all specifiers. Follows the same
|
|
395
|
+
// syntax and rules as the "imports" property defined in `package.json`.
|
|
256
396
|
imports,
|
|
397
|
+
// A map of preresolved imports with keys being serialized parent URLs and
|
|
398
|
+
// values being "imports" maps.
|
|
257
399
|
resolutions,
|
|
400
|
+
// The supported import conditions. "default" is always recognized.
|
|
258
401
|
conditions
|
|
259
402
|
}
|
|
260
403
|
```
|
|
261
404
|
|
|
262
405
|
#### `module.url`
|
|
263
406
|
|
|
407
|
+
The WHATWG `URL` instance for the module.
|
|
408
|
+
|
|
264
409
|
#### `module.filename`
|
|
265
410
|
|
|
411
|
+
The pathname of the `module.url`.
|
|
412
|
+
|
|
266
413
|
#### `module.dirname`
|
|
267
414
|
|
|
415
|
+
The directory name of the module.
|
|
416
|
+
|
|
268
417
|
#### `module.type`
|
|
269
418
|
|
|
419
|
+
The type of the module. See [`Module.constants.types`](#module.constants.types) for possible values.
|
|
420
|
+
|
|
270
421
|
#### `module.defaultType`
|
|
271
422
|
|
|
423
|
+
The assumed type of a module without a `type` using an ambiguous extension, such as `.js`. See [`Module.constants.types`](#module.constants.types) for possible values.
|
|
424
|
+
|
|
272
425
|
#### `module.cache`
|
|
273
426
|
|
|
427
|
+
A cache of loaded modules for this module. Defaults to `Module.cache`.
|
|
428
|
+
|
|
274
429
|
#### `module.main`
|
|
275
430
|
|
|
431
|
+
The module representing the entry script where the program was launched.
|
|
432
|
+
|
|
276
433
|
#### `module.exports`
|
|
277
434
|
|
|
435
|
+
The exports from the module.
|
|
436
|
+
|
|
278
437
|
#### `module.imports`
|
|
279
438
|
|
|
439
|
+
The import map when the module was loaded.
|
|
440
|
+
|
|
280
441
|
#### `module.resolutions`
|
|
281
442
|
|
|
443
|
+
A map of preresolved imports with keys being serialized parent URLs and values being `"imports"` maps.
|
|
444
|
+
|
|
282
445
|
#### `module.builtins`
|
|
283
446
|
|
|
447
|
+
A map of builtin module specifiers mapped to the loaded module.
|
|
448
|
+
|
|
284
449
|
#### `module.conditions`
|
|
285
450
|
|
|
451
|
+
An array of conditions used to resolve dependencies while loading the module. See [Conditional Exports](#conditional-exports) for possible values.
|
|
452
|
+
|
|
286
453
|
#### `module.protocol`
|
|
287
454
|
|
|
455
|
+
The `ModuleProtocol` class used for resolving, reading and loading modules. See [Protocols](#protocols).
|
|
456
|
+
|
|
288
457
|
#### `module.destroy()`
|
|
289
458
|
|
|
459
|
+
Unloads the module.
|
|
460
|
+
|
|
461
|
+
### CommonJS modules
|
|
462
|
+
|
|
463
|
+
#### `require(specifier[, options])`
|
|
464
|
+
|
|
465
|
+
Used to import JavaScript or JSON modules and local files. Relative paths such as `./`, `./foo`, `./bar/baz`, and `../foo` will be resolved against the directory named by `__dirname`. POSIX style paths are resolved in an OS independent fashion, meaning that the examples above will work on Windows in the same way they would on POSIX systems.
|
|
466
|
+
|
|
467
|
+
Returns the exported module contents.
|
|
468
|
+
|
|
469
|
+
Options include:
|
|
470
|
+
|
|
471
|
+
```js
|
|
472
|
+
options = {
|
|
473
|
+
// The import attributes which instruct how the file or module should be loaded.
|
|
474
|
+
// Possible values for `type` are `script`, `module`, `json`, `bundle`,
|
|
475
|
+
// `addon`, `binary` and `text`.
|
|
476
|
+
with: { type: 'json' }
|
|
477
|
+
}
|
|
478
|
+
```
|
|
479
|
+
|
|
480
|
+
#### `require.main`
|
|
481
|
+
|
|
482
|
+
The module representing the entry script where the program was launched. The same value as [`module.main`](#modulemain) for the current module.
|
|
483
|
+
|
|
484
|
+
#### `require.cache`
|
|
485
|
+
|
|
486
|
+
A cache of loaded modules for this module. The same value as `module.cache` for the current module.
|
|
487
|
+
|
|
488
|
+
#### `const path = require.resolve(specifier[, parentURL])`
|
|
489
|
+
|
|
490
|
+
Use the internal machinery of `require()` to resolve the `specifier` string relative to the URL `parentURL` and return the path string.
|
|
491
|
+
|
|
492
|
+
#### `require.addon([specifier][, parentURL])`
|
|
493
|
+
|
|
494
|
+
Also used to import modules but specifically loads only addon modules. `specifier` is resolved relative to `parentURL` using the [addon resolution](https://github.com/holepunchto/bare-addon-resolve#algorithm) algorithm.
|
|
495
|
+
|
|
496
|
+
Returns the exported module contents.
|
|
497
|
+
|
|
498
|
+
A common pattern for writing an addon module is to use `require.addon()` as the JavaScript module exports:
|
|
499
|
+
|
|
500
|
+
```js
|
|
501
|
+
module.exports = require.addon()
|
|
502
|
+
```
|
|
503
|
+
|
|
504
|
+
See [`bare-addon`](https://github.com/holepunchto/bare-addon) for a template of building native addon modules.
|
|
505
|
+
|
|
506
|
+
#### `require.addon.host`
|
|
507
|
+
|
|
508
|
+
Returns the string representation of the platform and architecture used when resolving addons with the pattern `<platform>-<arch>[-simulator]`. Returns the same value as `Bare.Addon.host`.
|
|
509
|
+
|
|
510
|
+
#### `const path = require.addon.resolve([specifier][, parentURL])`
|
|
511
|
+
|
|
512
|
+
Resolve the `specifier` string relative to the URL `parentURL` as an addon and returns the path string. The `specifier` is resolved using the [addon resolution algorithm](https://github.com/holepunchto/bare-addon-resolve#algorithm).
|
|
513
|
+
|
|
514
|
+
#### `const path = require.asset(specifier[, parentURL])`
|
|
515
|
+
|
|
516
|
+
Resolve the `specifier` relative to the `parentURL` and return the path of the asset as a string.
|
|
517
|
+
|
|
518
|
+
Can be used to load assets, for example the following loads `./foo.txt` from the local files:
|
|
519
|
+
|
|
520
|
+
```js
|
|
521
|
+
const fs = require('bare-fs')
|
|
522
|
+
const contents = fs.readFileSync(require.asset('./foo.txt'))
|
|
523
|
+
```
|
|
524
|
+
|
|
525
|
+
### ECMAScript modules
|
|
526
|
+
|
|
527
|
+
#### `import defaultExport, * as name, { export1, export2 as alias2, ... } from 'specifier' with { type: 'json' }`
|
|
528
|
+
|
|
529
|
+
The static `import` declaration is used to import read-only live bindings that are exported by another module. The imported bindings are called _live_ bindings because they are updated by the module that exported the binding, but cannot be re-assigned by the importing module. In brief, you can import what is exported from another module.
|
|
530
|
+
|
|
531
|
+
For more information on `import` syntax, see [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import).
|
|
532
|
+
|
|
533
|
+
#### `import.meta.url`
|
|
534
|
+
|
|
535
|
+
The string representation of the URL for the current module.
|
|
536
|
+
|
|
537
|
+
#### `import.meta.main`
|
|
538
|
+
|
|
539
|
+
A boolean representing whether the current module is the entry script where the program was launched.
|
|
540
|
+
|
|
541
|
+
#### `import.meta.cache`
|
|
542
|
+
|
|
543
|
+
A cache of loaded modules for this module. The same value as `module.cache` for the current module.
|
|
544
|
+
|
|
545
|
+
#### `const href = import.meta.resolve(specifier[, parentURL])`
|
|
546
|
+
|
|
547
|
+
A module-relative resolution function which returns the URL string for the module. The `specifier` is a string which is resolved relative to the `parentURL` which is a WHATWG URL.
|
|
548
|
+
|
|
549
|
+
#### `import.meta.addon([specifier][, parentURL])`
|
|
550
|
+
|
|
551
|
+
Also used to import modules but specifically loads only addon modules. `specifier` is resolved relative to `parentURL` using the [addon resolution](https://github.com/holepunchto/bare-addon-resolve#algorithm) algorithm.
|
|
552
|
+
|
|
553
|
+
Returns the exported module contents.
|
|
554
|
+
|
|
555
|
+
#### `import.meta.addon.host`
|
|
556
|
+
|
|
557
|
+
Returns the string representation of the platform and architecture used when resolving addons with the pattern `<platform>-<arch>[-simulator]`. Returns the same value as `Bare.Addon.host`.
|
|
558
|
+
|
|
559
|
+
#### `const href = import.meta.addon.resolve([specifier][, parentURL])`
|
|
560
|
+
|
|
561
|
+
Resolve the `specifier` string relative to the URL `parentURL` as an addon and returns the URL string. The `specifier` is resolved using the [addon resolution algorithm](https://github.com/holepunchto/bare-addon-resolve#algorithm).
|
|
562
|
+
|
|
563
|
+
#### `const href = import.meta.asset(specifier[, parentURL])`
|
|
564
|
+
|
|
565
|
+
Resolve the `specifier` relative to the `parentURL` and return the URL of the asset as a string.
|
|
566
|
+
|
|
290
567
|
### Custom `require()`
|
|
291
568
|
|
|
569
|
+
Creating a custom require allows one to create a preconfigured `require()`. This can be useful in scenarios such as a Read-Evaluate-Print-Loop (REPL) where the parent URL is set to a directory so requiring relative paths to work correctly.
|
|
570
|
+
|
|
292
571
|
#### `const require = Module.createRequire(parentURL[, options])`
|
|
293
572
|
|
|
294
573
|
Options include:
|
|
295
574
|
|
|
296
575
|
```js
|
|
297
|
-
{
|
|
298
|
-
referrer
|
|
299
|
-
|
|
300
|
-
|
|
576
|
+
options = {
|
|
577
|
+
// The module to become the `referrer` for the returned `require()`. Defaults
|
|
578
|
+
// to creating a new module instance from the `parentURL` with the same
|
|
579
|
+
// options.
|
|
580
|
+
module: null,
|
|
581
|
+
// The referring module.
|
|
582
|
+
referrer: null,
|
|
583
|
+
// The type of the module. See Module.constants.types for possible values.
|
|
584
|
+
type: Module.constants.types.SCRIPT,
|
|
585
|
+
// The assumed type of a module without a type using an ambiguous extension
|
|
586
|
+
// such as `.js`. See Module.constants.types. Inherited from `referrer` if it
|
|
587
|
+
// is defined, otherwise defaults to SCRIPT.
|
|
588
|
+
defaultType: Module.constants.types.SCRIPT,
|
|
589
|
+
// A cache of loaded modules. Inherited from `referrer` if it is defined,
|
|
590
|
+
// otherwise defaults to `Module.cache`
|
|
301
591
|
cache,
|
|
592
|
+
// The module representing the entry script where the program was launched.
|
|
302
593
|
main,
|
|
594
|
+
// The ModuleProtocol to use resolve the specifier and/or the module. Defaults to
|
|
595
|
+
// referrer's protocol if defined, otherwise defaults to Module.protocol
|
|
303
596
|
protocol,
|
|
597
|
+
// A default "imports" map to apply to all specifiers. Follows the same
|
|
598
|
+
// syntax and rules as the "imports" property defined in `package.json`.
|
|
304
599
|
imports,
|
|
600
|
+
// A map of preresolved imports with keys being serialized parent URLs and
|
|
601
|
+
// values being "imports" maps.
|
|
305
602
|
resolutions,
|
|
603
|
+
// A map of builtin module specifiers to loaded modules.
|
|
306
604
|
builtins,
|
|
605
|
+
// The supported import conditions. "default" is always recognized.
|
|
307
606
|
conditions
|
|
308
607
|
}
|
|
309
608
|
```
|
|
310
609
|
|
|
311
610
|
### Protocols
|
|
312
611
|
|
|
313
|
-
|
|
612
|
+
Protocols define how to resolve, access and load modules. Custom protocols can be defined to extend or replace how module are resolved and loaded to support things like loading modules via a [`Hyperdrive`](https://github.com/holepunchto/hyperdrive).
|
|
613
|
+
|
|
614
|
+
#### `const protocol = new Module.Protocol(methods, context = null)`
|
|
314
615
|
|
|
315
616
|
Methods include:
|
|
316
617
|
|
|
317
618
|
```js
|
|
318
|
-
{
|
|
619
|
+
methods = {
|
|
620
|
+
// function (specifier, parentURL): string
|
|
621
|
+
// A function to preprocess the `specifier` and `parentURL` before the resolve
|
|
622
|
+
// algorithm is called.
|
|
319
623
|
preresolve,
|
|
624
|
+
// function (url): string
|
|
625
|
+
// A function to process the resolved URL. Can be used to convert file paths,
|
|
626
|
+
// etc.
|
|
320
627
|
postresolve,
|
|
628
|
+
// function* (specifier, parentURL, imports): [URL]
|
|
629
|
+
// A generator to resolve the `specifier` to a URL.
|
|
321
630
|
resolve,
|
|
631
|
+
// function (url): boolean
|
|
632
|
+
// A function that returns whether the URL exists as a boolean.
|
|
322
633
|
exists,
|
|
634
|
+
// function (url): string | Buffer
|
|
635
|
+
// A function that returns the source code of a URL represented as a string or
|
|
636
|
+
// buffer.
|
|
323
637
|
read,
|
|
638
|
+
// function (url): object
|
|
639
|
+
// A function that returns the evaluated exports for the url. This is
|
|
640
|
+
// only called for Javascript modules (extensions `.js`, `.cjs` & `.mjs`)
|
|
641
|
+
// by default. If defined, this function will skip calling `read()` and
|
|
642
|
+
// evaluating the source method for the default implementations of the
|
|
643
|
+
// Javascript extensions.
|
|
324
644
|
load,
|
|
645
|
+
// function (url): URL
|
|
646
|
+
// A function used to post process URLs for addons before `postresolve()`.
|
|
325
647
|
addon,
|
|
648
|
+
// function (url): URL
|
|
649
|
+
// A function used to post process URLs for assets before `postresolve()`.
|
|
326
650
|
asset
|
|
327
651
|
}
|
|
328
652
|
```
|