Skip to content

Commit c13646f

Browse files
committed
Added bundle command using ReactPackager
Added bundle script Pipe http response straight to file Used ReactPackager directly, minor fixes Added error handling to fs.writeFile Changed .then to .done
1 parent 3e8b41f commit c13646f

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

Examples/SampleApp/iOS/AppDelegate.m

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
2929
jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/Examples/SampleApp/index.ios.bundle"];
3030

3131
// OPTION 2
32-
// Load from pre-bundled file on disk. To re-generate the static bundle, run
32+
// Load from pre-bundled file on disk. To re-generate the static bundle,
33+
// from the root of your project directory, run
3334
//
34-
// $ curl 'http://localhost:8081/Examples/SampleApp/index.ios.bundle?dev=false&minify=true' -o iOS/main.jsbundle
35+
// $ react-native bundle
3536
//
3637
// and uncomment the next following line
3738
// jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];

local-cli/bundle.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
var http = require('http');
2+
var fs = require('fs');
3+
var path = require('path');
4+
var chalk = require('chalk');
5+
var blacklist = require('../packager/blacklist.js');
6+
var ReactPackager = require('../packager/react-packager');
7+
8+
var OUT_PATH = 'iOS/main.jsbundle';
9+
10+
function getBundle(flags) {
11+
12+
var options = {
13+
projectRoots: [path.resolve(__dirname, '../../..')],
14+
transformModulePath: require.resolve('../packager/transformer.js'),
15+
assetRoots: [path.resolve(__dirname, '../../..')],
16+
cacheVersion: '2',
17+
blacklistRE: blacklist('ios')
18+
};
19+
20+
var url = '/index.ios.bundle?dev=' + flags.dev;
21+
22+
console.log('Building package...');
23+
ReactPackager.buildPackageFromUrl(options, url)
24+
.done(function(bundle) {
25+
console.log('Build complete');
26+
fs.writeFile(OUT_PATH, bundle.getSource({
27+
inlineSourceMap: false,
28+
minify: flags.minify
29+
}), function(err) {
30+
if (err) {
31+
console.log(chalk.red('Error saving bundle to disk'));
32+
throw err;
33+
} else {
34+
console.log('Successfully saved bundle to ' + OUT_PATH);
35+
}
36+
});
37+
});
38+
}
39+
40+
function showHelp() {
41+
console.log([
42+
'Usage: react-native bundle [options]',
43+
'',
44+
'Options:',
45+
' --dev\t\tsets DEV flag to true',
46+
' --minify\tminify js bundle'
47+
].join('\n'));
48+
process.exit(1);
49+
}
50+
51+
module.exports = {
52+
init: function(args) {
53+
var flags = {
54+
help: args.indexOf('--help') !== -1,
55+
dev: args.indexOf('--dev') !== -1,
56+
minify: args.indexOf('--minify') !== -1
57+
}
58+
59+
if (flags.help) {
60+
showHelp();
61+
} else {
62+
getBundle(flags);
63+
}
64+
}
65+
}

local-cli/cli.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
var spawn = require('child_process').spawn;
88
var path = require('path');
99
var install = require('./install.js');
10+
var bundle = require('./bundle.js');
1011

1112
function printUsage() {
1213
console.log([
1314
'Usage: react-native <command>',
1415
'',
1516
'Commands:',
1617
' start: starts the webserver',
17-
' install: installs npm react components'
18+
' install: installs npm react components',
19+
' bundle: builds the javascript bundle for offline use'
1820
].join('\n'));
1921
process.exit(1);
2022
}
@@ -36,6 +38,9 @@ function run() {
3638
case 'install':
3739
install.init();
3840
break;
41+
case 'bundle':
42+
bundle.init(args);
43+
break;
3944
default:
4045
console.error('Command `%s` unrecognized', args[0]);
4146
printUsage();

0 commit comments

Comments
 (0)