diff --git a/packages/community-cli-plugin/README.md b/packages/community-cli-plugin/README.md index 3cf597929eb1e9..aebbf39d106fcc 100644 --- a/packages/community-cli-plugin/README.md +++ b/packages/community-cli-plugin/README.md @@ -59,6 +59,7 @@ npx react-native bundle --entry-file [options] | `--minify [boolean]` | Allows overriding whether bundle is minified. Defaults to `false` if `--dev` is set. Disabling minification can be useful for speeding up production builds for testing purposes. | | `--bundle-output ` | Specify the path to store the resulting bundle. | | `--bundle-encoding ` | Specify the encoding for writing the bundle (). | +| `--resolver-option ` | Custom resolver options of the form key=value. URL-encoded. May be specified multiple times. | | `--sourcemap-output ` | Specify the path to store the source map file for the resulting bundle. | | `--sourcemap-sources-root ` | Set the root path for source map entries. | | `--sourcemap-use-absolute-path` | Report `SourceMapURL` using its full path. | diff --git a/packages/community-cli-plugin/package.json b/packages/community-cli-plugin/package.json index 808b12d62edde4..653f9f6ccf5624 100644 --- a/packages/community-cli-plugin/package.json +++ b/packages/community-cli-plugin/package.json @@ -32,6 +32,7 @@ "metro-config": "^0.80.3", "metro-core": "^0.80.3", "node-fetch": "^2.2.0", + "querystring": "^0.2.1", "readline": "^1.3.0" }, "devDependencies": { diff --git a/packages/community-cli-plugin/src/commands/bundle/buildBundle.js b/packages/community-cli-plugin/src/commands/bundle/buildBundle.js index cce7c0030bc81f..98aac3b8fa5c46 100644 --- a/packages/community-cli-plugin/src/commands/bundle/buildBundle.js +++ b/packages/community-cli-plugin/src/commands/bundle/buildBundle.js @@ -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, + resolverOption?: Array, }; async function buildBundle( @@ -65,6 +66,10 @@ async function buildBundleWithConfig( config: ConfigT, bundleImpl: typeof metroBundle | typeof metroRamBundle = metroBundle, ): Promise { + 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); diff --git a/packages/community-cli-plugin/src/commands/bundle/index.js b/packages/community-cli-plugin/src/commands/bundle/index.js index 46f7827dbbf320..1e8522a845cf95 100644 --- a/packages/community-cli-plugin/src/commands/bundle/index.js +++ b/packages/community-cli-plugin/src/commands/bundle/index.js @@ -115,16 +115,11 @@ const bundleCommand: Command = { parse: (val: string): string => path.resolve(val), }, { - name: '--custom-resolver-options ', - description: 'Custom resolver options, format: key=value,key2=value2.', - parse: (val: string): Record => { - return Object.fromEntries( - val.split(',').map(option => { - const [key, value] = option.split('='); - return [key, value]; - }), - ); - }, + name: '--resolver-option ', + description: + 'Custom resolver options of the form key=value. URL-encoded. May be specified multiple times.', + parse: (val: string, previous: Array = []): Array => + previous.concat([val]), }, ], }; diff --git a/packages/community-cli-plugin/src/utils/parseKeyValueParamArray.js b/packages/community-cli-plugin/src/utils/parseKeyValueParamArray.js new file mode 100644 index 00000000000000..021529007aeddf --- /dev/null +++ b/packages/community-cli-plugin/src/utils/parseKeyValueParamArray.js @@ -0,0 +1,30 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict-local + * @format + * @oncall react_native + */ + +import querystring from 'querystring'; + +export default function parseKeyValueParamArray( + keyValueArray: $ReadOnlyArray, +): Record { + const result = {}; + + for (const item of keyValueArray) { + if (item.indexOf('=') === -1) { + throw new Error('Expected parameter to include "=" but found: ' + item); + } + if (item.indexOf('&') !== -1) { + throw new Error('Parameter cannot include "&" but found: ' + item); + } + Object.assign(result, querystring.parse(item)); + } + + return result; +} diff --git a/yarn.lock b/yarn.lock index 9172d3ded60d81..1195f86032bcaf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8073,6 +8073,11 @@ query-string@^6.12.1: split-on-first "^1.0.0" strict-uri-encode "^2.0.0" +querystring@^0.2.1: + version "0.2.1" + resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.1.tgz#40d77615bb09d16902a85c3e38aa8b5ed761c2dd" + integrity sha512-wkvS7mL/JMugcup3/rMitHmd9ecIGd2lhFhK9N3UUQ450h66d1r3Y9nvXzQAW1Lq+wyx61k/1pfKS5KuKiyEbg== + queue-microtask@^1.2.2: version "1.2.3" resolved "https://registry.yarnpkg.com/queue-microtask/-/queue-microtask-1.2.3.tgz#4929228bbc724dfac43e0efb058caf7b6cfb6243"