简体   繁体   中英

React-Native Packager API

So before I get to the problem, here's what I'm trying to do. I want to be able to feed my index.ios.js file to the React packager and have it generate a bundle for me. I'd like to do this all within JS (eg I don't want to run a package server that is monitoring my js files, etc.). In other words, I want to keep the packager very light-weight and I want it to be easy to create many instances of the packager by just creating a new packager instance and calling some JS.

As such, I've been trying to cobble together a JS script to do this but I have been having a tough time. Here's my script so far (excuse any bad style as I'm coming from Obj-C land and this is my first big NodeJS project):

var ReactPackager = require('react-native/packager/react-packager')
var AssetServer = require('react-native/packager/react-packager/src/AssetServer');
var Bundler = require('react-native/packager/react-packager/src/Bundler')
var FileWatcher = require('node-haste').FileWatcher;

const transformModulePath = `${__dirname}/node_modules/react-native/packager/transformer.js`;
const bundler = new Bundler({
  projectRoots: [__dirname],
  fileWatcher: FileWatcher.createDummyWatcher(),
  assetServer: new AssetServer({
    projectRoots: [__dirname],
    assetExts: [],
  }),
  transformModulePath: transformModulePath
});

bundler.bundle({
  entryFile: `${__dirname}/tmp/index.ios.js`,
  dev: false,
  minify: true,
  platform: 'ios',
});

Cool! So the packager is chugging along but it eventually gets stuck at:

[8:16:33 AM] <START> Building Dependency Graph
[8:16:33 AM] <START> Crawling File System
[8:16:33 AM] <START> find dependencies
[8:16:35 AM] <END>   Crawling File System (2203ms)
[8:16:35 AM] <START> Building in-memory fs for JavaScript
[8:16:35 AM] <END>   Building in-memory fs for JavaScript (107ms)
[8:16:35 AM] <START> Building Haste Map

Failed to build DependencyGraph: @providesModule naming collision:
  Duplicate module name: React
  Paths: /Users/staufman/dev/karma/.KarmaTemplate/node_modules/react/lib/React.js collides with /Users/staufman/dev/karma/.KarmaTemplate/node_modules/react-native/Libraries/ReactNative/React.js

This error is caused by a @providesModule declaration with the same name accross two different files.
Error: @providesModule naming collision:
  Duplicate module name: React
  Paths: /Users/staufman/dev/karma/.KarmaTemplate/node_modules/react/lib/React.js collides with /Users/staufman/dev/karma/.KarmaTemplate/node_modules/react-native/Libraries/ReactNative/React.js

Ok, so it looks like the installed React and React-Native modules are colliding. If I remove the React module, I get:

[8:20:42 AM] <START> Building Dependency Graph
[8:20:42 AM] <START> Crawling File System
[8:20:42 AM] <START> find dependencies
[8:20:44 AM] <END>   Crawling File System (1634ms)
[8:20:44 AM] <START> Building in-memory fs for JavaScript
[8:20:44 AM] <END>   Building in-memory fs for JavaScript (100ms)
[8:20:44 AM] <START> Building Haste Map
[8:20:44 AM] <END>   Building Haste Map (84ms)
[8:20:44 AM] <END>   Building Dependency Graph (1826ms)
transformed 1/2 (50%)

And then, it just gets stuck at this step. I've tried debugging the internals but I actually have no clue where that message (transformed 1/2) gets printed and why it's bugging out. I'm guessing the transformer is trying to utilize one of its workers but because I'm reaching behind the curtain and running the packager manually, the workers haven't been spawned properly? That's a complete shot in the dark but I'm lost.

Any help would be much appreciated. Thanks!

React Native CLI已经提供了bundle命令,您可以使用如下命令

react-native bundle --entry-file index.ios.js --platform ios --dev false --bundle-output dist/app.bundle.js

After working through this issue on and off, it turns out that running the server is a workable approach. The key was realizing that the process was I/O bound, not just CPU bound.

While bundling, the server would just stall and I realized that it might have to do with the limited RAM available on an AWS instance.

I found this great article on how to increase the swap space of my Linux box:

https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04

By adding 2GB of swap space, the bundling server no longer stalls. Turns out it must have been an issue of not having enough RAM!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM