diff --git a/local-cli/bundle/buildBundle.js b/local-cli/bundle/buildBundle.js index 31d81ee075c2e7..b9161f9c7d6b95 100644 --- a/local-cli/bundle/buildBundle.js +++ b/local-cli/bundle/buildBundle.js @@ -6,7 +6,6 @@ * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ -'use strict'; const log = require('../util/log').out('bundle'); const outputBundle = require('./output/bundle'); @@ -14,7 +13,7 @@ const Promise = require('promise'); const ReactPackager = require('../../packager/react-packager'); const saveAssets = require('./saveAssets'); -function buildBundle(args, config, output = outputBundle) { +function buildBundle(args, config, output = outputBundle, packagerInstance) { return new Promise((resolve, reject) => { // This is used by a bazillion of npm modules we don't control so we don't @@ -38,25 +37,34 @@ function buildBundle(args, config, output = outputBundle) { platform: args.platform, }; - const clientPromise = ReactPackager.createClientFor(options); + var bundlePromise; + if (packagerInstance) { + bundlePromise = output.build(packagerInstance, requestOpts) + .then(bundle => { + output.save(bundle, args, log); + return bundle; + }); + } else { + const clientPromise = ReactPackager.createClientFor(options); - // Build and save the bundle - const bundlePromise = clientPromise - .then(client => { - log('Created ReactPackager'); - return output.build(client, requestOpts); - }) - .then(bundle => { - output.save(bundle, args, log); - return bundle; - }); + // Build and save the bundle + bundlePromise = clientPromise + .then(client => { + log('Created ReactPackager'); + return output.build(client, requestOpts); + }) + .then(bundle => { + output.save(bundle, args, log); + return bundle; + }); - // When we're done bundling, close the client - Promise.all([clientPromise, bundlePromise]) - .then(([client]) => { - log('Closing client'); - client.close(); - }); + // When we're done bundling, close the client + Promise.all([clientPromise, bundlePromise]) + .then(([client]) => { + log('Closing client'); + client.close(); + }); + } // Save the assets of the bundle const assets = bundlePromise diff --git a/local-cli/bundle/bundle.js b/local-cli/bundle/bundle.js index debbb62ecfc1e9..e2a6441e054fce 100644 --- a/local-cli/bundle/bundle.js +++ b/local-cli/bundle/bundle.js @@ -6,7 +6,6 @@ * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ -'use strict'; const buildBundle = require('./buildBundle'); const bundleCommandLineArgs = require('./bundleCommandLineArgs'); @@ -17,17 +16,17 @@ const outputPrepack = require('./output/prepack'); /** * Builds the bundle starting to look for dependencies at the given entry path. */ -function bundleWithOutput(argv, config, output) { +function bundleWithOutput(argv, config, output, packagerInstance) { const args = parseCommandLine(bundleCommandLineArgs, argv); if (!output) { output = args.prepack ? outputPrepack : outputBundle; } - return buildBundle(args, config, output); + return buildBundle(args, config, output, packagerInstance); } -function bundle(argv, config) { - return bundleWithOutput(argv, config); +function bundle(argv, config, packagerInstance) { + return bundleWithOutput(argv, config, undefined, packagerInstance); } module.exports = bundle; diff --git a/local-cli/bundle/unbundle.js b/local-cli/bundle/unbundle.js index 042093ced8970a..41460d3eb37f42 100644 --- a/local-cli/bundle/unbundle.js +++ b/local-cli/bundle/unbundle.js @@ -6,7 +6,6 @@ * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ -'use strict'; const bundleWithOutput = require('./bundle').withOutput; const outputUnbundle = require('./output/unbundle'); @@ -14,8 +13,8 @@ const outputUnbundle = require('./output/unbundle'); /** * Builds the bundle starting to look for dependencies at the given entry path. */ -function unbundle(argv, config) { - return bundleWithOutput(argv, config, outputUnbundle); +function unbundle(argv, config, packagerInstance) { + return bundleWithOutput(argv, config, outputUnbundle, packagerInstance); } module.exports = unbundle; diff --git a/local-cli/dependencies/dependencies.js b/local-cli/dependencies/dependencies.js index adc1e889701176..372c65705ccde7 100644 --- a/local-cli/dependencies/dependencies.js +++ b/local-cli/dependencies/dependencies.js @@ -6,10 +6,8 @@ * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ -'use strict'; const fs = require('fs'); -const log = require('../util/log').out('dependencies'); const parseCommandLine = require('../util/parseCommandLine'); const path = require('path'); const Promise = require('promise'); @@ -18,13 +16,13 @@ const ReactPackager = require('../../packager/react-packager'); /** * Returns the dependencies an entry path has. */ -function dependencies(argv, config) { +function dependencies(argv, config, packagerInstance) { return new Promise((resolve, reject) => { - _dependencies(argv, config, resolve, reject); + _dependencies(argv, config, resolve, reject, packagerInstance); }); } -function _dependencies(argv, config, resolve, reject) { +function _dependencies(argv, config, resolve, reject, packagerInstance) { const args = parseCommandLine([ { command: 'entry-file', @@ -82,35 +80,50 @@ function _dependencies(argv, config, resolve, reject) { ? fs.createWriteStream(args.output) : process.stdout; - // TODO: allow to configure which logging namespaces should get logged - // log('Running ReactPackager'); - // log('Waiting for the packager.'); - resolve(ReactPackager.createClientFor(packageOpts).then(client => { - // log('Packager client was created'); - return client.getOrderedDependencyPaths(options) - .then(deps => { - // log('Packager returned dependencies'); - client.close(); + if (packagerInstance) { + resolve(packagerInstance.getOrderedDependencyPaths(options).then( + deps => { + return _dependenciesHandler( + deps, + packageOpts.projectRoots, + outStream, + writeToFile + ); + } + )); + } else { + resolve(ReactPackager.createClientFor(packageOpts).then(client => { + return client.getOrderedDependencyPaths(options) + .then(deps => { + client.close(); + return _dependenciesHandler( + deps, + packageOpts.projectRoots, + outStream, + writeToFile + ); + }); + })); + } +} - deps.forEach(modulePath => { - // Temporary hack to disable listing dependencies not under this directory. - // Long term, we need either - // (a) JS code to not depend on anything outside this directory, or - // (b) Come up with a way to declare this dependency in Buck. - const isInsideProjectRoots = packageOpts.projectRoots.filter( - root => modulePath.startsWith(root) - ).length > 0; +function _dependenciesHandler(deps, projectRoots, outStream, writeToFile) { + deps.forEach(modulePath => { + // Temporary hack to disable listing dependencies not under this directory. + // Long term, we need either + // (a) JS code to not depend on anything outside this directory, or + // (b) Come up with a way to declare this dependency in Buck. + const isInsideProjectRoots = projectRoots.filter( + root => modulePath.startsWith(root) + ).length > 0; - if (isInsideProjectRoots) { - outStream.write(modulePath + '\n'); - } - }); - return writeToFile - ? Promise.denodeify(outStream.end).bind(outStream)() - : Promise.resolve(); - // log('Wrote dependencies to output file'); - }); - })); + if (isInsideProjectRoots) { + outStream.write(modulePath + '\n'); + } + }); + return writeToFile + ? Promise.denodeify(outStream.end).bind(outStream)() + : Promise.resolve(); } module.exports = dependencies;