From f5e1e3d9dacec89ef914d4c0c61069cdc5b9cba9 Mon Sep 17 00:00:00 2001 From: Philipp von Weitershausen Date: Thu, 17 Dec 2015 17:46:21 -0800 Subject: [PATCH 1/2] Ability to override RN packager's projectRoots and assetRoots settings By overriding these options, the rnws consumer can more accurately restrict the directories that the RN packager is allowed to walk, which can yield significant gains in start-up speed. --- bin/rnws.js | 26 +++++++++++++++++++++++--- lib/Server.js | 9 ++++++++- lib/getReactNativeExternals.js | 7 ++++--- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/bin/rnws.js b/bin/rnws.js index e4064e4..483a80d 100755 --- a/bin/rnws.js +++ b/bin/rnws.js @@ -7,7 +7,7 @@ const packageJson = require('../package.json'); const createBundle = require('../lib/createBundle'); const Server = require('../lib/Server'); -function normalizePlatforms(options) { +function normalizeOptions(options) { options.platforms = []; if (options.android) { options.platforms.push('android'); @@ -15,6 +15,16 @@ function normalizePlatforms(options) { if (options.ios) { options.platforms.push('ios'); } + + if (options.projectRoots) { + options.projectRoots = options.projectRoots.split(',') + .map(dir => path.resolve(process.cwd(), dir)); + } + if (options.assetRoots) { + options.assetRoots = options.assetRoots.split(',') + .map(dir => path.resolve(process.cwd(), dir)); + } + return options; } @@ -88,6 +98,16 @@ function commonOptions(program) { 'iOS entry module name. Has no effect if \'--no-ios\' is passed. [index.ios]', 'index.ios' ) + .option( + '--projectRoots [projectRoots]', + 'List of comma-separated paths for the react-native packager to consider as project root directories', + null + ) + .option( + '--assetRoots [assetRoots]', + 'List of comma-separated paths for the react-native packager to consider as asset root directories', + null + ) .option( '-r, --resetCache', 'Remove cached react-native packager files [false]', @@ -99,7 +119,7 @@ commonOptions(program.command('start')) .description('Start the webpack server.') .option('-r, --hot', 'Enable hot module replacement. [false]', false) .action(function(options) { - const opts = normalizePlatforms(options.opts()); + const opts = normalizeOptions(options.opts()); const server = createServer(opts); server.start(); }); @@ -127,7 +147,7 @@ commonOptions(program.command('bundle')) false ) .action(function(options) { - const opts = normalizePlatforms(options.opts()); + const opts = normalizeOptions(options.opts()); const server = createServer(opts); const bundlePaths = { android: opts.androidBundlePath, diff --git a/lib/Server.js b/lib/Server.js index 5ba4fea..727fcbb 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -70,6 +70,8 @@ class Server { ios: options.iosEntry, }; this.platforms = options.platforms; + this.projectRoots = options.projectRoots; + this.assetRoots = options.assetRoots; this.resetCache = !!options.resetCache; this.hot = !!options.hot; this.webpackConfig = options.webpackConfig; @@ -274,6 +276,10 @@ class Server { '--root', this.entryDir, '--port', this.packagerPort, ]).concat( + this.projectRoots ? ['--projectRoots', this.projectRoots.join(',')] : [] + ).concat( + this.assetRoots ? ['--assetRoots', this.assetRoots.join(',')] : [] + ).concat( this.resetCache ? '--reset-cache' : [] @@ -306,7 +312,8 @@ class Server { const webpackConfig = this.webpackConfig; const hot = this.hot; return getReactNativeExternals({ - projectRoot: process.cwd(), + projectRoots: this.projectRoots, + assetRoots: this.assetRoots, platforms: this.platforms, }).then(reactNativeExternals => { diff --git a/lib/getReactNativeExternals.js b/lib/getReactNativeExternals.js index 1f7b2e8..d29858b 100644 --- a/lib/getReactNativeExternals.js +++ b/lib/getReactNativeExternals.js @@ -11,7 +11,8 @@ const Promise = require('bluebird'); function getReactNativeExternals(options) { return Promise.all(options.platforms.map( (platform) => getReactNativeDependencyNames({ - projectRoot: options.projectRoot, + projectRoots: options.projectRoots || [process.cwd()], + assetRoots: options.assetRoots || [process.cwd()], platform: platform, }) )).then((moduleNamesGroupedByPlatform) => { @@ -45,9 +46,9 @@ function getReactNativeDependencyNames(options) { const rnEntryPoint = require.resolve('react-native'); return ReactPackager.getDependencies({ - assetRoots: [options.projectRoot], blacklistRE: blacklist(false /* don't blacklist any platform */), - projectRoots: [options.projectRoot], + projectRoots: options.projectRoots, + assetRoots: options.assetRoots, transformModulePath: require.resolve('react-native/packager/transformer'), }, { entryFile: rnEntryPoint, From 6cda4970b720d98fae94f4e0f516999a908c49b1 Mon Sep 17 00:00:00 2001 From: Philipp von Weitershausen Date: Wed, 23 Dec 2015 18:00:07 -0800 Subject: [PATCH 2/2] Harmonize style a bit --- lib/Server.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/Server.js b/lib/Server.js index 727fcbb..e251f66 100644 --- a/lib/Server.js +++ b/lib/Server.js @@ -280,9 +280,7 @@ class Server { ).concat( this.assetRoots ? ['--assetRoots', this.assetRoots.join(',')] : [] ).concat( - this.resetCache - ? '--reset-cache' - : [] + this.resetCache ? '--reset-cache' : [] ); const opts = {stdio: 'inherit'}; this.packageServer = spawn(cmd, args, opts);