2015-04-15 00:19:58 +00:00
|
|
|
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');
|
|
|
|
|
2015-09-17 16:58:50 +00:00
|
|
|
var OUT_PATH = {
|
|
|
|
android: 'android/app/src/main/assets/index.android.bundle',
|
|
|
|
ios: 'ios/main.jsbundle'
|
|
|
|
};
|
|
|
|
var URL_PATH = {
|
|
|
|
android: '/index.android.bundle?platform=android&dev=',
|
|
|
|
ios: '/index.ios.bundle?platform=ios&dev='
|
|
|
|
};
|
2015-10-02 23:01:18 +00:00
|
|
|
var SOURCEMAP_OUT_PATH = {
|
|
|
|
android: 'android/index.android.map',
|
|
|
|
ios: 'ios/index.ios.map'
|
|
|
|
};
|
2015-04-15 00:19:58 +00:00
|
|
|
|
|
|
|
function getBundle(flags) {
|
2015-09-17 16:58:50 +00:00
|
|
|
var platform = flags.platform ? flags.platform : 'ios';
|
|
|
|
var outPath = flags.out ? flags.out : OUT_PATH[platform];
|
2015-10-02 23:01:18 +00:00
|
|
|
var sourceMapOutPath = SOURCEMAP_OUT_PATH[platform];
|
2015-05-27 18:02:26 +00:00
|
|
|
|
|
|
|
var projectRoots = [path.resolve(__dirname, '../../..')];
|
2015-06-02 22:32:49 +00:00
|
|
|
if (flags.root) {
|
2015-05-27 18:02:26 +00:00
|
|
|
projectRoots.push(path.resolve(flags.root));
|
|
|
|
}
|
|
|
|
|
|
|
|
var assetRoots = [path.resolve(__dirname, '../../..')];
|
2015-06-12 12:06:31 +00:00
|
|
|
if (flags.assetRoots) {
|
|
|
|
assetRoots = assetRoots.concat(flags.assetRoots.split(",").map(function(root) {
|
|
|
|
return path.resolve(root);
|
2015-06-12 13:03:28 +00:00
|
|
|
}));
|
2015-05-27 18:02:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-15 00:19:58 +00:00
|
|
|
var options = {
|
2015-05-27 18:02:26 +00:00
|
|
|
projectRoots: projectRoots,
|
2015-04-15 00:19:58 +00:00
|
|
|
transformModulePath: require.resolve('../packager/transformer.js'),
|
2015-05-27 18:02:26 +00:00
|
|
|
assetRoots: assetRoots,
|
2015-09-23 22:24:50 +00:00
|
|
|
cacheVersion: '3',
|
2015-09-17 16:58:50 +00:00
|
|
|
blacklistRE: blacklist(platform),
|
2015-04-15 00:19:58 +00:00
|
|
|
};
|
|
|
|
|
2015-09-17 16:58:50 +00:00
|
|
|
var url = flags.url ? flags.url.replace(/\.js$/i, '.bundle?dev=') : URL_PATH[platform];
|
2015-07-14 02:40:39 +00:00
|
|
|
url = url.match(/^\//) ? url : '/' + url;
|
|
|
|
url += flags.dev;
|
2015-04-15 00:19:58 +00:00
|
|
|
|
|
|
|
console.log('Building package...');
|
|
|
|
ReactPackager.buildPackageFromUrl(options, url)
|
|
|
|
.done(function(bundle) {
|
|
|
|
console.log('Build complete');
|
2015-05-27 18:02:26 +00:00
|
|
|
fs.writeFile(outPath, bundle.getSource({
|
2015-04-15 00:19:58 +00:00
|
|
|
inlineSourceMap: false,
|
|
|
|
minify: flags.minify
|
2015-10-02 23:01:18 +00:00
|
|
|
}), function(bundleWriteErr) {
|
|
|
|
if (bundleWriteErr) {
|
2015-04-15 00:19:58 +00:00
|
|
|
console.log(chalk.red('Error saving bundle to disk'));
|
2015-10-02 23:01:18 +00:00
|
|
|
throw bundleWriteErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('Successfully saved bundle to ' + outPath);
|
|
|
|
|
|
|
|
if (flags.writeSourcemap) {
|
|
|
|
fs.writeFile(sourceMapOutPath, bundle.getSourceMap(), function(sourceMapWriteErr) {
|
|
|
|
if (sourceMapWriteErr) {
|
|
|
|
console.log(chalk.red('Error saving source map to disk'));
|
|
|
|
throw sourceMapWriteErr;
|
|
|
|
} else {
|
|
|
|
console.log('Successfully saved source map to ' + sourceMapOutPath);
|
|
|
|
}
|
|
|
|
});
|
2015-04-15 00:19:58 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function showHelp() {
|
|
|
|
console.log([
|
|
|
|
'Usage: react-native bundle [options]',
|
|
|
|
'',
|
|
|
|
'Options:',
|
|
|
|
' --dev\t\tsets DEV flag to true',
|
2015-05-27 18:02:26 +00:00
|
|
|
' --minify\tminify js bundle',
|
|
|
|
' --root\t\tadd another root(s) to be used in bundling in this project',
|
2015-06-12 12:06:31 +00:00
|
|
|
' --assetRoots\t\tspecify the root directories of app assets',
|
2015-05-27 18:02:26 +00:00
|
|
|
' --out\t\tspecify the output file',
|
2015-07-14 02:40:39 +00:00
|
|
|
' --url\t\tspecify the bundle file url',
|
2015-09-17 16:58:50 +00:00
|
|
|
' --platform\t\tspecify the platform(android/ios)',
|
2015-10-02 23:01:18 +00:00
|
|
|
' --write-sourcemap\t\twrite bundle source map to disk'
|
2015-04-15 00:19:58 +00:00
|
|
|
].join('\n'));
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init: function(args) {
|
|
|
|
var flags = {
|
|
|
|
help: args.indexOf('--help') !== -1,
|
|
|
|
dev: args.indexOf('--dev') !== -1,
|
2015-05-27 18:02:26 +00:00
|
|
|
minify: args.indexOf('--minify') !== -1,
|
|
|
|
root: args.indexOf('--root') !== -1 ? args[args.indexOf('--root') + 1] : false,
|
2015-09-17 16:58:50 +00:00
|
|
|
platform: args.indexOf('--platform') !== -1 ? args[args.indexOf('--platform') + 1] : false,
|
2015-06-12 12:06:31 +00:00
|
|
|
assetRoots: args.indexOf('--assetRoots') !== -1 ? args[args.indexOf('--assetRoots') + 1] : false,
|
2015-05-27 18:02:26 +00:00
|
|
|
out: args.indexOf('--out') !== -1 ? args[args.indexOf('--out') + 1] : false,
|
2015-07-14 02:40:39 +00:00
|
|
|
url: args.indexOf('--url') !== -1 ? args[args.indexOf('--url') + 1] : false,
|
2015-10-02 23:01:18 +00:00
|
|
|
writeSourcemap: args.indexOf('--write-sourcemap') !== -1,
|
2015-04-15 00:19:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags.help) {
|
|
|
|
showHelp();
|
|
|
|
} else {
|
|
|
|
getBundle(flags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|