2015-10-20 20:47:41 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
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
|
|
|
*
|
|
|
|
* @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');
|
2017-12-15 14:21:09 +00:00
|
|
|
const {Terminal} = require('metro-core');
|
2018-02-19 18:29:01 +00:00
|
|
|
/* $FlowFixMe(site=react_native_oss) */
|
2017-11-29 11:00:49 +00:00
|
|
|
const TerminalReporter = require('metro/src/lib/TerminalReporter');
|
2018-02-19 18:29:01 +00:00
|
|
|
/* $FlowFixMe(site=react_native_oss) */
|
2017-11-29 11:00:49 +00:00
|
|
|
const TransformCaching = require('metro/src/lib/TransformCaching');
|
2016-10-22 13:07:01 +00:00
|
|
|
|
2017-11-29 12:37:22 +00:00
|
|
|
const {defaults} = require('metro');
|
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-07-25 00:04:38 +00:00
|
|
|
const {ASSET_REGISTRY_PATH} = require('../core/Constants');
|
|
|
|
|
2017-01-03 18:52:28 +00:00
|
|
|
import type {RequestOptions, OutputOptions} from './types.flow';
|
2017-11-29 11:00:49 +00:00
|
|
|
import type {ConfigT} from 'metro';
|
2016-11-21 21:19:13 +00:00
|
|
|
|
2017-11-29 12:37:22 +00:00
|
|
|
const defaultAssetExts = defaults.assetExts;
|
|
|
|
const defaultSourceExts = defaults.sourceExts;
|
|
|
|
const defaultPlatforms = defaults.platforms;
|
|
|
|
const defaultProvidesModuleNodeModules = defaults.providesModuleNodeModules;
|
|
|
|
|
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
|
|
|
},
|
|
|
|
config: ConfigT,
|
|
|
|
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';
|
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-01-12 14:23:16 +00:00
|
|
|
const assetExts = (config.getAssetExts && config.getAssetExts()) || [];
|
|
|
|
const sourceExts = (config.getSourceExts && config.getSourceExts()) || [];
|
|
|
|
const platforms = (config.getPlatforms && config.getPlatforms()) || [];
|
|
|
|
|
|
|
|
const transformModulePath = args.transformer
|
|
|
|
? path.resolve(args.transformer)
|
|
|
|
: config.getTransformModulePath();
|
|
|
|
|
|
|
|
const providesModuleNodeModules =
|
|
|
|
typeof config.getProvidesModuleNodeModules === 'function'
|
|
|
|
? config.getProvidesModuleNodeModules()
|
|
|
|
: defaultProvidesModuleNodeModules;
|
|
|
|
|
|
|
|
const terminal = new Terminal(process.stdout);
|
|
|
|
|
|
|
|
const server = new Server({
|
|
|
|
assetExts: defaultAssetExts.concat(assetExts),
|
|
|
|
assetRegistryPath: ASSET_REGISTRY_PATH,
|
|
|
|
blacklistRE: config.getBlacklistRE(),
|
2018-02-26 11:21:11 +00:00
|
|
|
cacheStores: config.cacheStores,
|
2018-02-14 16:42:30 +00:00
|
|
|
cacheVersion: config.cacheVersion,
|
2018-01-18 15:50:53 +00:00
|
|
|
dynamicDepsInPackages: config.dynamicDepsInPackages,
|
2018-02-14 16:42:28 +00:00
|
|
|
enableBabelRCLookup: config.getEnableBabelRCLookup(),
|
2018-01-12 14:23:16 +00:00
|
|
|
extraNodeModules: config.extraNodeModules,
|
|
|
|
getModulesRunBeforeMainModule: config.getModulesRunBeforeMainModule,
|
|
|
|
getPolyfills: config.getPolyfills,
|
|
|
|
getTransformOptions: config.getTransformOptions,
|
|
|
|
globalTransformCache: null,
|
2018-01-30 19:20:20 +00:00
|
|
|
hasteImplModulePath: config.hasteImplModulePath,
|
2018-01-12 14:23:16 +00:00
|
|
|
maxWorkers: args.maxWorkers,
|
|
|
|
platforms: defaultPlatforms.concat(platforms),
|
|
|
|
postMinifyProcess: config.postMinifyProcess,
|
|
|
|
postProcessModules: config.postProcessModules,
|
|
|
|
postProcessBundleSourcemap: config.postProcessBundleSourcemap,
|
|
|
|
projectRoots: config.getProjectRoots(),
|
|
|
|
providesModuleNodeModules: providesModuleNodeModules,
|
|
|
|
resetCache: args.resetCache,
|
|
|
|
reporter: new TerminalReporter(terminal),
|
2018-02-23 16:46:48 +00:00
|
|
|
sourceExts: sourceExts.concat(defaultSourceExts),
|
2018-01-12 14:23:16 +00:00
|
|
|
transformCache: TransformCaching.useTempDir(),
|
|
|
|
transformModulePath: transformModulePath,
|
|
|
|
watch: false,
|
|
|
|
workerPath: config.getWorkerPath && config.getWorkerPath(),
|
|
|
|
});
|
2015-10-20 20:47:41 +00:00
|
|
|
|
2018-01-12 14:23:16 +00:00
|
|
|
const bundle = await output.build(server, requestOpts);
|
2017-11-08 17:07:41 +00:00
|
|
|
|
|
|
|
await output.save(bundle, args, log);
|
2015-10-20 20:47:41 +00:00
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
// Save the assets of the bundle
|
2018-01-12 14:23:16 +00:00
|
|
|
const outputAssets = await server.getAssets({
|
2017-11-08 17:07:41 +00:00
|
|
|
...Server.DEFAULT_BUNDLE_OPTIONS,
|
|
|
|
...requestOpts,
|
2017-11-28 12:41:10 +00:00
|
|
|
bundleType: 'todo',
|
2017-11-08 17:07:41 +00:00
|
|
|
});
|
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.
|
2017-11-08 17:07:41 +00:00
|
|
|
const assets = await saveAssets(
|
|
|
|
outputAssets,
|
|
|
|
args.platform,
|
|
|
|
args.assetsDest,
|
|
|
|
);
|
|
|
|
|
2018-01-12 14:23:16 +00:00
|
|
|
server.end();
|
2017-11-08 17:07:41 +00:00
|
|
|
|
2016-07-30 15:59:16 +00:00
|
|
|
return assets;
|
2015-10-20 20:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = buildBundle;
|