Skip to content
This repository has been archived by the owner on Feb 18, 2019. It is now read-only.

Commit

Permalink
Add packager worker for Buck
Browse files Browse the repository at this point in the history
Reviewed By: martinbigio

Differential Revision: D3051886

fb-gh-sync-id: da19ee987c0ec04cde550147d57bd90d98712e14
shipit-source-id: da19ee987c0ec04cde550147d57bd90d98712e14
  • Loading branch information
sam-swarr authored and Facebook Github Bot 6 committed Mar 18, 2016
1 parent 22e55d4 commit fa44607
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 59 deletions.
46 changes: 27 additions & 19 deletions local-cli/bundle/buildBundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
* 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');
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
Expand All @@ -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
Expand Down
9 changes: 4 additions & 5 deletions local-cli/bundle/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions local-cli/bundle/unbundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
* 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');

/**
* 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;
77 changes: 45 additions & 32 deletions local-cli/dependencies/dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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',
Expand Down Expand Up @@ -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;

0 comments on commit fa44607

Please sign in to comment.