🚇 The JavaScript bundler for React Native. https://facebook.github.io/metro
Go to file
Ben Alpert e41008f208 Consume react, fbjs from npm
Summary:
We don't (yet) treat these the same as any other modules because we still have special resolution rules for them in the packager allowing the use of `providesModule`, but I believe this allows people to use npm react in their RN projects and not have duplicate copies of React. Fixes facebook/react-native#2985.

This relies on fbjs 0.6, which includes `.flow` files alongside the `.js` files to allow them to be typechecked without additional configuration. This also uses react 0.14.5, which shims a couple of files (as `.native.js`) to avoid DOM-specific bits. Once we fix these in React, we will use the same code on web and native. Hopefully we can also remove the packager support I'm adding here for `.native.js`.

This diff is not the desired end state for us – ideally the packager would know nothing of react or fbjs, and we'll get there eventually by not relying on `providesModule` in order to load react and fbjs modules. (fbjs change posted here but not merged yet: https://github.com/facebook/fbjs/pull/84.)

This should also allow relay to work seamlessly with RN, but I haven't verified this.

public

Reviewed By: sebmarkbage

Differential Revision: D2786197

fb-gh-sync-id: ff50f28445e949edc9501f4b599df7970813870d
2015-12-30 11:41:09 -08:00
react-packager Consume react, fbjs from npm 2015-12-30 11:41:09 -08:00
README.md getTransformOptionsModulePath 2015-12-24 01:02:30 -08:00
babelRegisterOnly.js Upgrade transformers to be compatible babel 6 2015-11-10 10:55:23 -08:00
blacklist.js Consume react, fbjs from npm 2015-12-30 11:41:09 -08:00
launchPackager.command Fix launchCommand 2015-11-11 04:39:28 -08:00
package.json Fix cli entry points 2015-10-22 10:13:13 -07:00
packager.sh Fix cli entry points 2015-10-22 10:13:13 -07:00
react-native-xcode.sh Add support for nodenv with xcode 2015-12-17 14:10:36 -08:00
rn-cli.config.js Allow user to configure transformer through cl argument 2015-10-16 13:26:22 -07:00
transformer.js Support plugins that conform to ES6 modules 2015-12-04 07:45:29 -08:00

README.md

React Native Packager

React Native Packager is a project similar in scope to browserify or webpack, it provides a CommonJS-like module system, JavaScript 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 cycles. Additionally, we don't want users -- with large code bases -- 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 discourage people to use this module format because going forward, we 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.

React Native users need not to understand how the packager work, however, this documentation might be useful for advanced users and 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 endpoint then the source map will be generated from that package
  • if the package has not been previously asked for, this will go through the same steps outlined in the .bundle endpoint then generate the source map.

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 which will effect how the React Native core libraries behave.
  • 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 a require('moduleName') the end of your bundle.
  • inlineSourceMap boolean, defaults to false: whether to inline source maps.

/debug

This is a page used for debugging, it has links to two pages:

  • Cached Packages: which shows you the packages that's been already generated and cached
  • Dependency Graph: is the in-memory graph of all the modules and their dependencies

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.

var ReactPackager = require('./react-packager');

ReactPackager.middleware(options)

Returns a function that can be used in a connect-like middleware. Takes the following options:

  • projectRoots array (required): Is the roots where your JavaScript file will exist
  • blacklistRE regexp: Is a patter to ignore certain paths from the packager
  • polyfillModuleName array: Paths to polyfills you want to be included at the start of the bundle
  • cacheVersion string: used in creating the cache file
  • resetCache boolean, defaults to false: whether to use the cache on 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
  • assetRoots array: Where should the packager look for assets
  • getTransformOptionsModulePath string: Path to module that exports a function that acts as a middleware for generating options to pass to the transformer based on the bundle and module being transformed.

ReactPackager.buildPackageFromUrl(options, url)

Build a package from a url (see the .bundle endpoint). options is the same options that is passed to ReactPackager.middleware

ReactPackager.getDependencies(options, main)

Given an entry point module. Recursively collect all the dependent modules and return it as an array. options is the same options that is passed to ReactPackager.middleware

Debugging

To get verbose output when running the packager, define an environment variable:

export DEBUG=ReactNativePackager:*

You can combine this with other values, e.g. DEBUG=babel,ReactNativePackager:*. Under the hood this uses the debug package, see its documentation for all the available options.

The /debug endpoint discussed above is also useful.

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?

We love webpack, however, when we tried on our codebase it was slower than our developers would like it to be. You can find more discussion about the subject here.