Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundle script instead of curl #851

Merged
merged 2 commits into from
Apr 21, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Examples/SampleApp/iOS/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/Examples/SampleApp/index.ios.bundle"];

// OPTION 2
// Load from pre-bundled file on disk. To re-generate the static bundle, run
// Load from pre-bundled file on disk. To re-generate the static bundle,
// from the root of your project directory, run
//
// $ curl 'http://localhost:8081/Examples/SampleApp/index.ios.bundle?dev=false&minify=true' -o iOS/main.jsbundle
// $ react-native bundle
//
// and uncomment the next following line
// jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
Expand Down
8 changes: 4 additions & 4 deletions docs/RunningOnDevice.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ You can also pack all the JavaScript code within the app itself. This way you ca
1. Open `iOS/AppDelegate.m`
2. Follow the instructions for "OPTION 2":
* Uncomment `jsCodeLocation = [[NSBundle mainBundle] ...`
* Run given `curl` command in terminal from the root directory of your app
* Run the `react-native bundle` command in terminal from the root directory of your app

Packager supports a couple of options:
The bundle script supports a couple of flags:

* `dev` (true by default) - sets the value of `__DEV__` variable. When `true` it turns on a bunch of useful development warnings. For production it is recommended to use `dev=false`.
* `minify` (false by default) - whether or not to pipe the JS code through UglifyJS.
* `--dev` - sets the value of `__DEV__` variable to true. When `true` it turns on a bunch of useful development warnings. For production it is recommended to set `__DEV__=false`.
* `--minify` - pipe the JS code through UglifyJS.

## Troubleshooting

Expand Down
65 changes: 65 additions & 0 deletions local-cli/bundle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var http = require('http');
var fs = require('fs');
var path = require('path');
var chalk = require('chalk');
var blacklist = require('../packager/blacklist.js');
var ReactPackager = require('../packager/react-packager');

var OUT_PATH = 'iOS/main.jsbundle';

function getBundle(flags) {

var options = {
projectRoots: [path.resolve(__dirname, '../../..')],
transformModulePath: require.resolve('../packager/transformer.js'),
assetRoots: [path.resolve(__dirname, '../../..')],
cacheVersion: '2',
blacklistRE: blacklist('ios')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can revisit this later when Android ships, no need for extra CLI option for now (you had this on your TODO comment)

};

var url = '/index.ios.bundle?dev=' + flags.dev;

console.log('Building package...');
ReactPackager.buildPackageFromUrl(options, url)
.done(function(bundle) {
console.log('Build complete');
fs.writeFile(OUT_PATH, bundle.getSource({
inlineSourceMap: false,
minify: flags.minify
}), function(err) {
if (err) {
console.log(chalk.red('Error saving bundle to disk'));
throw err;
} else {
console.log('Successfully saved bundle to ' + OUT_PATH);
}
});
});
}

function showHelp() {
console.log([
'Usage: react-native bundle [options]',
'',
'Options:',
' --dev\t\tsets DEV flag to true',
' --minify\tminify js bundle'
].join('\n'));
process.exit(1);
}

module.exports = {
init: function(args) {
var flags = {
help: args.indexOf('--help') !== -1,
dev: args.indexOf('--dev') !== -1,
minify: args.indexOf('--minify') !== -1
}

if (flags.help) {
showHelp();
} else {
getBundle(flags);
}
}
}
7 changes: 6 additions & 1 deletion local-cli/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
var spawn = require('child_process').spawn;
var path = require('path');
var install = require('./install.js');
var bundle = require('./bundle.js');

function printUsage() {
console.log([
'Usage: react-native <command>',
'',
'Commands:',
' start: starts the webserver',
' install: installs npm react components'
' install: installs npm react components',
' bundle: builds the javascript bundle for offline use'
].join('\n'));
process.exit(1);
}
Expand All @@ -36,6 +38,9 @@ function run() {
case 'install':
install.init();
break;
case 'bundle':
bundle.init(args);
break;
default:
console.error('Command `%s` unrecognized', args[0]);
printUsage();
Expand Down