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.
|
|
|
|
*/
|
2016-11-14 19:12:24 +00:00
|
|
|
'use strict';
|
2015-10-20 20:47:41 +00:00
|
|
|
|
|
|
|
const log = require('../util/log').out('bundle');
|
2016-10-22 13:07:01 +00:00
|
|
|
const Server = require('../../packager/react-packager/src/Server');
|
2016-12-19 18:50:20 +00:00
|
|
|
const TerminalReporter = require('../../packager/react-packager/src/lib/TerminalReporter');
|
2016-10-22 13:07:01 +00:00
|
|
|
|
2015-11-30 16:31:35 +00:00
|
|
|
const outputBundle = require('./output/bundle');
|
2016-06-07 00:45:30 +00:00
|
|
|
const path = require('path');
|
2015-11-21 04:17:17 +00:00
|
|
|
const saveAssets = require('./saveAssets');
|
2016-10-22 13:07:01 +00:00
|
|
|
const defaultAssetExts = require('../../packager/defaults').assetExts;
|
2015-11-21 04:17:17 +00:00
|
|
|
|
2016-11-21 21:19:13 +00:00
|
|
|
import type RequestOptions from './types.flow';
|
|
|
|
|
2016-04-07 15:57:18 +00:00
|
|
|
function saveBundle(output, bundle, args) {
|
|
|
|
return Promise.resolve(
|
|
|
|
output.save(bundle, args, log)
|
|
|
|
).then(() => bundle);
|
|
|
|
}
|
|
|
|
|
2016-03-18 19:44:01 +00:00
|
|
|
function buildBundle(args, config, output = outputBundle, packagerInstance) {
|
2016-07-30 15:59:16 +00:00
|
|
|
// 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';
|
2015-10-20 20:47:41 +00:00
|
|
|
|
2016-11-21 21:19:13 +00:00
|
|
|
const requestOpts: RequestOptions = {
|
2016-07-30 15:59:16 +00:00
|
|
|
entryFile: args.entryFile,
|
2016-08-09 16:38:32 +00:00
|
|
|
sourceMapUrl: args.sourcemapOutput && path.basename(args.sourcemapOutput),
|
2016-07-30 15:59:16 +00:00
|
|
|
dev: args.dev,
|
|
|
|
minify: !args.dev,
|
|
|
|
platform: args.platform,
|
|
|
|
};
|
2016-06-22 15:08:28 +00:00
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
// If a packager instance was not provided, then just create one for this
|
|
|
|
// bundle command and close it down afterwards.
|
|
|
|
var shouldClosePackager = false;
|
|
|
|
if (!packagerInstance) {
|
2016-10-22 13:07:01 +00:00
|
|
|
const assetExts = (config.getAssetExts && config.getAssetExts()) || [];
|
2016-08-02 17:13:13 +00:00
|
|
|
|
2016-09-12 22:11:22 +00:00
|
|
|
const transformModulePath =
|
|
|
|
args.transformer ? path.resolve(args.transformer) :
|
|
|
|
typeof config.getTransformModulePath === 'function' ? config.getTransformModulePath() :
|
|
|
|
undefined;
|
|
|
|
|
2016-08-02 17:13:13 +00:00
|
|
|
const options = {
|
|
|
|
projectRoots: config.getProjectRoots(),
|
|
|
|
assetExts: defaultAssetExts.concat(assetExts),
|
2016-09-23 14:59:59 +00:00
|
|
|
blacklistRE: config.getBlacklistRE(),
|
2016-11-28 15:27:09 +00:00
|
|
|
getTransformOptions: config.getTransformOptions,
|
2016-09-12 22:11:22 +00:00
|
|
|
transformModulePath: transformModulePath,
|
2016-08-02 17:13:13 +00:00
|
|
|
extraNodeModules: config.extraNodeModules,
|
|
|
|
resetCache: args.resetCache,
|
2016-11-17 04:11:48 +00:00
|
|
|
watch: false,
|
2016-12-19 18:50:20 +00:00
|
|
|
reporter: new TerminalReporter(),
|
2016-08-02 17:13:13 +00:00
|
|
|
};
|
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
packagerInstance = new Server(options);
|
|
|
|
shouldClosePackager = true;
|
|
|
|
}
|
2015-10-20 20:47:41 +00:00
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
const bundlePromise = output.build(packagerInstance, requestOpts)
|
|
|
|
.then(bundle => {
|
|
|
|
if (shouldClosePackager) {
|
|
|
|
packagerInstance.end();
|
|
|
|
}
|
|
|
|
return saveBundle(output, bundle, args);
|
|
|
|
});
|
2015-10-20 20:47:41 +00:00
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
// Save the assets of the bundle
|
|
|
|
const assets = bundlePromise
|
|
|
|
.then(bundle => bundle.getAssets())
|
|
|
|
.then(outputAssets => saveAssets(
|
|
|
|
outputAssets,
|
|
|
|
args.platform,
|
|
|
|
args.assetsDest,
|
|
|
|
));
|
2015-11-21 04:17:17 +00:00
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
// When we're done saving bundle output and the assets, we're done.
|
|
|
|
return assets;
|
2015-10-20 20:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = buildBundle;
|