Add packager worker for Buck

Reviewed By: martinbigio

Differential Revision: D3051886

fb-gh-sync-id: da19ee987c0ec04cde550147d57bd90d98712e14
shipit-source-id: da19ee987c0ec04cde550147d57bd90d98712e14
This commit is contained in:
Sam Swarr 2016-03-18 12:44:01 -07:00 committed by Facebook Github Bot 6
parent 22e55d4396
commit fa44607bf6
4 changed files with 79 additions and 60 deletions

View File

@ -6,7 +6,6 @@
* LICENSE file in the root directory of this source tree. An additional grant * 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. * of patent rights can be found in the PATENTS file in the same directory.
*/ */
'use strict';
const log = require('../util/log').out('bundle'); const log = require('../util/log').out('bundle');
const outputBundle = require('./output/bundle'); const outputBundle = require('./output/bundle');
@ -14,7 +13,7 @@ const Promise = require('promise');
const ReactPackager = require('../../packager/react-packager'); const ReactPackager = require('../../packager/react-packager');
const saveAssets = require('./saveAssets'); const saveAssets = require('./saveAssets');
function buildBundle(args, config, output = outputBundle) { function buildBundle(args, config, output = outputBundle, packagerInstance) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// This is used by a bazillion of npm modules we don't control so we don't // 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, 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 // Build and save the bundle
const bundlePromise = clientPromise bundlePromise = clientPromise
.then(client => { .then(client => {
log('Created ReactPackager'); log('Created ReactPackager');
return output.build(client, requestOpts); return output.build(client, requestOpts);
}) })
.then(bundle => { .then(bundle => {
output.save(bundle, args, log); output.save(bundle, args, log);
return bundle; return bundle;
}); });
// When we're done bundling, close the client // When we're done bundling, close the client
Promise.all([clientPromise, bundlePromise]) Promise.all([clientPromise, bundlePromise])
.then(([client]) => { .then(([client]) => {
log('Closing client'); log('Closing client');
client.close(); client.close();
}); });
}
// Save the assets of the bundle // Save the assets of the bundle
const assets = bundlePromise const assets = bundlePromise

View File

@ -6,7 +6,6 @@
* LICENSE file in the root directory of this source tree. An additional grant * 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. * of patent rights can be found in the PATENTS file in the same directory.
*/ */
'use strict';
const buildBundle = require('./buildBundle'); const buildBundle = require('./buildBundle');
const bundleCommandLineArgs = require('./bundleCommandLineArgs'); 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. * 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); const args = parseCommandLine(bundleCommandLineArgs, argv);
if (!output) { if (!output) {
output = args.prepack ? outputPrepack : outputBundle; output = args.prepack ? outputPrepack : outputBundle;
} }
return buildBundle(args, config, output); return buildBundle(args, config, output, packagerInstance);
} }
function bundle(argv, config) { function bundle(argv, config, packagerInstance) {
return bundleWithOutput(argv, config); return bundleWithOutput(argv, config, undefined, packagerInstance);
} }
module.exports = bundle; module.exports = bundle;

View File

@ -6,7 +6,6 @@
* LICENSE file in the root directory of this source tree. An additional grant * 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. * of patent rights can be found in the PATENTS file in the same directory.
*/ */
'use strict';
const bundleWithOutput = require('./bundle').withOutput; const bundleWithOutput = require('./bundle').withOutput;
const outputUnbundle = require('./output/unbundle'); 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. * Builds the bundle starting to look for dependencies at the given entry path.
*/ */
function unbundle(argv, config) { function unbundle(argv, config, packagerInstance) {
return bundleWithOutput(argv, config, outputUnbundle); return bundleWithOutput(argv, config, outputUnbundle, packagerInstance);
} }
module.exports = unbundle; module.exports = unbundle;

View File

@ -6,10 +6,8 @@
* LICENSE file in the root directory of this source tree. An additional grant * 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. * of patent rights can be found in the PATENTS file in the same directory.
*/ */
'use strict';
const fs = require('fs'); const fs = require('fs');
const log = require('../util/log').out('dependencies');
const parseCommandLine = require('../util/parseCommandLine'); const parseCommandLine = require('../util/parseCommandLine');
const path = require('path'); const path = require('path');
const Promise = require('promise'); const Promise = require('promise');
@ -18,13 +16,13 @@ const ReactPackager = require('../../packager/react-packager');
/** /**
* Returns the dependencies an entry path has. * Returns the dependencies an entry path has.
*/ */
function dependencies(argv, config) { function dependencies(argv, config, packagerInstance) {
return new Promise((resolve, reject) => { 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([ const args = parseCommandLine([
{ {
command: 'entry-file', command: 'entry-file',
@ -82,35 +80,50 @@ function _dependencies(argv, config, resolve, reject) {
? fs.createWriteStream(args.output) ? fs.createWriteStream(args.output)
: process.stdout; : process.stdout;
// TODO: allow to configure which logging namespaces should get logged if (packagerInstance) {
// log('Running ReactPackager'); resolve(packagerInstance.getOrderedDependencyPaths(options).then(
// log('Waiting for the packager.'); deps => {
resolve(ReactPackager.createClientFor(packageOpts).then(client => { return _dependenciesHandler(
// log('Packager client was created'); deps,
return client.getOrderedDependencyPaths(options) packageOpts.projectRoots,
.then(deps => { outStream,
// log('Packager returned dependencies'); writeToFile
client.close(); );
}
deps.forEach(modulePath => { ));
// Temporary hack to disable listing dependencies not under this directory. } else {
// Long term, we need either resolve(ReactPackager.createClientFor(packageOpts).then(client => {
// (a) JS code to not depend on anything outside this directory, or return client.getOrderedDependencyPaths(options)
// (b) Come up with a way to declare this dependency in Buck. .then(deps => {
const isInsideProjectRoots = packageOpts.projectRoots.filter( client.close();
root => modulePath.startsWith(root) return _dependenciesHandler(
).length > 0; deps,
packageOpts.projectRoots,
if (isInsideProjectRoots) { outStream,
outStream.write(modulePath + '\n'); writeToFile
} );
}); });
return writeToFile }));
? Promise.denodeify(outStream.end).bind(outStream)() }
: Promise.resolve(); }
// log('Wrote dependencies to output file');
}); 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();
} }
module.exports = dependencies; module.exports = dependencies;