-
Notifications
You must be signed in to change notification settings - Fork 886
/
buildBundle.ts
118 lines (103 loc) · 3.44 KB
/
buildBundle.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import Server from 'metro/src/Server';
// @ts-ignore - no typed definition for the package
const outputBundle = require('metro/src/shared/output/bundle');
import type {BundleOptions} from 'metro/shared/types';
import type {ConfigT} from 'metro-config';
import path from 'path';
import chalk from 'chalk';
import {CommandLineArgs} from './bundleCommandLineArgs';
import type {Config} from '@react-native-community/cli-types';
import saveAssets from './saveAssets';
import {default as loadMetroConfig} from '../../tools/loadMetroConfig';
import {logger} from '@react-native-community/cli-tools';
interface RequestOptions {
entryFile: string;
sourceMapUrl: string | undefined;
dev: boolean;
minify: boolean;
platform: string | undefined;
unstable_transformProfile: BundleOptions['unstable_transformProfile'];
generateStaticViewConfigs: boolean;
}
async function buildBundle(
args: CommandLineArgs,
ctx: Config,
output: typeof outputBundle = outputBundle,
) {
const config = await loadMetroConfig(ctx, {
maxWorkers: args.maxWorkers,
resetCache: args.resetCache,
config: args.config,
});
return buildBundleWithConfig(args, config, output);
}
/**
* Create a bundle using a pre-loaded Metro config. The config can be
* re-used for several bundling calls if multiple platforms are being
* bundled.
*/
export async function buildBundleWithConfig(
args: CommandLineArgs,
config: ConfigT,
output: typeof outputBundle = outputBundle,
) {
if (config.resolver.platforms.indexOf(args.platform) === -1) {
logger.error(
`Invalid platform ${
args.platform ? `"${chalk.bold(args.platform)}" ` : ''
}selected.`,
);
logger.info(
`Available platforms are: ${config.resolver.platforms
.map((x) => `"${chalk.bold(x)}"`)
.join(
', ',
)}. If you are trying to bundle for an out-of-tree platform, it may not be installed.`,
);
throw new Error('Bundling failed');
}
// 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';
let sourceMapUrl = args.sourcemapOutput;
if (sourceMapUrl && !args.sourcemapUseAbsolutePath) {
sourceMapUrl = path.basename(sourceMapUrl);
}
const requestOpts: RequestOptions = {
entryFile: args.entryFile,
sourceMapUrl,
dev: args.dev,
minify: args.minify !== undefined ? args.minify : !args.dev,
platform: args.platform,
unstable_transformProfile: args.unstableTransformProfile as BundleOptions['unstable_transformProfile'],
generateStaticViewConfigs: args.generateStaticViewConfigs,
};
const server = new Server(config);
try {
const bundle = await output.build(server, requestOpts);
await output.save(bundle, args, logger.info);
// 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,
args.assetCatalogDest,
);
} finally {
server.end();
}
}
export default buildBundle;