From ef209c1a33c74b7f9e4129ef32b8d261c73b8343 Mon Sep 17 00:00:00 2001 From: Arthur Lee Date: Tue, 14 Apr 2015 20:19:58 -0400 Subject: [PATCH] Added bundle command using ReactPackager Added bundle script Pipe http response straight to file Used ReactPackager directly, minor fixes --- Examples/SampleApp/iOS/AppDelegate.m | 5 ++- local-cli/bundle.js | 59 ++++++++++++++++++++++++++++ local-cli/cli.js | 7 +++- 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 local-cli/bundle.js diff --git a/Examples/SampleApp/iOS/AppDelegate.m b/Examples/SampleApp/iOS/AppDelegate.m index 5b6cc2d58e30b6..bb30741d86939d 100644 --- a/Examples/SampleApp/iOS/AppDelegate.m +++ b/Examples/SampleApp/iOS/AppDelegate.m @@ -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"]; diff --git a/local-cli/bundle.js b/local-cli/bundle.js new file mode 100644 index 00000000000000..f3dab2d44a5f64 --- /dev/null +++ b/local-cli/bundle.js @@ -0,0 +1,59 @@ +var http = require('http'); +var fs = require('fs'); +var path = require('path'); +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') + }; + + var url = '/index.ios.bundle?dev=' + flags.dev; + + console.log('Building package...'); + ReactPackager.buildPackageFromUrl(options, url) + .then(function(bundle) { + console.log('Build complete'); + fs.writeFile(OUT_PATH, bundle.getSource({ + inlineSourceMap: false, + minify: flags.minify + }), function() { + 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); + } + } +} diff --git a/local-cli/cli.js b/local-cli/cli.js index 48dc9e5dc03b4a..99d05c7c9fc647 100644 --- a/local-cli/cli.js +++ b/local-cli/cli.js @@ -7,6 +7,7 @@ var spawn = require('child_process').spawn; var path = require('path'); var install = require('./install.js'); +var bundle = require('./bundle.js'); function printUsage() { console.log([ @@ -14,7 +15,8 @@ function printUsage() { '', '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); } @@ -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();