Align parsing of custom resolver options, rename arg (#42392)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/42392

Follow-up to https://github.com/facebook/react-native/pull/42333 following internal feedback. We are now aligning this to match the [`metro build` command](https://github.com/facebook/metro/blob/702e1b8fc7ab8b973bcd53f1a41f7e797cbf7dca/packages/metro/src/commands/build.js#L85-L91). This also improves validation on parsing (done after initial `commander` arg parsing as variadic string).

Changelog: [Internal]
(same as https://github.com/facebook/react-native/pull/42333)

Reviewed By: motiz88

Differential Revision: D52911017

fbshipit-source-id: 54049aa20c9db344a0f485fddf62fb267e672376
This commit is contained in:
Alex Hunt
2024-01-22 10:31:51 -08:00
committed by Facebook GitHub Bot
parent 4681e407b2
commit 4e92f87dfd
6 changed files with 49 additions and 12 deletions
@@ -14,6 +14,7 @@ import type {ConfigT} from 'metro-config';
import type {RequestOptions} from 'metro/src/shared/types.flow';
import loadMetroConfig from '../../utils/loadMetroConfig';
import parseKeyValueParamArray from '../../utils/parseKeyValueParamArray';
import saveAssets from './saveAssets';
import {logger} from '@react-native-community/cli-tools';
import chalk from 'chalk';
@@ -42,7 +43,7 @@ export type BundleCommandArgs = {
verbose: boolean,
unstableTransformProfile: string,
indexedRamBundle?: boolean,
customResolverOptions?: Record<string, string>,
resolverOption?: Array<string>,
};
async function buildBundle(
@@ -65,6 +66,10 @@ async function buildBundleWithConfig(
config: ConfigT,
bundleImpl: typeof metroBundle | typeof metroRamBundle = metroBundle,
): Promise<void> {
const customResolverOptions = parseKeyValueParamArray(
args.resolverOption ?? [],
);
if (config.resolver.platforms.indexOf(args.platform) === -1) {
logger.error(
`Invalid platform ${
@@ -100,7 +105,7 @@ async function buildBundleWithConfig(
minify: args.minify !== undefined ? args.minify : !args.dev,
platform: args.platform,
unstable_transformProfile: args.unstableTransformProfile,
customResolverOptions: args.customResolverOptions,
customResolverOptions,
};
const server = new Server(config);
@@ -115,16 +115,11 @@ const bundleCommand: Command = {
parse: (val: string): string => path.resolve(val),
},
{
name: '--custom-resolver-options <string>',
description: 'Custom resolver options, format: key=value,key2=value2.',
parse: (val: string): Record<string, string> => {
return Object.fromEntries(
val.split(',').map(option => {
const [key, value] = option.split('=');
return [key, value];
}),
);
},
name: '--resolver-option <string...>',
description:
'Custom resolver options of the form key=value. URL-encoded. May be specified multiple times.',
parse: (val: string, previous: Array<string> = []): Array<string> =>
previous.concat([val]),
},
],
};