2015-03-26 01:19:28 +00:00
React Native Packager
--------------------
React Native Packager is a project similar in scope to browserify or
2015-04-01 15:46:12 +00:00
webpack, it provides a CommonJS-like module system, JavaScript
2015-03-26 01:19:28 +00:00
compilation (ES6, Flow, JSX), bundling, and asset loading.
The main difference is the Packager's focus on compilation and
bundling speed. We aim for a sub-second edit-reload
2015-04-01 15:46:12 +00:00
cycles. Additionally, we don't want users -- with large code bases --
2015-03-26 01:19:28 +00:00
to wait more than a few seconds after starting the packager.
The main deviation from the node module system is the support for our
proprietary module format known as `@providesModule` . However, we
2016-02-16 19:50:39 +00:00
discourage people from using this module format because going forward we
2015-03-26 01:19:28 +00:00
want to completely separate our infrastructure from React Native and
provide an experience most JavaScript developers are familiar with,
namely the node module format. We want to even go further, and let you
choose your own packager and asset pipeline or even integrate into
your existing infrastructure.
2015-09-16 17:30:53 +00:00
React Native users need not to understand how the packager work,
however, this documentation might be useful for advanced users and
2015-03-26 01:19:28 +00:00
people who want to fix bugs or add features to the packager (patches
welcome!).
## HTTP interface
The main way you'd interact with the packager is via the HTTP
interface. The following is the list of endpoints and their respective
functions.
### /path/to/moduleName.bundle
Does the following in order:
* parse out `path/to/moduleName`
* add a `.js` suffix to the path
* looks in your project root(s) for the file
* recursively collects all the dependencies from an in memory graph
* runs the modules through the transformer (might just be cached)
* concatenate the modules' content into a bundle
* responds to the client with the bundle (and a SourceMap URL)
### /path/to/moduleName.map
* if the package has been previously generated via the `.bundle`
2015-04-01 15:46:12 +00:00
endpoint then the source map will be generated from that package
2015-03-26 01:19:28 +00:00
* if the package has not been previously asked for, this will go
2015-04-01 15:46:12 +00:00
through the same steps outlined in the `.bundle` endpoint then
generate the source map.
2015-03-26 01:19:28 +00:00
Note that source map generation currently assumes that the code has
been compiled with jstransform, which preserves line and column
numbers which allows us to generate source maps super fast.
### /path/to/moduleName.(map|bundle) query params
You can pass options for the bundle creation through the query params,
if the option is boolean `1/0` or `true/false` is accepted.
Here are the current options the packager accepts:
* `dev` boolean, defaults to true: sets a global `__DEV__` variable
2015-06-03 18:35:32 +00:00
which will effect how the React Native core libraries behave.
2015-03-26 01:19:28 +00:00
* `minify` boolean, defaults to false: whether to minify the bundle.
* `runModule` boolean, defaults to true: whether to require your entry
point module. So if you requested `moduleName` , this option will add
2015-04-01 15:46:12 +00:00
a `require('moduleName')` the end of your bundle.
2015-03-26 01:19:28 +00:00
* `inlineSourceMap` boolean, defaults to false: whether to inline
source maps.
### /debug
2016-06-16 21:16:22 +00:00
This is a page used for debugging, it offers a link to a single page :
2015-03-26 01:19:28 +00:00
2015-04-01 15:46:12 +00:00
* Cached Packages: which shows you the packages that's been already
2015-03-26 01:19:28 +00:00
generated and cached
## Programmatic API
The packager is made of two things:
* The core packager (which we're calling ReactPackager)
* The scripts, devtools launcher, server run etc.
ReactPackager is how you mainly interact with the API.
```js
var ReactPackager = require('./react-packager');
```
2016-11-23 14:15:17 +00:00
### ReactPackager.buildBundle(serverOptions, bundleOptions)
2015-03-26 01:19:28 +00:00
2016-11-23 14:15:17 +00:00
Builds a bundle according to the provided options.
#### `serverOptions`
2015-03-26 01:19:28 +00:00
* `projectRoots` array (required): Is the roots where your JavaScript
file will exist
2016-09-21 06:24:25 +00:00
* `blacklistRE` regexp: Is a pattern to ignore certain paths from the
2015-03-26 01:19:28 +00:00
packager
* `polyfillModuleName` array: Paths to polyfills you want to be
included at the start of the bundle
2015-04-01 15:46:12 +00:00
* `cacheVersion` string: used in creating the cache file
* `resetCache` boolean, defaults to false: whether to use the cache on
2015-03-26 01:19:28 +00:00
disk
* `transformModulePath` string: Path to the module used as a
JavaScript transformer
* `nonPersistent` boolean, defaults to false: Whether the server
should be used as a persistent deamon to watch files and update
itself
2016-11-28 15:27:09 +00:00
* `getTransformOptions` function: A function that acts as a middleware for
generating options to pass to the transformer based on the bundle being built.
2015-03-26 01:19:28 +00:00
2016-11-23 14:15:17 +00:00
#### `bundleOptions`
2015-03-26 01:19:28 +00:00
2016-11-23 14:15:17 +00:00
* `entryFile` string (required): the entry file of the bundle, relative to one
2016-11-24 13:28:29 +00:00
of the roots.
2016-11-23 14:15:17 +00:00
* `dev` boolean (defaults to `true` ): sets a global `__DEV__` variable
which will effect how the React Native core libraries behave.
* `minify` boolean: Whether to minify code and apply production optimizations.
* `runModule` boolean (defaults to `true` ): whether to require your entry
point module.
* `inlineSourceMap` boolean, defaults to false: whether to inline
source maps.
* `platform` string: The target platform for the build
* `generateSourceMaps` boolean: Whether to generate source maps.
* `sourceMapUrl` string: The url of the source map (will be appended to
the bundle).
2015-03-26 01:19:28 +00:00
2015-10-01 18:46:09 +00:00
## Debugging
To get verbose output when running the packager, define an environment variable:
2017-01-04 15:11:16 +00:00
export DEBUG=RNP:*
2016-03-23 16:27:19 +00:00
2017-01-04 15:11:16 +00:00
You can combine this with other values, e.g. `DEBUG=babel,RNP:*` . Under the hood this uses the [`debug` ](https://www.npmjs.com/package/debug ) package, see its documentation for all the available options.
2016-03-23 16:27:19 +00:00
2015-10-01 18:46:09 +00:00
The `/debug` endpoint discussed above is also useful.
2015-03-26 01:19:28 +00:00
## FAQ
### Can I use this in my own non-React Native project?
Yes. It's not really tied to React Native, however feature development
is informed by React Native needs.
### Why didn't you use webpack?
2015-04-01 15:46:12 +00:00
We love webpack, however, when we tried on our codebase it was slower
2015-12-16 02:34:19 +00:00
than our developers would like it to be. You can find more discussion about
the subject [here ](https://github.com/facebook/react-native/issues/5 ).