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);
});
}

View File

@ -11,12 +11,16 @@
const buildBundle = require('./buildBundle');
const bundleCommandLineArgs = require('./bundleCommandLineArgs');
const parseCommandLine = require('../util/parseCommandLine');
const outputBundle = require('./output/bundle');
const outputPrepack = require('./output/prepack');
/**
* Builds the bundle starting to look for dependencies at the given entry path.
*/
function bundle(argv, config) {
return buildBundle(parseCommandLine(bundleCommandLineArgs, argv), config);
const args = parseCommandLine(bundleCommandLineArgs, argv);
const output = args.prepack ? outputPrepack : outputBundle;
return buildBundle(args, config, output);
}
module.exports = bundle;

View File

@ -0,0 +1,59 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* 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 Promise = require('promise');
const sign = require('../sign');
const writeFile = require('./writeFile');
function buildBundle(packagerClient, requestOptions) {
return packagerClient.buildBundle(requestOptions);
}
function createCodeWithMap(bundle, dev) {
if (!dev) {
return bundle.getMinifiedSourceAndMap(dev);
} else {
return {
code: bundle.getSource({dev}),
map: JSON.stringify(bundle.getSourceMap({dev})),
};
}
}
function saveBundleAndMap(bundle, options, log) {
const {
'bundle-output': bundleOutput,
'bundle-encoding': encoding,
dev,
'sourcemap-output': sourcemapOutput,
} = options;
log('start');
const codeWithMap = createCodeWithMap(bundle, dev);
log('finish');
log('Writing bundle output to:', bundleOutput);
const writeBundle = writeFile(bundleOutput, sign(codeWithMap.code), encoding);
writeBundle.then(() => log('Done writing bundle output'));
if (sourcemapOutput) {
log('Writing sourcemap output to:', sourcemapOutput);
const writeMap = writeFile(sourcemapOutput, codeWithMap.map, null);
writeMap.then(() => log('Done writing sourcemap output'));
return Promise.all([writeBundle, writeMap]);
} else {
return writeBundle;
}
}
exports.build = buildBundle;
exports.save = saveBundleAndMap;
exports.formatName = 'bundle';

View File

@ -0,0 +1,34 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* 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 writeFile = require('./writeFile');
function buildPrepackBundle(packagerClient, requestOptions) {
return packagerClient.buildPrepackBundle(requestOptions);
}
function savePrepackBundle(bundle, options, log) {
const {
'bundle-output': bundleOutput,
'bridge-config': bridgeConfig,
} = options;
const result = bundle.build({
batchedBridgeConfig: bridgeConfig
});
log('Writing prepack bundle output to:', bundleOutput);
const writePrepackBundle = writeFile(bundleOutput, result, 'ucs-2');
writePrepackBundle.then(() => log('Done writing prepack bundle output'));
return writePrepackBundle;
}
exports.build = buildPrepackBundle;
exports.save = savePrepackBundle;

View File

@ -0,0 +1,25 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* 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 Promise = require('promise');
function writeFile(file, data, encoding) {
return new Promise((resolve, reject) => {
fs.writeFile(
file,
data,
encoding,
error => error ? reject(error) : resolve()
);
});
}
module.exports = writeFile;