-
Notifications
You must be signed in to change notification settings - Fork 117
feat(tools-react-native): Add mergeTransformerConfigs function to @rnx-kit/tools-react-native #4044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@rnx-kit/tools-react-native": patch | ||
| "@rnx-kit/cli": patch | ||
| --- | ||
|
|
||
| Add function to merge transformer configs to tools-react-native and use it in the cli |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { mergeTransformerConfigs } from "./lib/metro-utils.js"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module.exports = require("./lib/metro-utils.js"); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,77 @@ | ||||||||||||||||||||||||
| import type { GetTransformOptions, TransformerConfigT } from "metro-config"; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * Type guard to check if a value is a plain object (i.e., a record). This is used to ensure that we only attempt to | ||||||||||||||||||||||||
| * recursively merge plain objects in the `simpleObjectMerge` function. | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| function isRecord(value: unknown): value is Record<string, unknown> { | ||||||||||||||||||||||||
| return value !== null && typeof value === "object" && !Array.isArray(value); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * Simple merge helper that recursively merges plain objects. Note that array merges are not supported, | ||||||||||||||||||||||||
| * as the behavior isn't deterministic (e.g., should we concatenate arrays, or override them?). If a property is an array in | ||||||||||||||||||||||||
| * multiple configs, the value from the last config will win. | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| function simpleObjectMerge( | ||||||||||||||||||||||||
| ...options: Record<string, unknown>[] | ||||||||||||||||||||||||
| ): Record<string, unknown> { | ||||||||||||||||||||||||
| /** @type {Record<string, unknown>} */ | ||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stray JSDoc
Suggested change
|
||||||||||||||||||||||||
| const result: Record<string, unknown> = {}; | ||||||||||||||||||||||||
| for (const option of options) { | ||||||||||||||||||||||||
| for (const [key, value] of Object.entries(option)) { | ||||||||||||||||||||||||
| if (isRecord(value) && isRecord(result[key])) { | ||||||||||||||||||||||||
| result[key] = simpleObjectMerge(result[key], value); | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| result[key] = value; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| return result; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * Creates a function that sequentially calls multiple `GetTransformOptions` functions and merges their results. | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| function createGetTransformOptions( | ||||||||||||||||||||||||
| ...subFunctions: GetTransformOptions[] | ||||||||||||||||||||||||
| ): GetTransformOptions { | ||||||||||||||||||||||||
| if (subFunctions.length === 0) { | ||||||||||||||||||||||||
| throw new Error("At least one getTransformOptions function is required"); | ||||||||||||||||||||||||
| } else if (subFunctions.length === 1) { | ||||||||||||||||||||||||
| return subFunctions[0]; | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| return async (entryPoints, options, getDepsOf) => { | ||||||||||||||||||||||||
| const results = await Promise.all( | ||||||||||||||||||||||||
| subFunctions.map((fn) => fn(entryPoints, options, getDepsOf)) | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
| return simpleObjectMerge(...results); | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * Merges multiple Metro transformer configurations into one. Properties from later configs override earlier | ||||||||||||||||||||||||
| * ones. If multiple configs provide a `getTransformOptions` function, the returned config wraps them so that each | ||||||||||||||||||||||||
| * is called in order and their results are deep-merged. | ||||||||||||||||||||||||
| * | ||||||||||||||||||||||||
| * @param configs one or more transformer config objects to merge. Later configs take precedence over earlier ones. | ||||||||||||||||||||||||
| * @returns transformer configuration suitable for use by Metro | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| export function mergeTransformerConfigs( | ||||||||||||||||||||||||
| ...configs: Partial<TransformerConfigT>[] | ||||||||||||||||||||||||
| ): Partial<TransformerConfigT> { | ||||||||||||||||||||||||
| // collect the getTransformOptions functions from all configs, and if there are multiple, we'll create a wrapper function for them | ||||||||||||||||||||||||
| const getTransformOptionsFns = configs | ||||||||||||||||||||||||
| .map((config) => config?.getTransformOptions) | ||||||||||||||||||||||||
| .filter((fn) => typeof fn === "function"); | ||||||||||||||||||||||||
|
Comment on lines
+65
to
+67
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whenever you see
Suggested change
JavaScript's implementation is eager and will cause additional unnecessary allocations. |
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // if there are multiple getTransformOptions functions, create a wrapper function that calls in sequence and merges their results | ||||||||||||||||||||||||
| if (getTransformOptionsFns.length > 1) { | ||||||||||||||||||||||||
| configs.push({ | ||||||||||||||||||||||||
| getTransformOptions: createGetTransformOptions(...getTransformOptionsFns), | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return Object.assign({}, ...configs); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be parameterized?