2015-10-20 20:47:41 +00:00
|
|
|
/**
|
|
|
|
* 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 log = require('../util/log').out('bundle');
|
2015-11-30 16:31:35 +00:00
|
|
|
const outputBundle = require('./output/bundle');
|
2015-10-20 20:47:41 +00:00
|
|
|
const Promise = require('promise');
|
2015-10-26 14:55:29 +00:00
|
|
|
const ReactPackager = require('../../packager/react-packager');
|
2015-11-21 04:17:17 +00:00
|
|
|
const saveAssets = require('./saveAssets');
|
|
|
|
|
2015-11-30 16:31:35 +00:00
|
|
|
function buildBundle(args, config, output = outputBundle) {
|
2015-10-20 20:47:41 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
// This is used by a bazillion of npm modules we don't control so we don't
|
|
|
|
// have other choice than defining it as an env variable here.
|
|
|
|
process.env.NODE_ENV = args.dev ? 'development' : 'production';
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
projectRoots: config.getProjectRoots(),
|
|
|
|
assetRoots: config.getAssetRoots(),
|
|
|
|
blacklistRE: config.getBlacklistRE(),
|
2015-12-24 09:01:18 +00:00
|
|
|
getTransformOptionsModulePath: config.getTransformOptionsModulePath,
|
2015-10-20 20:47:41 +00:00
|
|
|
transformModulePath: args.transformer,
|
|
|
|
verbose: args.verbose,
|
2016-01-08 19:09:17 +00:00
|
|
|
disableInternalTransforms: true,
|
2015-10-20 20:47:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const requestOpts = {
|
|
|
|
entryFile: args['entry-file'],
|
|
|
|
dev: args.dev,
|
|
|
|
minify: !args.dev,
|
|
|
|
platform: args.platform,
|
|
|
|
};
|
|
|
|
|
2015-11-30 16:31:35 +00:00
|
|
|
const clientPromise = ReactPackager.createClientFor(options);
|
2015-11-21 04:17:17 +00:00
|
|
|
|
|
|
|
// Build and save the bundle
|
2015-11-30 16:31:35 +00:00
|
|
|
const bundlePromise = clientPromise
|
|
|
|
.then(client => {
|
|
|
|
log('Created ReactPackager');
|
|
|
|
return output.build(client, requestOpts);
|
|
|
|
})
|
|
|
|
.then(bundle => {
|
|
|
|
output.save(bundle, args, log);
|
|
|
|
return bundle;
|
|
|
|
});
|
2015-11-21 04:17:17 +00:00
|
|
|
|
|
|
|
// When we're done bundling, close the client
|
2015-11-30 16:31:35 +00:00
|
|
|
Promise.all([clientPromise, bundlePromise])
|
|
|
|
.then(([client]) => {
|
|
|
|
log('Closing client');
|
|
|
|
client.close();
|
|
|
|
});
|
2015-11-21 04:17:17 +00:00
|
|
|
|
|
|
|
// Save the assets of the bundle
|
2015-11-30 16:31:35 +00:00
|
|
|
const assets = bundlePromise
|
|
|
|
.then(bundle => bundle.getAssets())
|
|
|
|
.then(outputAssets => saveAssets(
|
|
|
|
outputAssets,
|
|
|
|
args.platform,
|
|
|
|
args['assets-dest']
|
|
|
|
));
|
|
|
|
|
|
|
|
// When we're done saving bundle output and the assets, we're done.
|
2015-11-21 04:17:17 +00:00
|
|
|
resolve(assets);
|
2015-10-20 20:47:41 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = buildBundle;
|