Skip to content

Commit

Permalink
Merge pull request #146 from philikon/optimize_externals
Browse files Browse the repository at this point in the history
getReactNativeExternals: only pay for what you eat
  • Loading branch information
philikon committed Dec 22, 2015
2 parents d638bd4 + a78d81a commit f6afd06
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 33 deletions.
43 changes: 22 additions & 21 deletions bin/rnws.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
16 changes: 6 additions & 10 deletions lib/getReactNativeExternals.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
Expand Down

0 comments on commit f6afd06

Please sign in to comment.