-
Notifications
You must be signed in to change notification settings - Fork 117
feat(metro-transformer): Add @rnx-kit/metro-transformer package and transformer plugins #4022
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
Closed
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b8c5fac
first draft of the metro-transformer package
JasonVMo cd2c57e
add common transformer package and centralize configuration types
JasonVMo 92a0bc8
start of cli changes
JasonVMo f788ab7
Merge branch 'main' of https://github.com/microsoft/rnx-kit into user…
JasonVMo a4ebfe0
update vscode settings to use new tools
JasonVMo ebe6bc6
update readmes, package entries, and reduce some complexity
JasonVMo 1fc3b15
docs(changeset): Add @rnx-kit/metro-transformer with support for tran…
JasonVMo c87ab7e
docs(changeset): Consume new metro-transformer package
JasonVMo e701423
mark types package public
JasonVMo 6d492db
docs(changeset): Add types package for common rnx-kit metro configura…
JasonVMo 2a36067
docs(changeset): Use new types package for serializer types
JasonVMo 3d8be3b
Merge branch 'main' of https://github.com/microsoft/rnx-kit into user…
JasonVMo 0125839
remove temp file packages from the branch
JasonVMo e46712f
fix align deps issue
JasonVMo a68124f
respond to PR feedback
JasonVMo 284e3f8
another piece of PR feedback
JasonVMo c05dbac
fix lockfile issue
JasonVMo 19bd442
merging and resolution fixes from pr comments
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,5 @@ | ||
| --- | ||
| "@rnx-kit/metro-transformer": major | ||
| --- | ||
|
|
||
| Add @rnx-kit/metro-transformer with support for transformer merging and selecting babel transformers based on file type |
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,5 @@ | ||
| --- | ||
| "@rnx-kit/metro-serializer": patch | ||
| --- | ||
|
|
||
| Use new types package for serializer types |
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/cli": minor | ||
| "@rnx-kit/metro-serializer": patch | ||
| --- | ||
|
|
||
| Consume new metro-transformer package |
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,5 @@ | ||
| --- | ||
| "@rnx-kit/types-metro-config": major | ||
| --- | ||
|
|
||
| Add types package for common rnx-kit metro configuration types |
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
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 |
|---|---|---|
|
|
@@ -2,14 +2,24 @@ import { warn } from "@rnx-kit/console"; | |
| import { CyclicDependencies } from "@rnx-kit/metro-plugin-cyclic-dependencies-detector"; | ||
| import { DuplicateDependencies } from "@rnx-kit/metro-plugin-duplicates-checker"; | ||
| import { TypeScriptPlugin } from "@rnx-kit/metro-plugin-typescript"; | ||
| import type { MetroPlugin } from "@rnx-kit/metro-serializer"; | ||
| import { MetroSerializer } from "@rnx-kit/metro-serializer"; | ||
| import { | ||
| esbuildTransformerConfig, | ||
| MetroSerializer as MetroSerializerEsbuild, | ||
| } from "@rnx-kit/metro-serializer-esbuild"; | ||
| import { MetroTransformer } from "@rnx-kit/metro-transformer"; | ||
| import type { BundleParameters } from "@rnx-kit/types-bundle-config"; | ||
| import type { ConfigT, SerializerConfigT } from "metro-config"; | ||
| import type { | ||
| TransformerPlugin, | ||
| SerializerHookPlugin, | ||
| SerializerPlugin, | ||
| ExtendedTransformerConfig, | ||
| } from "@rnx-kit/types-metro-config"; | ||
| import type { | ||
| ConfigT, | ||
| SerializerConfigT, | ||
| TransformerConfigT, | ||
| } from "metro-config"; | ||
| import type { WritableDeep } from "type-fest"; | ||
| import { getDefaultBundlerPlugins } from "../bundle/defaultPlugins.ts"; | ||
|
|
||
|
|
@@ -104,11 +114,11 @@ export function customizeMetroConfig( | |
| // with readonly props to a type where the props are writeable. | ||
| const metroConfig = metroConfigReadonly as WritableDeep<ConfigT>; | ||
|
|
||
| const metroPlugins: MetroPlugin[] = []; | ||
| const serializerHooks: Record< | ||
| string, | ||
| SerializerConfigT["experimentalSerializerHook"] | ||
| > = {}; | ||
| const metroPlugins: SerializerPlugin[] = []; | ||
| const serializerHooks: Record<string, SerializerHookPlugin> = {}; | ||
| const transformers: ExtendedTransformerConfig[] = metroConfig.transformer | ||
| ? [metroConfig.transformer] | ||
| : []; | ||
|
|
||
| const legacyWarning = readLegacyOptions(extraParams); | ||
| if (legacyWarning) { | ||
|
|
@@ -152,6 +162,15 @@ export function customizeMetroConfig( | |
| serializerHooks[module] = plugin(options, print); | ||
| break; | ||
|
|
||
| case "transformer": | ||
| { | ||
| const transformerPlugin: TransformerPlugin = plugin(options, print); | ||
| if (transformerPlugin.transformer) { | ||
| transformers.unshift(transformerPlugin.transformer); | ||
| } | ||
| } | ||
| break; | ||
|
|
||
|
JasonVMo marked this conversation as resolved.
|
||
| default: | ||
| throw new Error(`${module}: unknown plugin type: ${plugin.type}`); | ||
| } | ||
|
|
@@ -170,7 +189,8 @@ export function customizeMetroConfig( | |
| ? extraParams.treeShake | ||
| : undefined | ||
| ); | ||
| Object.assign(metroConfig.transformer, esbuildTransformerConfig); | ||
| // otherwise, add it to the list to be merged by the MetroTransformer | ||
|
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. Why otherwise? |
||
| transformers.push(esbuildTransformerConfig); | ||
| } else if (metroPlugins.length > 0) { | ||
| // MetroSerializer acts as a CustomSerializer, and it works with both | ||
| // older and newer versions of Metro. Older versions expect a return | ||
|
|
@@ -189,6 +209,12 @@ export function customizeMetroConfig( | |
| // We don't want this set if unused | ||
| delete metroConfig.serializer.customSerializer; | ||
| } | ||
| // Use the MetroTransformer to resolve the final transformer, if there are no valid transformers it will return an empty | ||
| // config. If there is one and it is already valid it will return it as is, if there are multiple or if they use extended | ||
| // options it will merge and transform them. | ||
| metroConfig.transformer = MetroTransformer( | ||
| ...transformers | ||
| ) as WritableDeep<TransformerConfigT>; | ||
|
|
||
| const hooks = Object.values(serializerHooks); | ||
| switch (hooks.length) { | ||
|
|
||
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,78 @@ | ||
| # @rnx-kit/metro-transformer | ||
|
|
||
| [](https://github.com/microsoft/rnx-kit/actions/workflows/build.yml) | ||
| [](https://www.npmjs.com/package/@rnx-kit/metro-transformer) | ||
|
|
||
| `@rnx-kit/metro-transformer` is a pluggable Metro transformer that lets you | ||
| route files to different Babel transformers based on glob patterns and merge transformer configuration | ||
| from multiple sources. | ||
|
|
||
| ## Installation | ||
|
|
||
| ```sh | ||
| yarn add @rnx-kit/metro-transformer --dev | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| Use `MetroTransformer` to build the `transformer` section of your Metro config: | ||
|
|
||
| ```js | ||
| // metro.config.js | ||
| const { makeMetroConfig } = require("@rnx-kit/metro-config"); | ||
| const { MetroTransformer } = require("@rnx-kit/metro-transformer"); | ||
| const { resolve } = require("node:path"); | ||
|
|
||
| module.exports = makeMetroConfig({ | ||
| transformer: MetroTransformer({ | ||
| // any standard options can be used... | ||
| getTransformOptions: async () => ({ | ||
| // options here | ||
| }), | ||
| // add in a specific file babel transformer | ||
| babelTransformers: { | ||
| // Route .svg files through react-native-svg-transformer | ||
| "**/*.svg": resolve( | ||
| require.resolve("react-native-svg-transformer/package.json"), | ||
| "../transformer.js" | ||
| ), | ||
| }, | ||
| }), | ||
| }); | ||
| ``` | ||
|
|
||
| ## Configuration | ||
|
|
||
| `MetroTransformer(config)` accepts any number of `ExtendedTransformerConfig` objects | ||
| and returns a `TransformerConfigT` suitable for Metro's `transformer` field. | ||
|
|
||
| ### `babelTransformers` | ||
|
|
||
| `Record<string, string>` — Maps glob patterns to absolute paths of Babel | ||
| transformers. When Metro processes a file, patterns are tested in insertion | ||
| order and the first match wins. Patterns are matched with | ||
| [micromatch](https://github.com/micromatch/micromatch) against the full file | ||
| path, so use `**` to match across directories (e.g. `"**/*.svg"` rather than | ||
| `"*.svg"`). | ||
|
|
||
| ```js | ||
| MetroTransformer({ | ||
| babelTransformers: { | ||
| "**/*.svg": require.resolve("react-native-svg-transformer/transformer"), | ||
| "**/*.png": require.resolve("./myImageTransformer"), | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| ### `customTransformerOptions` | ||
|
|
||
| `Record<string, unknown>` — Arbitrary options merged into | ||
| `customTransformOptions` and forwarded to every Babel transformer. Useful for | ||
| passing configuration through to delegate transformers. | ||
|
|
||
| ### Standard Metro transformer options | ||
|
|
||
| Any other field accepted by Metro's `TransformerConfigT` (e.g. | ||
| `getTransformOptions`, `babelTransformerPath`) can be included and will be | ||
| merged into the final config. Multiple configs are merged left-to-right, with | ||
| later values overwriting earlier ones. |
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,47 @@ | ||
| { | ||
| "name": "@rnx-kit/metro-transformer", | ||
| "version": "0.0.1", | ||
| "description": "metro-transformer", | ||
| "homepage": "https://github.com/microsoft/rnx-kit/tree/main/packages/metro-transformer#readme", | ||
| "license": "MIT", | ||
| "author": { | ||
| "name": "Microsoft Open Source", | ||
| "email": "microsoftopensource@users.noreply.github.com" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/microsoft/rnx-kit", | ||
| "directory": "packages/metro-transformer" | ||
| }, | ||
| "files": [ | ||
| "lib/**/*.d.ts", | ||
| "lib/**/*.js" | ||
| ], | ||
| "type": "commonjs", | ||
| "main": "lib/index.js", | ||
| "types": "lib/index.d.ts", | ||
| "scripts": { | ||
| "build": "rnx-kit-scripts build", | ||
| "format": "rnx-kit-scripts format", | ||
| "lint": "rnx-kit-scripts lint", | ||
| "test": "rnx-kit-scripts test" | ||
| }, | ||
| "dependencies": { | ||
| "@rnx-kit/types-metro-config": "^0.0.1", | ||
| "micromatch": "^4.0.8" | ||
| }, | ||
| "devDependencies": { | ||
| "@babel/core": "^7.20.0", | ||
| "@react-native/metro-babel-transformer": "^0.83.0", | ||
| "@rnx-kit/scripts": "*", | ||
| "@rnx-kit/tsconfig": "*", | ||
| "@types/babel__core": "^7.20.0", | ||
| "@types/micromatch": "^4.0.10", | ||
| "metro": "^0.83.3", | ||
| "metro-babel-transformer": "^0.83.3", | ||
| "metro-config": "^0.83.3" | ||
| }, | ||
| "engines": { | ||
| "node": ">=18.12" | ||
| } | ||
| } |
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.
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.
transformeris always set because Metro requires it. This also means that we will always run the merger logic below.I tested this locally with only tree shaking enabled and this is what it returned:
Ideally, we don't want this plugin to be running at all if it's not set.