2015-10-20 13:47:41 -07:00
|
|
|
/**
|
2018-09-11 15:27:47 -07:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2015-10-20 13:47:41 -07:00
|
|
|
*
|
2018-02-16 18:24:55 -08: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 10:52:28 -08:00
|
|
|
*
|
2018-05-11 12:43:49 -07:00
|
|
|
* @format
|
2017-01-03 10:52:28 -08:00
|
|
|
* @flow
|
2015-10-20 13:47:41 -07:00
|
|
|
*/
|
2017-01-03 10:52:28 -08:00
|
|
|
|
2016-11-14 11:12:24 -08:00
|
|
|
'use strict';
|
2015-10-20 13:47:41 -07:00
|
|
|
|
|
|
|
const log = require('../util/log').out('bundle');
|
2018-02-19 10:29:01 -08:00
|
|
|
/* $FlowFixMe(site=react_native_oss) */
|
2017-11-29 03:00:49 -08:00
|
|
|
const Server = require('metro/src/Server');
|
2016-10-22 06:07:01 -07:00
|
|
|
|
2018-02-19 10:29:01 -08:00
|
|
|
/* $FlowFixMe(site=react_native_oss) */
|
2017-11-29 03:00:49 -08:00
|
|
|
const outputBundle = require('metro/src/shared/output/bundle');
|
2016-06-06 17:45:30 -07:00
|
|
|
const path = require('path');
|
2015-11-20 20:17:17 -08:00
|
|
|
const saveAssets = require('./saveAssets');
|
|
|
|
|
2017-01-03 10:52:28 -08:00
|
|
|
import type {RequestOptions, OutputOptions} from './types.flow';
|
2018-07-25 05:44:38 -07:00
|
|
|
import type {ConfigT} from 'metro-config/src/configTypes.flow';
|
2017-11-29 04:37:22 -08:00
|
|
|
|
2017-11-08 09:07:41 -08:00
|
|
|
async function buildBundle(
|
2017-01-03 10:52:28 -08:00
|
|
|
args: OutputOptions & {
|
|
|
|
assetsDest: mixed,
|
|
|
|
entryFile: string,
|
2017-06-13 09:11:57 -07:00
|
|
|
maxWorkers: number,
|
2017-01-03 10:52:28 -08:00
|
|
|
resetCache: boolean,
|
|
|
|
transformer: string,
|
2018-01-30 00:35:40 -08:00
|
|
|
minify: boolean,
|
2017-01-03 10:52:28 -08:00
|
|
|
},
|
2018-07-25 05:44:38 -07:00
|
|
|
configPromise: Promise<ConfigT>,
|
2017-01-03 10:52:28 -08:00
|
|
|
output = outputBundle,
|
|
|
|
) {
|
2016-07-30 08:59:16 -07: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 05:44:38 -07:00
|
|
|
const config = await configPromise;
|
2015-10-20 13:47:41 -07:00
|
|
|
|
2017-04-06 09:21:38 -07:00
|
|
|
let sourceMapUrl = args.sourcemapOutput;
|
|
|
|
if (sourceMapUrl && !args.sourcemapUseAbsolutePath) {
|
|
|
|
sourceMapUrl = path.basename(sourceMapUrl);
|
|
|
|
}
|
|
|
|
|
2016-11-21 13:19:13 -08:00
|
|
|
const requestOpts: RequestOptions = {
|
2016-07-30 08:59:16 -07:00
|
|
|
entryFile: args.entryFile,
|
2017-04-06 09:21:38 -07:00
|
|
|
sourceMapUrl,
|
2016-07-30 08:59:16 -07:00
|
|
|
dev: args.dev,
|
2018-01-30 00:35:40 -08:00
|
|
|
minify: args.minify !== undefined ? args.minify : !args.dev,
|
2016-07-30 08:59:16 -07:00
|
|
|
platform: args.platform,
|
|
|
|
};
|
2016-06-22 08:08:28 -07:00
|
|
|
|
2018-08-20 09:56:53 -07:00
|
|
|
const server = new Server({...config, resetCache: args.resetCache});
|
2015-10-20 13:47:41 -07:00
|
|
|
|
2018-07-17 03:14:57 -07: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 13:47:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = buildBundle;
|