2015-10-20 20:47:41 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2015-10-20 20:47:41 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-01-03 18:52:28 +00:00
|
|
|
*
|
2018-05-11 19:43:49 +00:00
|
|
|
* @format
|
2017-01-03 18:52:28 +00:00
|
|
|
* @flow
|
2015-10-20 20:47:41 +00:00
|
|
|
*/
|
2017-01-03 18:52:28 +00:00
|
|
|
|
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');
|
2018-02-19 18:29:01 +00:00
|
|
|
/* $FlowFixMe(site=react_native_oss) */
|
2017-11-29 11:00:49 +00:00
|
|
|
const Server = require('metro/src/Server');
|
2016-10-22 13:07:01 +00:00
|
|
|
|
2018-02-19 18:29:01 +00:00
|
|
|
/* $FlowFixMe(site=react_native_oss) */
|
2017-11-29 11:00:49 +00:00
|
|
|
const outputBundle = require('metro/src/shared/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');
|
|
|
|
|
2017-01-03 18:52:28 +00:00
|
|
|
import type {RequestOptions, OutputOptions} from './types.flow';
|
2018-07-25 12:44:38 +00:00
|
|
|
import type {ConfigT} from 'metro-config/src/configTypes.flow';
|
2017-11-29 12:37:22 +00:00
|
|
|
|
2017-11-08 17:07:41 +00:00
|
|
|
async function buildBundle(
|
2017-01-03 18:52:28 +00:00
|
|
|
args: OutputOptions & {
|
|
|
|
assetsDest: mixed,
|
|
|
|
entryFile: string,
|
2017-06-13 16:11:57 +00:00
|
|
|
maxWorkers: number,
|
2017-01-03 18:52:28 +00:00
|
|
|
resetCache: boolean,
|
|
|
|
transformer: string,
|
2018-01-30 08:35:40 +00:00
|
|
|
minify: boolean,
|
2017-01-03 18:52:28 +00:00
|
|
|
},
|
2018-07-25 12:44:38 +00:00
|
|
|
configPromise: Promise<ConfigT>,
|
2017-01-03 18:52:28 +00:00
|
|
|
output = outputBundle,
|
|
|
|
) {
|
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';
|
2018-07-25 12:44:38 +00:00
|
|
|
const config = await configPromise;
|
2015-10-20 20:47:41 +00:00
|
|
|
|
2017-04-06 16:21:38 +00:00
|
|
|
let sourceMapUrl = args.sourcemapOutput;
|
|
|
|
if (sourceMapUrl && !args.sourcemapUseAbsolutePath) {
|
|
|
|
sourceMapUrl = path.basename(sourceMapUrl);
|
|
|
|
}
|
|
|
|
|
2016-11-21 21:19:13 +00:00
|
|
|
const requestOpts: RequestOptions = {
|
2016-07-30 15:59:16 +00:00
|
|
|
entryFile: args.entryFile,
|
2017-04-06 16:21:38 +00:00
|
|
|
sourceMapUrl,
|
2016-07-30 15:59:16 +00:00
|
|
|
dev: args.dev,
|
2018-01-30 08:35:40 +00:00
|
|
|
minify: args.minify !== undefined ? args.minify : !args.dev,
|
2016-07-30 15:59:16 +00:00
|
|
|
platform: args.platform,
|
|
|
|
};
|
2016-06-22 15:08:28 +00:00
|
|
|
|
2018-08-20 16:56:53 +00:00
|
|
|
const server = new Server({...config, resetCache: args.resetCache});
|
2015-10-20 20:47:41 +00:00
|
|
|
|
2018-07-17 10:14:57 +00:00
|
|
|
try {
|
|
|
|
const bundle = await output.build(server, requestOpts);
|
|
|
|
|
|
|
|
await output.save(bundle, args, log);
|
|
|
|
|
|
|
|
// Save the assets of the bundle
|
|
|
|
const outputAssets = await server.getAssets({
|
|
|
|
...Server.DEFAULT_BUNDLE_OPTIONS,
|
|
|
|
...requestOpts,
|
|
|
|
bundleType: 'todo',
|
|
|
|
});
|
|
|
|
|
|
|
|
// When we're done saving bundle output and the assets, we're done.
|
|
|
|
return await saveAssets(outputAssets, args.platform, args.assetsDest);
|
|
|
|
} finally {
|
|
|
|
server.end();
|
|
|
|
}
|
2015-10-20 20:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = buildBundle;
|