Skip to content
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

Allow customizing the babel transform runtime version #771

Conversation

newyankeecodeshop
Copy link
Contributor

Summary

As described in PR #714 , when the bundle is built using a recent version of babel, some helpers are defined repeatedly instead of being referenced via the runtime. In our case, _getRequireWildcardCache() and _interopRequireWildcard().

Since the react native preset configures @babel/plugin-transform-runtime when enableBabelRuntime is enabled, I thought it would be straightforward to overload this metro configuration option to allow the runtime version to be specified. Therefore, changing babel.config.js to be:

module.exports = {
  presets: [
    [
      "module:metro-react-native-babel-preset",
      { enableBabelRuntime: "7.14.5" }
    ]
  ]
};

The above would both enable the runtime and configure it to use version 7.14.5 behaviors. An app developer can do yarn why @babel/runtime to determine the runtime version and specify it in this configuration.

Adding this option doesn't change the behavior for existing users who are using true | false | undefined for the babel runtime configuration.

Test plan

All unit tests pass.

I modified our application as specified above while referencing my modified build of metro. The bundle showed a 6% size reduction and _interopRequireWildcard was required via the runtime.

Verified

This commit was signed with the committer’s verified signature.
wagoodman Alex Goodman
@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Feb 10, 2022

Verified

This commit was signed with the committer’s verified signature.
wagoodman Alex Goodman
extraPlugins.push([
require('@babel/plugin-transform-runtime'),
{
helpers: true,
regenerator: !isHermes,
version: isVersion ? options.enableBabelRuntime : '7.0.0-beta.0',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid baking in the plugin's default fallback version?

Suggested change
version: isVersion ? options.enableBabelRuntime : '7.0.0-beta.0',
...(isVersion && {version: options.enableBabelRuntime}),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes definitely - I like this mechanism for initializing a specific property only under certain circumstances.

@robhogan
Copy link
Contributor

robhogan commented Mar 7, 2022

Thanks again @newyankeecodeshop - this looks like a useful optimisation

Verified

This commit was signed with the committer’s verified signature.
wagoodman Alex Goodman
@facebook-github-bot
Copy link
Contributor

@rh389 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@leotm
Copy link
Contributor

leotm commented Mar 29, 2022

Just confirming, does this mean we need to update the overload version each time to benefit from latest @babel/runtime e.g.

With the -6% bundle? Just thinking how to automate, might look into a + equivalent or some way to pick it up as a variable if its pos w js easily to pickup from node modules

@newyankeecodeshop
Copy link
Contributor Author

@leotm You don't have to update the version each time you upgrade your babel necessarily. Setting the version to a reasonably newer version, such as 7.14.5, will provide that bundle size reduction (via optimizing interopRequireWildcard).
You can see what helpers the version affects in this file:
https://github.com/babel/babel/blob/main/packages/babel-helpers/src/helpers.ts

@newyankeecodeshop newyankeecodeshop deleted the transform-runtime-version branch March 31, 2022 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants