2015-11-20 19:07:09 +00:00
# Re-Natal
2017-02-22 08:02:41 +00:00
### A utility for building ClojureScript-based React Native apps
Artur Girenko
2015-11-21 11:30:33 +00:00
[@drapanjanas ](https://twitter.com/drapanjanas )
2015-08-30 04:01:40 +00:00
---
2017-02-22 08:02:41 +00:00
Re-Natal is a simple command-line utility that automates most of the process of
setting up a React Native app running on ClojureScript with [Reagent] + [re-frame ](https://github.com/Day8/re-frame ), [Om.Next] or [Rum].
2015-11-20 19:07:09 +00:00
2017-02-22 08:02:41 +00:00
This project is a fork of [dmotz/natal ](https://github.com/dmotz/natal ) by Dan Motzenbecker.
2015-11-27 23:45:38 +00:00
2017-02-22 08:02:41 +00:00
Figwheel support is based on the brilliant solution developed by Will Decker [decker405/figwheel-react-native ](https://github.com/decker405/figwheel-react-native ),
which works on both platforms.
2015-08-30 04:01:40 +00:00
2015-08-30 04:02:14 +00:00
2015-12-15 20:07:50 +00:00
For more ClojureScript React Native resources visit [cljsrn.org ](http://cljsrn.org ).
2017-02-22 08:02:41 +00:00
Contributions are very welcome.
## Status
2017-05-01 11:29:42 +00:00
- Uses [React Native] v0.43.4
2017-02-22 08:02:41 +00:00
- Reusable codebase between iOS and Android
- Figwheel used for REPL and live coding
- Works in iOS (real device and simulator)
- Works in Android (real device and simulators, specifically AVD and Genymotion)
- Figwheel REPL can be started from within nREPL
- Simultaneous development of iOS and Android apps
- Manual reload and automatic hot reload
- Custom react-native components supported
- Source maps available
2016-05-03 19:31:39 +00:00
- Supported React wrappers:
2017-02-22 08:02:41 +00:00
[Reagent], [Om.Next], and [Rum]
2017-05-01 11:30:34 +00:00
- Support of windows (UWP and WPF) apps
2017-02-22 08:02:41 +00:00
- [Unified way of using static images of React Native 0.14+ ](https://facebook.github.io/react-native/docs/images.html ) supported
2015-11-27 23:45:38 +00:00
2017-02-22 08:02:41 +00:00
## Dependencies
As Re-Natal is an orchestration of many individual tools, there are quite a few dependencies.
If you've previously done React Native or Clojure development, you should hopefully
have most installed already. Platform dependencies are listed under their respective tools.
- [npm ](https://www.npmjs.com ) `>=1.4`
- [Node.js ](https://nodejs.org ) `>=4.0.0`
- [react-native-cli ](https://www.npmjs.com/package/react-native-cli ) `>=0.1.7` (install with `npm install -g react-native-cli` )
- [Leiningen ](http://leiningen.org ) `>=2.5.3`
- [Java 8 ](http://www.oracle.com/technetwork/java/javase/downloads/index.html )
For iOS development:
- [Xcode ](https://developer.apple.com/xcode ) (+ Command Line Tools) `>=6.3`
- [OS X ](http://www.apple.com/osx ) `>=10.10`
## Creating a new project
2015-08-31 02:03:00 +00:00
2015-10-03 18:50:12 +00:00
Before getting started, make sure you have the
2016-01-08 20:10:11 +00:00
[required dependencies ](#dependencies ) installed.
2015-08-31 02:03:00 +00:00
Then, install the CLI using npm:
2015-08-30 04:02:14 +00:00
```
2015-11-20 19:07:09 +00:00
$ npm install -g re-natal
2015-08-30 04:02:14 +00:00
```
2017-02-22 08:02:41 +00:00
To generate a new app, run `re-natal init` with your app's name as an argument:
2015-08-30 04:02:14 +00:00
```
2015-11-20 19:07:09 +00:00
$ re-natal init FutureApp
2015-08-30 04:02:14 +00:00
```
2017-02-22 08:02:41 +00:00
2017-04-26 19:35:11 +00:00
This will generate a project which uses Reagent v0.6.
2017-02-22 08:02:41 +00:00
You may specify the -i option to choose a specific React wrapper: Om.Next, Reagent v0.6 or Rum:
2016-02-11 12:18:21 +00:00
```
2016-05-03 19:31:39 +00:00
$ re-natal init FutureApp -i [om-next | reagent6 | rum]
2016-02-11 12:18:21 +00:00
```
2015-08-30 04:02:14 +00:00
2015-10-17 17:17:29 +00:00
If your app's name is more than a single word, be sure to type it in CamelCase.
2015-08-30 04:02:14 +00:00
A corresponding hyphenated Clojure namespace will be created.
2017-02-22 08:02:41 +00:00
The init process will take a few minutes — coffee break! If all goes well you should see basic instructions on how to run in iOS simulator.
2015-11-27 23:45:38 +00:00
2016-04-08 19:48:11 +00:00
## Development with Figwheel
2017-02-22 08:02:41 +00:00
Initially the `index.*.js` files are generated with the production profile, ready for deployment.
However, during development it is better to use the development profile and integrate with Figwheel. Switch to the development profile with:
2015-11-27 23:45:38 +00:00
```
$ re-natal use-figwheel
2015-11-28 15:42:37 +00:00
```
2015-10-04 23:29:36 +00:00
2017-02-22 08:02:41 +00:00
This command needs to be run every time you switch to the development profile or specify a development environment (with `use-ios-device` or `use-android-device` ).
2016-04-08 19:48:11 +00:00
2017-02-22 08:02:41 +00:00
NOTE: You might need to restart React Native Packager and reload your app.
2016-04-08 19:48:11 +00:00
2017-02-22 08:02:41 +00:00
Start the Figwheel REPL with
2016-05-25 05:44:37 +00:00
2016-04-08 19:48:11 +00:00
```
2017-02-22 08:02:41 +00:00
$ lein figwheel [ios | android]
2016-04-08 19:48:11 +00:00
```
2017-02-22 08:02:41 +00:00
If all went well you should see the REPL prompt and changes in source files should be hot-loaded by Figwheel.
2016-04-08 19:48:11 +00:00
2017-02-22 08:02:41 +00:00
#### Starting Figwheel REPL from nREPL
To start Figwheel from within nREPL session:
2016-04-08 19:48:11 +00:00
```
2017-02-22 08:02:41 +00:00
$ lein repl
```
Then in the nREPL prompt type:
2016-04-08 19:48:11 +00:00
```
2017-02-22 08:02:41 +00:00
user=> (start-figwheel "ios")
2016-11-21 18:53:27 +00:00
```
2017-02-22 08:02:41 +00:00
Or, for Android build type:
2016-11-21 18:53:27 +00:00
```
2017-02-22 08:02:41 +00:00
user=> (start-figwheel "android")
2016-04-08 19:48:11 +00:00
```
2017-02-22 08:02:41 +00:00
Or, for both type:
```
user=> (start-figwheel "ios" "android")
```
## Running the app
### Note for Linux users
On Linux, the React Native Packager has to be started manually with
```
react-native start
2016-04-08 19:48:11 +00:00
```
2017-02-22 08:02:41 +00:00
See [here ](#running-on-linux ) for more details.
2016-04-08 19:48:11 +00:00
2017-02-22 08:02:41 +00:00
### iOS
#### Using iOS simulator
2016-04-08 19:48:11 +00:00
2017-02-22 08:02:41 +00:00
```
re-natal use-ios-device simulator
react-native run-ios
```
2016-03-03 22:33:45 +00:00
2017-02-22 08:02:41 +00:00
#### Using real iOS device
2016-03-03 22:33:45 +00:00
2017-02-22 08:02:41 +00:00
```
re-natal use-ios-device real
```
If this doesn't correctly detect your computer's IP you can pass your IP address explicitly: `re-natal use-ios-device <IP address>` .
And then run
2016-03-03 22:33:45 +00:00
2015-11-28 15:42:37 +00:00
```
2017-02-22 08:02:41 +00:00
react-native run-ios
2015-11-28 15:42:37 +00:00
```
2017-02-22 08:02:41 +00:00
2017-02-23 23:11:26 +00:00
#### Switching between iOS devices
Run `use-ios-device` to configure device type you want to use in development:
2015-12-06 20:17:52 +00:00
```
2017-02-22 08:02:41 +00:00
$ re-natal use-ios-device < real | simulator >
2015-12-06 20:17:52 +00:00
$ re-natal use-figwheel
2017-02-22 08:02:41 +00:00
$ lein figwheel ios
2015-12-06 20:17:52 +00:00
```
2017-02-22 08:02:41 +00:00
2017-02-23 23:11:26 +00:00
---
### Android
2017-02-22 08:02:41 +00:00
#### Using Android Virtual Device (AVD)
[Set up a virtual device in AVD ](https://developer.android.com/studio/run/managing-avds.html ). Start the virtual device then run
2015-12-06 20:17:52 +00:00
```
2017-02-22 08:02:41 +00:00
$ re-natal use-android-device avd
$ re-natal use-figwheel
$ lein figwheel android
2015-12-06 20:23:14 +00:00
$ react-native run-android
2015-12-06 20:17:52 +00:00
```
2017-02-22 08:02:41 +00:00
2016-04-08 19:48:11 +00:00
#### Using Genymotion simulator
2017-02-22 08:02:41 +00:00
Set up and start the Genymotion simulator then run
2015-12-06 20:17:52 +00:00
```
2016-01-24 12:46:34 +00:00
$ re-natal use-android-device genymotion
$ re-natal use-figwheel
2015-12-06 20:17:52 +00:00
$ lein figwheel android
2015-12-06 20:23:14 +00:00
$ react-native run-android
2015-12-06 20:17:52 +00:00
```
2015-11-28 15:42:37 +00:00
2017-02-22 08:02:41 +00:00
#### Using real Android device
To run figwheel with real Android device please read [Running on Device ](https://facebook.github.io/react-native/docs/running-on-device-android.html#content ).
To make it work on a USB connected device I also had to run:
2015-12-06 20:17:52 +00:00
```
2017-02-22 08:02:41 +00:00
$ adb reverse tcp:8081 tcp:8081
$ adb reverse tcp:3449 tcp:3449
2015-12-06 20:17:52 +00:00
```
2017-02-22 08:02:41 +00:00
Then:
2015-12-06 20:17:52 +00:00
```
2017-02-26 20:29:17 +00:00
$ re-natal use-android-device real
2017-02-22 08:02:41 +00:00
$ re-natal use-figwheel
$ lein figwheel android
2015-12-06 20:23:14 +00:00
$ react-native run-android
2015-12-06 20:17:52 +00:00
```
2017-02-22 08:02:41 +00:00
2016-03-03 22:33:45 +00:00
#### Switching between Android devices
2016-01-24 17:07:46 +00:00
Run `use-android-device` to configure device type you want to use in development:
2016-01-24 12:46:34 +00:00
```
$ re-natal use-android-device < real | genymotion | avd >
$ re-natal use-figwheel
$ lein figwheel android
```
2017-02-22 08:02:41 +00:00
### Developing iOS and Android apps simultaneously
2016-01-24 12:46:34 +00:00
```
$ re-natal use-figwheel
$ lein figwheel ios android
```
2015-11-28 15:42:37 +00:00
2017-02-22 08:02:41 +00:00
---
2016-03-21 22:08:01 +00:00
2017-02-22 08:02:41 +00:00
### Using external React Native Components
2016-03-21 21:50:54 +00:00
2017-02-22 08:02:41 +00:00
Lets say you have installed an external library from npm like this:
2015-12-15 18:25:50 +00:00
```
2017-02-22 08:02:41 +00:00
$ npm i some-library --save
2015-12-15 18:25:50 +00:00
```
2017-02-22 08:02:41 +00:00
And you want to use a component called 'some-library/Component':
```clojure
(def Component (js/require "some-library/Component"))
2015-12-15 18:25:50 +00:00
```
2017-02-22 08:02:41 +00:00
This would work when you do `lein prod-build` and run your app, but will fail when you run with Figwheel.
The React Native packager statically scans for all calls to `require` and prepares the required
code to be available at runtime. But, dynamically loaded (by Figwheel) code bypasses this scan
and therefore requiring the custom component fails.
To overcome this run `use-component` :
2015-12-15 18:25:50 +00:00
```
2017-02-22 08:02:41 +00:00
$ re-natal use-component some-library/Component
2015-12-15 18:25:50 +00:00
```
2017-02-22 08:02:41 +00:00
or for a platform-specific component use the optional platform parameter:
2016-01-24 12:46:34 +00:00
```
2017-02-22 08:02:41 +00:00
$ re-natal use-component some-library/ComponentIOS ios
2016-01-24 12:46:34 +00:00
```
2017-02-22 08:02:41 +00:00
Then, regenerate index.*.js files:
```
$ re-natal use-figwheel
2015-12-15 18:25:50 +00:00
```
2017-02-22 08:02:41 +00:00
Lastly, you will have to restart the packager and reload your app.
NOTE: If you mistyped something, or no longer use the component and would like to remove it,
manually open `.re-natal` and fix it there (it's just a list of names in JSON format, so the process should be straight forward).
2015-12-15 18:25:50 +00:00
2015-11-28 15:42:37 +00:00
## REPL
2015-12-06 20:17:52 +00:00
You have to reload your app, and should see the REPL coming up with the prompt.
2015-11-27 23:45:38 +00:00
At the REPL prompt, try loading your app's namespace:
2015-08-30 04:16:58 +00:00
```clojure
2015-11-21 11:01:44 +00:00
(in-ns 'future-app.ios.core)
2015-08-30 04:16:58 +00:00
```
2015-10-04 23:29:36 +00:00
Changes you make via the REPL or by changing your `.cljs` files should appear live
in the simulator.
2015-08-30 04:16:58 +00:00
Try this command as an example:
```clojure
2015-11-22 09:57:51 +00:00
(dispatch [:set-greeting "Hello Native World!"])
2015-08-30 04:16:58 +00:00
```
2017-02-22 08:02:41 +00:00
2015-12-16 06:51:55 +00:00
## Running on Linux
In addition to the instructions above on Linux you might need to
start React Native packager manually with command `react-native start` .
2015-12-16 11:30:01 +00:00
This was reported in [#3 ](https://github.com/drapanjanas/re-natal/issues/3 )
2015-12-16 06:51:55 +00:00
2016-11-14 02:11:45 +00:00
See this [tutorial ](https://gadfly361.github.io/gadfly-blog/2016-11-13-clean-install-of-ubuntu-to-re-natal.html ) on how to set up and run re-natal on a clean install of Ubuntu.
2015-12-16 06:51:55 +00:00
See also [Linux and Windows support ](https://facebook.github.io/react-native/docs/linux-windows-support.html )
in React Native docs.
2015-08-30 04:16:58 +00:00
2017-04-26 19:35:11 +00:00
## Support of UWP and WPF apps (using react-native-windows)
To start new project with UWP app:
```
$ re-natal init FutureApp -u
```
To start new project with WPF app:
```
$ re-natal init FutureApp -w
```
Existing projects can also add windows platforms any time using commands:
```
2017-04-30 18:10:28 +00:00
$ re-natal add-platform windows
2017-04-26 19:35:11 +00:00
or
2017-04-30 18:10:28 +00:00
$ re-natal add-platform wpf
2017-04-26 19:35:11 +00:00
```
Note: for projects generated with re-natal version prior to 0.4.0 additional windows builds will not be added automatically to `project.clj` .
Workaround is to generate fresh windows project and copy-paste additional builds manually.
2017-02-22 08:02:41 +00:00
## Production build
2015-11-27 23:45:38 +00:00
Do this with command:
2015-11-21 09:42:24 +00:00
```
2015-11-27 23:45:38 +00:00
$ lein prod-build
2015-11-21 09:42:24 +00:00
```
2017-02-22 08:02:41 +00:00
Follow the [React Native documentation ](https://facebook.github.io/react-native/docs/signed-apk-android.html ) to proceed with the release.
2015-11-21 11:01:44 +00:00
2016-01-17 17:51:29 +00:00
## Static Images
2016-01-17 18:12:37 +00:00
Since version 0.14 React Native supports a [unified way of referencing static images ](https://facebook.github.io/react-native/docs/images.html )
2016-01-17 17:51:29 +00:00
2016-01-17 18:12:37 +00:00
In Re-Natal skeleton images are stored in "images" directory. Place your images there and reference them from cljs code:
2016-01-17 17:51:29 +00:00
```clojure
(def my-img (js/require "./images/my-img.png"))
```
#### Adding an image during development
2017-02-22 08:02:41 +00:00
When you have dropped a new image to "images" dir, you need to restart React Native packager and re-run command:
2016-01-17 17:51:29 +00:00
```
$ re-natal use-figwheel
```
This is needed to regenerate index.\*.js files which includes `require` calls to all local images.
After this you can use a new image in your cljs code.
2015-12-06 10:40:40 +00:00
## Upgrading existing Re-Natal project
2016-06-04 08:08:28 +00:00
#### Upgrading React Native version
To upgrade React Native to newer version please follow the official
[Upgrading ](https://facebook.github.io/react-native/docs/upgrading.html ) guide of React Native.
Re-Natal makes almost no changes to the files generated by react-native so the official guide should be valid.
#### Upgrading Re-Natal CLI version
2015-12-06 10:40:40 +00:00
Do this if you want to use newer version of re-natal.
Commit or backup your current project, so that you can restore it in case of any problem ;)
Upgrade re-natal npm package
```
$ npm upgrade -g re-natal
```
In root directory of your project run
```
$ re-natal upgrade
```
This will overwrite only some files which usually contain fixes in newer versions of re-natal,
and are unlikely to be changed by the user. No checks are done, these files are just overwritten:
- files in /env directory
- figwheel-bridge.js
2015-12-06 11:31:45 +00:00
Then to continue development using figwheel
```
$ re-natal use-figwheel
```
2017-02-22 08:02:41 +00:00
## Enabling source maps when debugging in chrome
2016-02-06 15:00:09 +00:00
To make source maps available in "Debug in Chrome" mode re-natal patches
2016-01-21 21:27:36 +00:00
the react native packager to serve \*.map files from file system and generate only index.\*.map file.
2016-02-06 15:00:09 +00:00
To achieve this [this line ](https://github.com/facebook/react-native/blob/master/packager/react-packager/src/Server/index.js#L413 )
2016-01-21 21:27:36 +00:00
of file "node_modules/react-native/packager/react-packager/src/Server/index.js" is modified to match only index.\*.map
2015-12-06 10:40:40 +00:00
2016-02-06 15:00:09 +00:00
To do this run: `re-natal enable-source-maps` and restart packager.
You can undo this any time by deleting `node_modules` and running `re-natal deps`
2016-01-26 19:03:56 +00:00
## Example Apps
* [Luno ](https://github.com/alwx/luno-react-native ) is a demo mobile application written in ClojureScript.
2016-05-13 22:18:11 +00:00
* [Re-Navigate ](https://github.com/vikeri/re-navigate ) example of using new Navigation component [NavigationExperimental ](https://github.com/ericvicenti/navigation-rfc )
2016-02-26 20:10:24 +00:00
* [Showcase of iOS navigation ](https://github.com/seantempesta/om-next-react-native-router-flux ) with react-native-router-flux and Om.Next
2016-05-24 19:22:53 +00:00
* [Catlantis ](https://github.com/madvas/catlantis ) is a funny demo application about cats
2016-10-14 17:44:44 +00:00
* [Lymchat ](https://github.com/tiensonqin/lymchat ) App to learn different cultures. Lym is available in [App Store ](https://itunes.apple.com/us/app/lym/id1134985541?ls=1&mt=8 ).
2017-02-22 08:02:41 +00:00
* [Status ](https://status.im ) ([Github](https://github.com/status-im/status-react/)): a web3 browser, messenger, and gateway to a decentralised world of Ethereum. re-natal + re-frame + cljs + golang
* [React Native TodoMVC ](https://github.com/TashaGospel/todo-mvc-re-natal ) is a mobile application which attempts to implement TodoMVC in Clojurescript and React Native.
2016-01-26 19:03:56 +00:00
2015-08-30 04:02:33 +00:00
## Tips
2015-10-04 23:29:36 +00:00
- Having `rlwrap` installed is optional but highly recommended since it makes
the REPL a much nicer experience with arrow keys.
- Running multiple React Native apps at once can cause problems with the React
Packager so try to avoid doing so.
- You can launch your app on the simulator without opening Xcode by running
2016-02-06 13:37:49 +00:00
`react-native run-ios` in your app's root directory (since RN 0.19.0).
2015-10-04 23:29:36 +00:00
2015-11-21 11:07:33 +00:00
- To change advanced settings run `re-natal xcode` to quickly open the Xcode project.
2015-08-30 04:02:33 +00:00
2016-02-06 17:03:04 +00:00
- If you have customized project layout and `re-natal upgrade` does not fit you well,
2016-02-06 13:37:49 +00:00
then these commands might be useful for you:
2016-02-06 17:03:04 +00:00
* `re-natal copy-figwheel-bridge` - just copies figwheel-bridge.js from current re-natal
2015-10-05 00:55:16 +00:00
2017-02-22 08:02:41 +00:00
## Local Development of Re-Natal
2016-05-11 19:07:31 +00:00
If you would like to run any of this on your local environment first clone the code to an appropriate place on your machine and install dependencies
```
$ git clone https://github.com/drapanjanas/re-natal.git
$ cd re-natal
$ npm install
```
To test any changes made to re-natal, cd to an already existing project or a brand new dummy project:
```
$ cd ../already-existing
```
and run the re-natal command line like so
```
2016-05-11 19:08:25 +00:00
$ node ../re-natal/index.js
2016-05-11 19:07:31 +00:00
Usage: re-natal [options] [command]
Commands:
2017-05-01 11:32:46 +00:00
init [options] < name > create a new ClojureScript React Native project
upgrade upgrades project files to current installed version of re-natal (the upgrade of re-natal itself is done via npm)
add-platform < platform > adds additional app platform: 'windows' - UWP app, 'wpf' - WPF app
xcode open Xcode project
deps install all dependencies for the project
use-figwheel generate index.*.js for development with figwheel
use-android-device < type > sets up the host for android device type: 'real' - localhost, 'avd' - 10.0.2.2, 'genymotion' - 10.0.3.2, IP
use-ios-device < type > sets up the host for ios device type: 'simulator' - localhost, 'real' - auto detect IP on eth0, IP
use-component < name > [< platform > ] configures a custom component to work with figwheel. name is the value you pass to (js/require) function.
enable-source-maps patches RN packager to server *.map files from filesystem, so that chrome can download them.
copy-figwheel-bridge copy figwheel-bridge.js into project
2016-05-11 19:07:31 +00:00
Options:
-h, --help output usage information
-V, --version output the version number
```
You can then run any of the commands manually.
2017-02-22 08:02:41 +00:00
[React Native]: https://facebook.github.io/react-native
[Reagent]: https://github.com/reagent-project/reagent
[Om.Next]: https://github.com/omcljs/om/wiki/Quick-Start-(om.next)
2017-02-23 23:11:26 +00:00
[Rum]: https://github.com/tonsky/rum