From 7a3275a1e8d29fa5f758dc698e947c7296d00b03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Nison?= Date: Fri, 2 Feb 2018 04:35:09 -0800 Subject: [PATCH] Writes documentation for Metro's API Reviewed By: davidaurelio Differential Revision: D6808245 fbshipit-source-id: 4c153c2c9dc8ddef3ebc564507281792267a2cc0 --- docs/API.md | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/docs/API.md b/docs/API.md index 11f97e2c..44db1e19 100644 --- a/docs/API.md +++ b/docs/API.md @@ -9,8 +9,59 @@ title: API --- +## Quick Start + + - Compile a file + + ``` + await Metro.runBuild({ + entry: 'index.js', + out: 'bundle.js', + config: Metro.loadMetroConfiguration(), + }); + ``` + + - Run a server & watch the filesystem for changes + + ``` + await Metro.runServer({ + port: 8080, + config: Metro.loadMetroConfiguration(), + }); + ``` + ## Reference -### `todo(fn)` +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 by simply requiring this file. -*TODO* +### `loadMetroConfiguration(filepath?: string, )` + +**Basic options:** `cwd`, `basename` + +Load the Metro configuration, either from `filepath` if specified, or by traversing the directory hierarchy from `cwd` to the root until it finds a file named `basename` (by default `metro.config.js`). The returned configuration will have been normalized and merged with Metro's default values. + +### `findMetroConfiguration(filepath?: string, )` + +**Basic options:** `cwd`, `basename` + +Same as above, but only locates the file. + +### `async runBuild()` + +**Required options:** `entry`, `out` + +**Basic options:** `dev`, `optimize`, `platform`, `sourceMap`, `sourceMapUrl` + +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 + +### `async runServer()` + +**Basic options:** `host`, `port`, `secure`, `secureKey`, `secureCert`, `hmrEnabled` + +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 `secure` family of options are present, the server will be exposed over HTTPS. If `hmrEnabled` is set, the server will also expose a websocket server and inject the HMR client into the generated bundles. + +### `createConnectMiddleware()` + +**Basic options:** `port` + +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.