diff --git a/bin/rnws.js b/bin/rnws.js index 12b1e13..e4064e4 100755 --- a/bin/rnws.js +++ b/bin/rnws.js @@ -7,12 +7,16 @@ const packageJson = require('../package.json'); const createBundle = require('../lib/createBundle'); const Server = require('../lib/Server'); -/** - * Create a new array with falsey values removed - * @param {Array} arr An array - * @return {Array} The array with falsey values removed - */ -const compact = arr => arr.filter(Boolean); +function normalizePlatforms(options) { + options.platforms = []; + if (options.android) { + options.platforms.push('android'); + } + if (options.ios) { + options.platforms.push('ios'); + } + return options; +} /** * Create a server instance using the provided options. @@ -95,7 +99,7 @@ commonOptions(program.command('start')) .description('Start the webpack server.') .option('-r, --hot', 'Enable hot module replacement. [false]', false) .action(function(options) { - const opts = options.opts(); + const opts = normalizePlatforms(options.opts()); const server = createServer(opts); server.start(); }); @@ -123,25 +127,22 @@ commonOptions(program.command('bundle')) false ) .action(function(options) { - const opts = options.opts(); + const opts = normalizePlatforms(options.opts()); const server = createServer(opts); + const bundlePaths = { + android: opts.androidBundlePath, + ios: opts.iosBundlePath, + }; - const doBundle = () => Promise.all(compact([ - opts.android && createBundle(server, { - platform: 'android', - targetPath: opts.androidBundlePath, - dev: !opts.optimize, - minify: opts.optimize, - sourceMap: opts.sourceMap, - }), - opts.ios && createBundle(server, { - platform: 'ios', - targetPath: opts.iosBundlePath, + const doBundle = () => Promise.all(opts.platforms.map( + (platform) => createBundle(server, { + platform: platform, + targetPath: bundlePaths[platform], dev: !opts.optimize, minify: opts.optimize, sourceMap: opts.sourceMap, - }), - ])); + }) + )); server.start() .then(doBundle) diff --git a/lib/Server.js b/lib/Server.js index 23abe12..5ba4fea 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -50,8 +50,8 @@ class Server { * @param {Number} port * @param {Number} packagerPort * @param {Number} webpackPort - * @param {Boolean} enableAndroid - * @param {Boolean} enableIos + * @param {Boolean} android Enable Android support + * @param {Boolean} ios Enable iOS support * @param {String} androidEntry * @param {String} iosEntry * @param {Object} webpackConfig The webpack config to use for webpack-dev-server @@ -69,6 +69,7 @@ class Server { android: options.androidEntry, ios: options.iosEntry, }; + this.platforms = options.platforms; this.resetCache = !!options.resetCache; this.hot = !!options.hot; this.webpackConfig = options.webpackConfig; @@ -306,6 +307,7 @@ class Server { const hot = this.hot; return getReactNativeExternals({ projectRoot: process.cwd(), + platforms: this.platforms, }).then(reactNativeExternals => { // Coerce externals into an array, without clobbering it diff --git a/lib/getReactNativeExternals.js b/lib/getReactNativeExternals.js index 5deb4bd..1f7b2e8 100644 --- a/lib/getReactNativeExternals.js +++ b/lib/getReactNativeExternals.js @@ -9,17 +9,13 @@ const Promise = require('bluebird'); * @return {Object} A webpack 'externals' config object */ function getReactNativeExternals(options) { - return Promise.props({ - androidModuleNames: getReactNativeDependencyNames({ + return Promise.all(options.platforms.map( + (platform) => getReactNativeDependencyNames({ projectRoot: options.projectRoot, - platform: 'android', - }), - iosModuleNames: getReactNativeDependencyNames({ - projectRoot: options.projectRoot, - platform: 'ios', - }), - }).then(r => { - const allReactNativeModules = r.androidModuleNames.concat(r.iosModuleNames); + platform: platform, + }) + )).then((moduleNamesGroupedByPlatform) => { + const allReactNativeModules = Array.prototype.concat.apply([], moduleNamesGroupedByPlatform); return makeWebpackExternalsConfig(allReactNativeModules); }); }