Skip to content

Commit 6634b66

Browse files
committed
Added bundle script
1 parent bac60f5 commit 6634b66

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

local-cli/bundle.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
var http = require('http');
2+
var fs = require('fs');
3+
4+
var OUT_PATH = 'iOS/main.jsbundle';
5+
6+
function getBundle(flags, callback) {
7+
var options = {
8+
host: 'localhost',
9+
port: 8081,
10+
path: '/index.ios.bundle?dev=' + flags.dev + '&minify=' + flags.minify
11+
};
12+
13+
console.log('Requesting bundle from local server');
14+
var req = http.request(options, function(res) {
15+
var bundle = '';
16+
17+
res.on('data', function (chunk) {
18+
bundle += chunk;
19+
});
20+
21+
res.on('end', function () {
22+
console.log('Got bundle');
23+
callback(bundle);
24+
});
25+
});
26+
27+
// set up error handler
28+
req.on('error', function(err) {
29+
console.error('An error occured while connecting to local server. Are you sure the server is running? Run npm start');
30+
});
31+
32+
// end request
33+
req.end();
34+
}
35+
36+
function showHelp() {
37+
console.log([
38+
'Usage: react-native bundle [options]',
39+
'',
40+
'Options:',
41+
' --dev\t\tsets DEV flag to true',
42+
' --minify\tminify js bundle'
43+
].join('\n'));
44+
process.exit(1);
45+
}
46+
47+
module.exports = {
48+
init: function(args) {
49+
var flags = {
50+
help: args.indexOf('--help') !== -1,
51+
dev: args.indexOf('--dev') !== -1,
52+
minify: args.indexOf('--minify') !== -1
53+
}
54+
55+
if (flags.help) {
56+
showHelp();
57+
} else {
58+
getBundle(flags, function(bundle) {
59+
fs.writeFile(OUT_PATH, bundle, function(err) {
60+
if(err) {
61+
return console.err('Error writing bundle to file', err);
62+
}
63+
console.log('Successfully saved bundle to ' + OUT_PATH);
64+
});
65+
});
66+
}
67+
}
68+
}

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+
' build: 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)