Skip to content

Commit 90fa286

Browse files
huntiefacebook-github-bot
authored andcommitted
Align parsing of custom resolver options, rename arg (#42392)
Summary: Follow-up to #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 #42333) Reviewed By: motiz88 Differential Revision: D52911017
1 parent fd0ca4d commit 90fa286

File tree

6 files changed

+50
-13
lines changed

6 files changed

+50
-13
lines changed

packages/community-cli-plugin/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ npx react-native bundle --entry-file <path> [options]
5959
| `--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. |
6060
| `--bundle-output <string>` | Specify the path to store the resulting bundle. |
6161
| `--bundle-encoding <string>` | Specify the encoding for writing the bundle (<https://nodejs.org/api/buffer.html#buffer_buffer>). |
62+
| `--resolver-option <string...>` | Custom resolver options of the form key=value. URL-encoded. May be specified multiple times. |
6263
| `--sourcemap-output <string>` | Specify the path to store the source map file for the resulting bundle. |
6364
| `--sourcemap-sources-root <string>` | Set the root path for source map entries. |
6465
| `--sourcemap-use-absolute-path` | Report `SourceMapURL` using its full path. |

packages/community-cli-plugin/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@
2222
"dist"
2323
],
2424
"dependencies": {
25-
"@react-native/dev-middleware": "0.74.0",
2625
"@react-native-community/cli-server-api": "13.0.0",
2726
"@react-native-community/cli-tools": "13.0.0",
27+
"@react-native/dev-middleware": "0.74.0",
2828
"@react-native/metro-babel-transformer": "0.74.0",
2929
"chalk": "^4.0.0",
3030
"execa": "^5.1.1",
3131
"metro": "^0.80.3",
3232
"metro-config": "^0.80.3",
3333
"metro-core": "^0.80.3",
3434
"node-fetch": "^2.2.0",
35+
"querystring": "^0.2.1",
3536
"readline": "^1.3.0"
3637
},
3738
"devDependencies": {

packages/community-cli-plugin/src/commands/bundle/buildBundle.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type {ConfigT} from 'metro-config';
1414
import type {RequestOptions} from 'metro/src/shared/types.flow';
1515

1616
import loadMetroConfig from '../../utils/loadMetroConfig';
17+
import parseKeyValueParamArray from '../../utils/parseKeyValueParamArray';
1718
import saveAssets from './saveAssets';
1819
import {logger} from '@react-native-community/cli-tools';
1920
import chalk from 'chalk';
@@ -42,7 +43,7 @@ export type BundleCommandArgs = {
4243
verbose: boolean,
4344
unstableTransformProfile: string,
4445
indexedRamBundle?: boolean,
45-
customResolverOptions?: Record<string, string>,
46+
resolverOption?: Array<string>,
4647
};
4748

4849
async function buildBundle(
@@ -65,6 +66,10 @@ async function buildBundleWithConfig(
6566
config: ConfigT,
6667
bundleImpl: typeof metroBundle | typeof metroRamBundle = metroBundle,
6768
): Promise<void> {
69+
const customResolverOptions = parseKeyValueParamArray(
70+
args.resolverOption ?? [],
71+
);
72+
6873
if (config.resolver.platforms.indexOf(args.platform) === -1) {
6974
logger.error(
7075
`Invalid platform ${
@@ -100,7 +105,7 @@ async function buildBundleWithConfig(
100105
minify: args.minify !== undefined ? args.minify : !args.dev,
101106
platform: args.platform,
102107
unstable_transformProfile: args.unstableTransformProfile,
103-
customResolverOptions: args.customResolverOptions,
108+
customResolverOptions,
104109
};
105110
const server = new Server(config);
106111

packages/community-cli-plugin/src/commands/bundle/index.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,11 @@ const bundleCommand: Command = {
115115
parse: (val: string): string => path.resolve(val),
116116
},
117117
{
118-
name: '--custom-resolver-options <string>',
119-
description: 'Custom resolver options, format: key=value,key2=value2.',
120-
parse: (val: string): Record<string, string> => {
121-
return Object.fromEntries(
122-
val.split(',').map(option => {
123-
const [key, value] = option.split('=');
124-
return [key, value];
125-
}),
126-
);
127-
},
118+
name: '--resolver-option <string...>',
119+
description:
120+
'Custom resolver options of the form key=value. URL-encoded. May be specified multiple times.',
121+
parse: (val: string, previous: Array<string> = []): Array<string> =>
122+
previous.concat([val]),
128123
},
129124
],
130125
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
* @oncall react_native
10+
*/
11+
12+
import querystring from 'querystring';
13+
14+
export default function parseKeyValueParamArray(
15+
keyValueArray: $ReadOnlyArray<string>,
16+
): Record<string, string> {
17+
const result = {};
18+
19+
for (const item of keyValueArray) {
20+
if (item.indexOf('=') === -1) {
21+
throw new Error('Expected parameter to include "=" but found: ' + item);
22+
}
23+
if (item.indexOf('&') !== -1) {
24+
throw new Error('Parameter cannot include "&" but found: ' + item);
25+
}
26+
Object.assign(result, querystring.parse(item));
27+
}
28+
29+
return result;
30+
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8073,6 +8073,11 @@ query-string@^6.12.1:
80738073
split-on-first "^1.0.0"
80748074
strict-uri-encode "^2.0.0"
80758075

8076+
querystring@^0.2.1:
8077+
version "0.2.1"
8078+
resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.1.tgz#40d77615bb09d16902a85c3e38aa8b5ed761c2dd"
8079+
integrity sha512-wkvS7mL/JMugcup3/rMitHmd9ecIGd2lhFhK9N3UUQ450h66d1r3Y9nvXzQAW1Lq+wyx61k/1pfKS5KuKiyEbg==
8080+
80768081
queue-microtask@^1.2.2:
80778082
version "1.2.3"
80788083
resolved "https://registry.yarnpkg.com/queue-microtask/-/queue-microtask-1.2.3.tgz#4929228bbc724dfac43e0efb058caf7b6cfb6243"

0 commit comments

Comments
 (0)