feat(tools-babel): Add tools-babel package for babel configuration and parsing support#4087
Open
feat(tools-babel): Add tools-babel package for babel configuration and parsing support#4087
Conversation
incubator/tools-babel/README.md
Outdated
| | Domain | Frequency | What is traced | | ||
| | -------------- | --------- | ---------------------------------------------------- | | ||
| | `transform` | medium | Parse operations (OXC native, AST conversion, Babel) | | ||
| | `babel-plugin` | high | Individual plugin visitor method calls | |
Contributor
There was a problem hiding this comment.
Suggested change
| | `babel-plugin` | high | Individual plugin visitor method calls | | |
| | `babel-plugin` | high | Individual plugin visitor method calls | |
incubator/tools-babel/README.md
Outdated
Comment on lines
+261
to
+312
| trackPerformance({ enable: "babel-plugin", strategy: "timing", frequency: "high" }); | ||
| ``` | ||
|
|
||
| ## TransformerSettings | ||
|
|
||
| Settings that persist across transformation passes: | ||
|
|
||
| | Field | Type | Default | Description | | ||
| | ---------------------- | -------------------------- | ------- | ------------------------------------------------------------ | | ||
| | `configCallerMixins` | `Record<string, string>` | -- | Extra fields added to Babel's `caller` config | | ||
| | `configDisabledPlugins`| `Set<string>` | -- | Plugin keys to remove from the resolved config | | ||
| | `parseDisableOxc` | `boolean` | -- | Disable OXC parser | | ||
| | `parseDisableHermes` | `boolean` | -- | Disable Hermes parser | | ||
| | `parseFlowDefault` | `boolean` | `true` | Assume Flow in `.js`/`.jsx` files under `node_modules` | | ||
| | `parseFlowWorkspace` | `boolean` | `false` | Assume Flow in workspace `.js`/`.jsx` files | | ||
| | `parseExtDefault` | `SrcSyntax` | `"js"` | Syntax for unknown file extensions (unset to skip) | | ||
| | `parseExtAliases` | `Record<string, SrcSyntax>`| -- | Map extensions to syntax types (e.g. `{ ".svg": "jsx" }`) | | ||
|
|
||
| ## API Reference | ||
|
|
||
| ### Config | ||
|
|
||
| | Function | Description | | ||
| | ------------------------------------- | -------------------------------------------------------------------------- | | ||
| | `getBabelConfig(args, settings?)` | Build a per-file Babel config from cached base config + file-specific settings | | ||
| | `filterConfigPlugins(config, disabled?)` | Resolve presets/overrides and filter plugins by key | | ||
|
|
||
| ### Parsing | ||
|
|
||
| | Function | Description | | ||
| | ----------------------------- | -------------------------------------------------------------- | | ||
| | `parseToAst(args)` | Parse with fallback chain: OXC -> Hermes -> Babel | | ||
| | `oxcParseToAst(args, trace?)` | Parse with OXC and convert ESTree to Babel AST | | ||
| | `hermesParseToAst(args)` | Parse with Hermes | | ||
| | `toBabelAST(program, source, isTypeScript?, comments?)` | Convert OXC ESTree to Babel AST | | ||
|
|
||
| ### Transformer | ||
|
|
||
| | Function | Description | | ||
| | ------------------------------------------------- | ------------------------------------------------------- | | ||
| | `makeTransformerArgs(babelArgs, settings?, updateContext?)` | Build `TransformerArgs` with context and Babel config | | ||
| | `initTransformerContext(filename, settings)` | Initialize file context without building Babel config | | ||
|
|
||
| ### Plugins | ||
|
|
||
| | Function | Description | | ||
| | --------------------------------------- | --------------------------------------------------------------- | | ||
| | `isConfigItem(plugin)` | Check if plugin is a Babel `ConfigItem` | | ||
| | `isPluginObj(plugin)` | Check if plugin is a resolved `PluginObj` | | ||
| | `getPluginTarget(plugin)` | Extract the plugin target (function or string) | | ||
| | `getPluginKey(plugin)` | Extract the key from a resolved plugin | | ||
| | `updateTransformOptions(options, visitor)` | Walk and modify plugins/presets/overrides in a config | |
Contributor
There was a problem hiding this comment.
Suggested change
| trackPerformance({ enable: "babel-plugin", strategy: "timing", frequency: "high" }); | |
| ``` | |
| ## TransformerSettings | |
| Settings that persist across transformation passes: | |
| | Field | Type | Default | Description | | |
| | ---------------------- | -------------------------- | ------- | ------------------------------------------------------------ | | |
| | `configCallerMixins` | `Record<string, string>` | -- | Extra fields added to Babel's `caller` config | | |
| | `configDisabledPlugins`| `Set<string>` | -- | Plugin keys to remove from the resolved config | | |
| | `parseDisableOxc` | `boolean` | -- | Disable OXC parser | | |
| | `parseDisableHermes` | `boolean` | -- | Disable Hermes parser | | |
| | `parseFlowDefault` | `boolean` | `true` | Assume Flow in `.js`/`.jsx` files under `node_modules` | | |
| | `parseFlowWorkspace` | `boolean` | `false` | Assume Flow in workspace `.js`/`.jsx` files | | |
| | `parseExtDefault` | `SrcSyntax` | `"js"` | Syntax for unknown file extensions (unset to skip) | | |
| | `parseExtAliases` | `Record<string, SrcSyntax>`| -- | Map extensions to syntax types (e.g. `{ ".svg": "jsx" }`) | | |
| ## API Reference | |
| ### Config | |
| | Function | Description | | |
| | ------------------------------------- | -------------------------------------------------------------------------- | | |
| | `getBabelConfig(args, settings?)` | Build a per-file Babel config from cached base config + file-specific settings | | |
| | `filterConfigPlugins(config, disabled?)` | Resolve presets/overrides and filter plugins by key | | |
| ### Parsing | |
| | Function | Description | | |
| | ----------------------------- | -------------------------------------------------------------- | | |
| | `parseToAst(args)` | Parse with fallback chain: OXC -> Hermes -> Babel | | |
| | `oxcParseToAst(args, trace?)` | Parse with OXC and convert ESTree to Babel AST | | |
| | `hermesParseToAst(args)` | Parse with Hermes | | |
| | `toBabelAST(program, source, isTypeScript?, comments?)` | Convert OXC ESTree to Babel AST | | |
| ### Transformer | |
| | Function | Description | | |
| | ------------------------------------------------- | ------------------------------------------------------- | | |
| | `makeTransformerArgs(babelArgs, settings?, updateContext?)` | Build `TransformerArgs` with context and Babel config | | |
| | `initTransformerContext(filename, settings)` | Initialize file context without building Babel config | | |
| ### Plugins | |
| | Function | Description | | |
| | --------------------------------------- | --------------------------------------------------------------- | | |
| | `isConfigItem(plugin)` | Check if plugin is a Babel `ConfigItem` | | |
| | `isPluginObj(plugin)` | Check if plugin is a resolved `PluginObj` | | |
| | `getPluginTarget(plugin)` | Extract the plugin target (function or string) | | |
| | `getPluginKey(plugin)` | Extract the key from a resolved plugin | | |
| | `updateTransformOptions(options, visitor)` | Walk and modify plugins/presets/overrides in a config | | |
| trackPerformance({ | |
| enable: "babel-plugin", | |
| strategy: "timing", | |
| frequency: "high", | |
| }); |
TransformerSettings
Settings that persist across transformation passes:
| Field | Type | Default | Description |
|---|---|---|---|
configCallerMixins |
Record<string, string> |
-- | Extra fields added to Babel's caller config |
configDisabledPlugins |
Set<string> |
-- | Plugin keys to remove from the resolved config |
parseDisableOxc |
boolean |
-- | Disable OXC parser |
parseDisableHermes |
boolean |
-- | Disable Hermes parser |
parseFlowDefault |
boolean |
true |
Assume Flow in .js/.jsx files under node_modules |
parseFlowWorkspace |
boolean |
false |
Assume Flow in workspace .js/.jsx files |
parseExtDefault |
SrcSyntax |
"js" |
Syntax for unknown file extensions (unset to skip) |
parseExtAliases |
Record<string, SrcSyntax> |
-- | Map extensions to syntax types (e.g. { ".svg": "jsx" }) |
API Reference
Config
| Function | Description |
|---|---|
getBabelConfig(args, settings?) |
Build a per-file Babel config from cached base config + file-specific settings |
filterConfigPlugins(config, disabled?) |
Resolve presets/overrides and filter plugins by key |
Parsing
| Function | Description |
|---|---|
parseToAst(args) |
Parse with fallback chain: OXC -> Hermes -> Babel |
oxcParseToAst(args, trace?) |
Parse with OXC and convert ESTree to Babel AST |
hermesParseToAst(args) |
Parse with Hermes |
toBabelAST(program, source, isTypeScript?, comments?) |
Convert OXC ESTree to Babel AST |
Transformer
| Function | Description |
|---|---|
makeTransformerArgs(babelArgs, settings?, updateContext?) |
Build TransformerArgs with context and Babel config |
initTransformerContext(filename, settings) |
Initialize file context without building Babel config |
Plugins
| Function | Description |
|---|---|
isConfigItem(plugin) |
Check if plugin is a Babel ConfigItem |
isPluginObj(plugin) |
Check if plugin is a resolved PluginObj |
getPluginTarget(plugin) |
Extract the plugin target (function or string) |
getPluginKey(plugin) |
Extract the key from a resolved plugin |
updateTransformOptions(options, visitor) |
Walk and modify plugins/presets/overrides in a config |
1ab7c49 to
d67e42a
Compare
…/jasonvmo/tools-babel
…/jasonvmo/tools-babel
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
NOTE: this currently includes the tools-performance change as it depends on that change. Ignore those files here.
Description
This adds a tools project for dealing with babel with regards to react-native transformations. This builds upon some of the work from the metro-transformer-oxc package as well as some of the work I was looking at for using native tools for transformation.
This package has tools that deal with babel configuration and parsing.
Babel Configuration
Several utilities are provided here:
@react-native/metro-babel-transformer, which is the standard upstream babel transformer for react-native.Babel Parsing
Parsing is one of the most time consuming and memory intensive parts of the transformation process. There are faster parsers but they generally produce estree ASTs rather than babel-ASTs. The current metro-transformer-oxc uses a utility estree-to-babel to convert the AST, which mostly works, but when timing the performance it ends up being almost as slow as babel parsing.
The parsing in here:
The tests are fairly wordy for output but this is useful for allowing tools like Claude to gain information about what is happening and what the differences are.