xref: /expo/packages/@expo/cli/src/export/embed/index.ts (revision 9d7b0c19)
1#!/usr/bin/env node
2import arg from 'arg';
3import chalk from 'chalk';
4import path from 'path';
5
6import { Command } from '../../../bin/cli';
7import { assertWithOptionsArgs, printHelp } from '../../utils/args';
8
9export const expoExportEmbed: Command = async (argv) => {
10  const rawArgsMap: arg.Spec = {
11    // Types
12    '--entry-file': String,
13    '--platform': String,
14    '--transformer': String,
15    '--bundle-output': String,
16    '--bundle-encoding': String,
17    '--max-workers': Number,
18    '--sourcemap-output': String,
19    '--sourcemap-sources-root': String,
20    '--assets-dest': String,
21    '--asset-catalog-dest': String,
22    '--unstable-transform-profile': String,
23    '--config': String,
24
25    // This is here for compatibility with the `npx react-native bundle` command.
26    // devs should use `DEBUG=expo:*` instead.
27    '--verbose': Boolean,
28    '--help': Boolean,
29    // Aliases
30    '-h': '--help',
31    '-v': '--verbose',
32  };
33  const args = assertWithOptionsArgs(rawArgsMap, {
34    argv,
35    permissive: true,
36  });
37
38  if (args['--help']) {
39    printHelp(
40      `(Internal) Export the JavaScript bundle during a native build script for embedding in a native binary`,
41      chalk`npx expo export:embed {dim <dir>}`,
42      [
43        chalk`<dir>                                  Directory of the Expo project. {dim Default: Current working directory}`,
44        `--entry-file <path>                    Path to the root JS file, either absolute or relative to JS root`,
45        `--platform <string>                    Either "ios" or "android" (default: "ios")`,
46        `--transformer <string>                 Specify a custom transformer to be used`,
47        `--dev [boolean]                        If false, warnings are disabled and the bundle is minified (default: true)`,
48        `--minify [boolean]                     Allows overriding whether bundle is minified. This defaults to false if dev is true, and true if dev is false. Disabling minification can be useful for speeding up production builds for testing purposes.`,
49        `--bundle-output <string>               File name where to store the resulting bundle, ex. /tmp/groups.bundle`,
50        `--bundle-encoding <string>             Encoding the bundle should be written in (https://nodejs.org/api/buffer.html#buffer_buffer). (default: "utf8")`,
51        `--max-workers <number>                 Specifies the maximum number of workers the worker-pool will spawn for transforming files. This defaults to the number of the cores available on your machine.`,
52        `--sourcemap-output <string>            File name where to store the sourcemap file for resulting bundle, ex. /tmp/groups.map`,
53        `--sourcemap-sources-root <string>      Path to make sourcemap's sources entries relative to, ex. /root/dir`,
54        `--sourcemap-use-absolute-path          Report SourceMapURL using its full path`,
55        `--assets-dest <string>                 Directory name where to store assets referenced in the bundle`,
56        `--asset-catalog-dest <string>          Directory to create an iOS Asset Catalog for images`,
57        `--unstable-transform-profile <string>  Experimental, transform JS for a specific JS engine. Currently supported: hermes, hermes-canary, default`,
58        `--reset-cache                          Removes cached files`,
59        `-v, --verbose                          Enables debug logging`,
60
61        `--config <string>                      Path to the CLI configuration file`,
62        `--generate-static-view-configs         Generate static view configs for Fabric components. If there are no Fabric components in the bundle or Fabric is disabled, this is just no-op.`,
63        // This is seemingly unused.
64        `--read-global-cache                    Try to fetch transformed JS code from the global cache, if configured.`,
65
66        `-h, --help                             Usage info`,
67      ].join('\n')
68    );
69  }
70
71  const [
72    { exportEmbedAsync },
73    { resolveOptions },
74    { logCmdError },
75    { resolveCustomBooleanArgsAsync },
76  ] = await Promise.all([
77    import('./exportEmbedAsync'),
78    import('./resolveOptions'),
79    import('../../utils/errors'),
80    import('../../utils/resolveArgs'),
81  ]);
82
83  return (async () => {
84    const parsed = await resolveCustomBooleanArgsAsync(argv ?? [], rawArgsMap, {
85      '--dev': Boolean,
86      '--minify': Boolean,
87      '--sourcemap-use-absolute-path': Boolean,
88      '--reset-cache': Boolean,
89      '--read-global-cache': Boolean,
90      '--generate-static-view-configs': Boolean,
91    });
92    return exportEmbedAsync(path.resolve(parsed.projectRoot), resolveOptions(args, parsed));
93  })().catch(logCmdError);
94};
95