-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b3680aa
add function for merging transformer configs to tools-react-native
JasonVMo 7deda09
add change files
JasonVMo 5e3f716
Merge branch 'main' of https://github.com/microsoft/rnx-kit into user…
JasonVMo dbd3027
Apply suggestions from code review
JasonVMo 46f98c4
Merge branch 'user/jasonvmo/rn-metro-utils' of https://github.com/mic…
JasonVMo 010cb15
address PR feedback
JasonVMo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { mergeTransformerConfigs } from "./lib/metro-utils.js"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| module.exports = require("./lib/metro-utils.js"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| 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> { | ||
| 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.reduce< | ||
| TransformerConfigT["getTransformOptions"][] | ||
| >((result, config) => { | ||
| const getTransformOptions = config?.getTransformOptions; | ||
| if (typeof getTransformOptions === "function") { | ||
| result.push(getTransformOptions); | ||
| } | ||
| return result; | ||
| }, []); | ||
|
|
||
| // 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); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.