Skip to content

Commit b9a150f

Browse files
authored
Docs: Fix inaccurate and outdated documentation (#2077) (#2077)
1 parent 3de6a03 commit b9a150f

12 files changed

Lines changed: 337 additions & 155 deletions

File tree

‎docs/API.md‎

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,31 +44,37 @@ title: Bundling API
4444

4545
## Reference
4646

47-
All functions exposed below accept an additional `config` option. This object should be the [Metro configuration](CLI.md) exposed by your `metro.config.js` file - you can obtain it using `Metro.loadConfig`.
47+
The `config` object accepted by the functions below is the [Metro configuration](./Configuration.md) exported by your `metro.config.js` file, which you can obtain with `Metro.loadConfig`.
4848

49-
### `loadConfig(<options>)`
49+
### `loadConfig(<options>, <defaultConfigOverrides>)`
5050

5151
**Basic options:** `config`, `cwd`
5252

53-
Load the Metro configuration, either from `config` in options if specified, or by traversing the directory hierarchy from `cwd` to the root until it finds a file (by default `metro.config.js`). The returned configuration will have been normalized and merged with Metro's default values.
53+
Load the Metro configuration, either from the path given in `config` if specified, or by traversing the directory hierarchy from `cwd` up to the user's home directory until it finds a [config file](./Configuration.md). The returned configuration will have been normalized and merged with Metro's default values, after applying `defaultConfigOverrides` to the defaults.
5454

55-
### `async runMetro(config)`
55+
`loadConfig` also accepts the options of the [CLI](./CLI.md), which override the corresponding config values: `port` ([`server.port`](./Configuration.md#port)), `projectRoot`, `watchFolders`, `assetExts`, `sourceExts` and `platforms` (the `resolver` options of the same names), [`maxWorkers`](./Configuration.md#maxworkers) (which defaults to a number based on `os.availableParallelism()`), `transformer` ([`transformer.babelTransformerPath`](./Configuration.md#babeltransformerpath)), `resetCache`, and `verbose` (`false` silences Metro's terminal output).
5656

57-
Creates a Metro server based on the config and returns it. You can use this as a middleware in your existing server.
57+
### `async runMetro(config, <options>)`
58+
59+
**Basic options:** `waitForBundler`, `watch`, `onBundleBuilt`, `hasReducedPerformance`
60+
61+
Creates a Metro server based on the config and returns it. Its `processRequest` method can be used as a middleware in your existing server. `hasReducedPerformance` only makes Metro's terminal output warn that Metro is operating with reduced performance.
5862

5963
### `async runBuild(config, <options>)`
6064

61-
**Required options:** `entry`, `out`
65+
**Required options:** `entry`
66+
67+
**Basic options:** `out`, `bundleOut`, `sourceMapOut`, `dev`, `minify`, `platform`, `sourceMap`, `sourceMapUrl`, `assets`, `onBegin`, `onProgress`, `onComplete`, `customResolverOptions`, `customTransformOptions`, `output`
6268

63-
**Basic options:** `dev`, `minify`, `platform`, `sourceMap`, `sourceMapUrl`, `assets`
69+
Bundles `entry` for the given `platform` (defaults to `'web'`). If `out` or `bundleOut` is given, saves the bundle to `bundleOut`, or to `out` with a `.js` extension. If `sourceMap` is set, also generates a source map. The source map will be inlined, unless `sourceMapUrl` is also defined, in which case `sourceMapUrl` is used as the bundle's `sourceMappingURL`. When saving to disk, the source map is also written to `sourceMapOut`, or to `out` with a `.map` extension. If `assets` is `true`, an array of `AssetData` will be generated and returned in the `assets` property of the result object.
6470

65-
Bundles `entry` for the given `platform`, and saves it to location `out`. If `sourceMap` is set, also generates a source map. The source map will be inlined, unless `sourceMapUrl` is also defined. In the latter case, a new file will be generated with the basename of the `sourceMapUrl` parameter. If `assets` is `true`, an array of `AssetData` will be generated and returned in the `assets` property of the result object.
71+
`output` replaces how the bundle is built and written to disk. It is an object with two functions: `build(server, requestOptions, buildOptions)`, whose result `runBuild` resolves to and passes to `save`, and `save(bundle, outputOptions, log)`, which is only called if `out` or `bundleOut` is given. Defaults to Metro's plain bundle output, `metro/private/shared/output/bundle`. For example, `require('metro/private/shared/output/RamBundle')` writes a [RAM bundle](./Bundling.md#indexed-ram-bundle) instead.
6672

6773
### `async runServer(config, <options>)`
6874

69-
**Basic options:** `host`, `port`, `secureServerOptions`, `unstable_middleware`, `unstable_priorityMiddleware`, `unstable_onServerCreated`, `secure (DEPRECATED)`, `secureKey (DEPRECATED)`, `secureCert (DEPRECATED)`
75+
**Basic options:** `host`, `onReady`, `onClose`, `onError`, `waitForBundler`, `watch`, `hasReducedPerformance`, `websocketEndpoints`, `secureServerOptions`, `unstable_middleware`, `unstable_priorityMiddleware`, `unstable_onServerCreated`, `secure (DEPRECATED)`, `secureKey (DEPRECATED)`, `secureCert (DEPRECATED)`
7076

71-
Starts a full Metro HTTP server. It will listen on the specified `host:port`, and can then be queried to retrieve bundles for various entry points. If the `secureServerOptions` family of options are present, the server will be exposed over HTTPS.
77+
Starts a full Metro HTTP server. It will listen on the specified `host` and [`server.port`](./Configuration.md#port), and can then be queried to retrieve bundles for various entry points. If `secureServerOptions` or [`server.tls`](./Configuration.md#tls) is present, the server will be exposed over HTTPS. Resolves to `{httpServer}`.
7278

7379
`unstable_middleware` and `unstable_priorityMiddleware` take the same shapes as [`server.unstable_middleware`](./Configuration.md#unstable_middleware-experimental) and [`server.unstable_priorityMiddleware`](./Configuration.md#unstable_prioritymiddleware-experimental), and wrap them: `unstable_middleware` passed here runs after the config's, and `unstable_priorityMiddleware` runs before the config's.
7480

@@ -80,6 +86,20 @@ Priority middleware handlers must call `next()` for every request they don't ful
8086

8187
### `createConnectMiddleware(config, <options>)`
8288

83-
**Basic options:** `port`, `onBundleBuilt`
89+
**Basic options:** `onBundleBuilt`, `watch`, `waitForBundler`, `hasReducedPerformance`
90+
91+
Instead of creating the full server, creates a Connect middleware that answers to bundle requests. This middleware can then be plugged into your own servers. Resolves to `{middleware, attachHmrServer(httpServer), metroServer, end()}`. The `onBundleBuilt` function is optional, is passed the bundle path, and is called when the server has finished creating the bundle.
92+
93+
### `async buildGraph(config, <options>)`
94+
95+
**Required options:** `entries`
96+
97+
**Basic options:** `dev`, `minify`, `platform`, `type`, `customTransformOptions`
98+
99+
Resolves and transforms the dependency graph for the given `entries` without serializing a bundle, and returns the graph.
100+
101+
### `attachMetroCli(yargs, <options>)`
102+
103+
**Basic options:** `build`, `serve`, `dependencies`
84104

85-
Instead of creating the full server, creates a Connect middleware that answers to bundle requests. This middleware can then be plugged into your own servers. The `port` parameter is optional and only used for logging purposes. The `onBundleBuilt` function is optional, is passed the bundle name, and is called when the server has finished creating the bundle.
105+
Registers Metro's [CLI commands](./CLI.md) on a `yargs` instance. Pass `null` for a command to omit it.

‎docs/Bundling.md‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,25 @@ When bundling, each of the modules gets assigned a numeric id, meaning no dynami
77

88
## Plain bundle
99

10-
This is the standard bundling format. In this format, all files are wrapped with a function call, then added to the global file. This is useful for environments that expect a JS only bundle (e.g. a browser). Just requiring the entry point with the `.bundle` extension should trigger a build of it.
10+
This is the standard bundling format. In this format, all files are wrapped with a function call (`__d()`, which registers the module with Metro's module system), then concatenated into a single file. This is useful for environments that expect a JS only bundle (e.g. a browser). Just requiring the entry point with the `.bundle` extension should trigger a build of it.
1111

1212
## Indexed RAM bundle
1313

14+
RAM (Random Access Modules) bundles store each module separately, so the app can load a module's code only when it is first required.
15+
1416
This format composes the bundle as a binary file, which format has the following parts (all numbers are expressed in Little Endian):
1517

1618
* A magic number: a `uint32` must be located at the beginning of the file, with the value `0xFB0BD1E5`. This is used to verify the file.
1719
* An offset table: the table is a sequence of `uint32` pairs, with a header
18-
* For the header, two `uint32`s can be found: the length of the table, and the length of the startup code.
19-
* For the pairs, they represent the offset in the file and the length of code module, in bytes.
20-
* Each of the modules, finished by a null byte (`\0`).
20+
* For the header, two `uint32`s can be found: the number of table entries (highest module id + 1), and the length of the startup code, in bytes.
21+
* For the pairs, they represent the offset of the module code relative to the start of the startup code, and the length of the module code, in bytes.
22+
* The startup code, followed by each of the modules, each finished by a null byte (`\0`). The startup code runs when the bundle loads: polyfills, Metro's module system, [`preloadedModules`](./Configuration.md#gettransformoptions), and the call that runs the entry point.
2123

2224
```
2325
` 0 1 2 3 4 5 6
2426
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3
2527
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
26-
| Magic number | Header size |
28+
| Magic number | Number of entries |
2729
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2830
| Startup code size | Module 0 offset |
2931
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
@@ -35,9 +37,9 @@ This format composes the bundle as a binary file, which format has the following
3537
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
3638
| | Module n offset |
3739
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38-
| Module n length | Module 0 code | Module 0 code | ... | \0 |
40+
| Module n length | Startup code | Startup code | ... | \0 |
3941
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40-
| Module 1 code | Module 1 code | ... | \0 | |
42+
| Module 0 code | Module 0 code | ... | \0 | |
4143
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ +
4244
| |
4345
+ ... +
@@ -49,12 +51,12 @@ This format composes the bundle as a binary file, which format has the following
4951

5052
This structure is optimal for an environment that is able to load all code in memory at once:
5153

52-
* By using the offset table, one can load any module in constant time, where the code for module `x` is located at `file[(x + 3) * sizeof(uint32)]`. Since there is a null character (`\0`) separating all modules, usually length does not even need to be used, and the module can be loaded directly as an ASCIIZ string.
53-
* Startup code is always found at `file[sizeof(uint32)]`.
54+
* By using the offset table, one can load any module in constant time, where the offset/length entry for module `x` is located at `file[(3 + 2 * x) * sizeof(uint32)]`, and its code starts at that offset from the start of the startup code. Since there is a null character (`\0`) separating all modules, usually length does not even need to be used, and the module can be loaded directly as a null-terminated (ASCIIZ) string.
55+
* Startup code is always found right after the offset table, at `file[(3 + 2 * numEntries) * sizeof(uint32)]`.
5456

5557
This bundling is usually used by iOS.
5658

5759
## File RAM bundle
5860

59-
Each module is stored as a file, with the name `js-modules/${id}.js`, plus an extra file called `UNBUNDLE` is created, which its only content is the magic number, `0xFB0BD1E5`. Note that the `UNBUNDLE` file is created at the root.
61+
Each module is stored as a file, with the name `js-modules/${id}.js`, plus an extra file called `UNBUNDLE` is created, which its only content is the magic number, `0xFB0BD1E5`, and which marks the bundle as a file RAM bundle. Note that the `UNBUNDLE` file is created inside the `js-modules` directory, and the startup code is written to the bundle output file.
6062
This bundling is usually used by Android, since package contents are zipped, and access to a zipped file is much faster. If the indexed format was used instead, all the bundled should be unzipped at once to get the code for the corresponding module.

‎docs/CLI.md‎

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,45 @@ Generates a JavaScript bundle containing the specified entrypoint and its descen
1414

1515
| Option | Alias | Description | Value |
1616
|----------|----------|----------|----------|
17-
| `out` | `O` | File name where to store the output | String |
18-
| `platform` | `p` | Which platform to bundle for | `web`, `android`, `ios` |
17+
| `out` | `O` | File name where to store the output (required) | String |
18+
| `platform` | `p` | Which platform to bundle for (e.g. `web`, `android`, `ios`) | String |
1919
| `minify` | `z` | Whether Metro should minify the bundle | Boolean |
20-
| `dev` | `g` | Create a development version of the build (`process.env.NODE_ENV = 'development'`) | Boolean |
20+
| `dev` | `g` | Create a development build, in which `__DEV__` is `true` | Boolean |
2121
| `config` | `c` | Location of the `metro.config.js` to use | String |
22-
| `max-workers` | `j` | The number of workers Metro should parallelize the transformer on | Number |
23-
| `project-roots` | `P` | The root folder of your project | Array |
22+
| `max-workers` | `j` | The number of workers Metro should parallelize the transformer on. Overrides [`maxWorkers`](./Configuration.md#maxworkers) which defaults to a number based on `os.availableParallelism()` | Number |
23+
| `project-roots` | `P` | Accepted, but has no effect | Array |
2424
| `source-map` | | Whether Metro should generate source maps | Boolean |
2525
| `source-map-url` | | URL where the source map can be found | String |
26-
| `legacy-bundler` | | Whether Metro should use the legacy bundler | Boolean |
26+
| `legacy-bundler` | | Accepted, but has no effect | Boolean |
2727
| `resolver-option` | | [Custom resolver options](./Resolution.md#customresolveroptions-string-mixed) of the form `key=value` | Array |
28-
| `transform-option` | | Custom transform options of the form `key=value` | Array |
28+
| `transform-option` | `transformer-option` | Custom transform options of the form `key=value` | Array |
29+
| `reset-cache` | | **Deprecated.** Clears Metro's caches before building, like [`resetCache`](./Configuration.md#resetcache) | Boolean |
30+
| `output-type` | `t` | Accepted, but has no effect | String |
2931

3032

3133
## `serve`
3234

33-
Starts Metro on the given port, building bundles on the fly.
35+
Alias: `start`
3436

35-
## `get-dependencies <entryFile>`
37+
Starts Metro on the given port, building bundles on the fly. Restarts the server when the config file changes.
38+
39+
### Options
40+
41+
| Option | Alias | Description | Value |
42+
|----------|----------|----------|----------|
43+
| `host` | `h` | Host to listen on. Defaults to `localhost` | String |
44+
| `port` | `p` | Port to listen on. Defaults to `8081`, overriding [`server.port`](./Configuration.md#port) | Number |
45+
| `config` | `c` | Location of the `metro.config.js` to use | String |
46+
| `max-workers` | `j` | The number of workers Metro should parallelize the transformer on. Overrides [`maxWorkers`](./Configuration.md#maxworkers) which defaults to a number based on `os.availableParallelism()` | Number |
47+
| `secure-server-options` | `s` | Options for the HTTPS server, using dot notation (e.g. `-s.cert="$(cat path/to/cert)" -s.key="$(cat path/to/key)"`) | Object |
48+
| `secure` | | **Deprecated.** Serve over HTTPS. Use `secure-server-options` instead | Boolean |
49+
| `secure-key` | | **Deprecated.** Path to the key file for HTTPS when `secure` is set | String |
50+
| `secure-cert` | | **Deprecated.** Path to the cert file for HTTPS when `secure` is set | String |
51+
| `reset-cache` | | **Deprecated.** Clears Metro's caches on startup, like [`resetCache`](./Configuration.md#resetcache) | Boolean |
52+
| `project-roots` | `P` | Accepted, but has no effect | Array |
53+
| `hmr-enabled` | `hmr` | Accepted, but has no effect | Boolean |
54+
55+
## `get-dependencies [entryFile]`
3656

3757
List all dependencies that will be bundled for a given entry point.
3858

@@ -44,6 +64,6 @@ List all dependencies that will be bundled for a given entry point.
4464
| `output` | File name where to store the output, ex. /tmp/dependencies.txt |
4565
| `platform` | The platform extension used for selecting modules |
4666
| `transformer` | Specify a custom transformer to be used |
47-
| `max-workers` | Specifies the maximum number of workers the worker-pool will spawn for transforming files. This defaults to the number of the cores available on your machine. |
48-
| `dev` | If false, skip all dev-only code path |
49-
| `verbose` | Enables logging |
67+
| `max-workers` | Specifies the maximum number of workers the worker-pool will spawn for transforming files. Overrides [`maxWorkers`](./Configuration.md#maxworkers) which defaults to a number based on `os.availableParallelism()`. |
68+
| `dev` | If false, skip all dev-only code path. Defaults to `true` |
69+
| `verbose` | Enables logging. Defaults to `false` |

0 commit comments

Comments
 (0)