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
* 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

View File

@ -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;

View File

@ -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;

View File

@ -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();
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;
if (isInsideProjectRoots) {
outStream.write(modulePath + '\n');
}
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
);
});
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;