Move bundle output formats to their own files

Reviewed By: tadeuzagallo

Differential Revision: D2702834

fb-gh-sync-id: fca308c5078a11924e071077822fb77d762bd564
This commit is contained in:
David Aurelio
2015-11-30 08:31:35 -08:00
committed by facebook-github-bot-4
parent 3c382a7619
commit 32fa0b7fa1
5 changed files with 148 additions and 91 deletions

View File

@@ -8,59 +8,13 @@
*/
'use strict';
const fs = require('fs');
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');
const sign = require('./sign');
function saveBundleAndMap(
bundle,
bundleOutput,
encoding,
sourcemapOutput,
dev
) {
log('start');
let codeWithMap;
if (!dev) {
codeWithMap = bundle.getMinifiedSourceAndMap(dev);
} else {
codeWithMap = {
code: bundle.getSource({ dev }),
map: JSON.stringify(bundle.getSourceMap({ dev })),
};
}
log('finish');
log('Writing bundle output to:', bundleOutput);
fs.writeFileSync(bundleOutput, sign(codeWithMap.code), encoding);
log('Done writing bundle output');
if (sourcemapOutput) {
log('Writing sourcemap output to:', sourcemapOutput);
fs.writeFileSync(sourcemapOutput, codeWithMap.map);
log('Done writing sourcemap output');
}
}
function savePrepackBundleAndMap(
bundle,
bundleOutput,
sourcemapOutput,
bridgeConfig
) {
log('Writing prepack bundle output to:', bundleOutput);
const result = bundle.build({
batchedBridgeConfig: bridgeConfig
});
fs.writeFileSync(bundleOutput, result, 'ucs-2');
log('Done writing prepack bundle output');
}
function buildBundle(args, config) {
function buildBundle(args, config, output = outputBundle) {
return new Promise((resolve, reject) => {
// This is used by a bazillion of npm modules we don't control so we don't
@@ -82,55 +36,36 @@ function buildBundle(args, config) {
platform: args.platform,
};
const prepack = args.prepack;
const client = ReactPackager.createClientFor(options);
client.then(() => log('Created ReactPackager'));
const clientPromise = ReactPackager.createClientFor(options);
// Build and save the bundle
let bundle;
if (prepack) {
bundle = client.then(c => c.buildPrepackBundle(requestOpts))
.then(outputBundle => {
savePrepackBundleAndMap(
outputBundle,
args['bundle-output'],
args['sourcemap-output'],
args['bridge-config']
);
return outputBundle;
});
} else {
bundle = client.then(c => c.buildBundle(requestOpts))
.then(outputBundle => {
saveBundleAndMap(
outputBundle,
args['bundle-output'],
args['bundle-encoding'],
args['sourcemap-output'],
args.dev
);
return outputBundle;
});
}
const 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
bundle.then(() => client.then(c => {
log('Closing client');
c.close();
}));
Promise.all([clientPromise, bundlePromise])
.then(([client]) => {
log('Closing client');
client.close();
});
// Save the assets of the bundle
const assets = bundle
.then(outputBundle => outputBundle.getAssets())
.then(outputAssets => saveAssets(
outputAssets,
args.platform,
args['assets-dest']
));
const assets = bundlePromise
.then(bundle => bundle.getAssets())
.then(outputAssets => saveAssets(
outputAssets,
args.platform,
args['assets-dest']
));
// When we're done saving the assets, we're done.
// When we're done saving bundle output and the assets, we're done.
resolve(assets);
});
}