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

feat(babel): rewrite babel preset for web #24725

Closed
wants to merge 14 commits into from

Conversation

EvanBacon
Copy link
Contributor

@EvanBacon EvanBacon commented Oct 4, 2023

Why

We've used metro-react-native-babel-preset for web to get the closest behavior between platforms, but this doesn't make the most sense because the target JS engines between web, native, and server are pretty different. This PR aims to improve the correctness per-platform, by introducing an entirely new preset for web/server which uses more mainstream settings. The aim is keep things mostly working the same, but without the spaghetti code in metro-react-native-babel-preset.

How

  • Add new web-only preset with @babel/preset-env, react, and typescript.
  • The new web-only preset uses a custom flow preset to support enums.
  • plugin-transform-runtime, babel-plugin-react-native-web, @babel/plugin-syntax-export-default-from are also added on web.
  • The lazy option isn't currently supported, unclear if we want to add this since web should be using esm more often than not.
  • This PR also introduces support for browserslist by using preset-env.

Test Plan

  • Existing tests should pass.
  • We may want to add more tests, but I'm not sure yet.

EvanBacon and others added 2 commits October 3, 2023 17:36
Co-authored-by: Expo Bot <34669131+expo-bot@users.noreply.github.com>
@expo-bot expo-bot added bot: passed checks ExpoBot has nothing to complain about and removed bot: suggestions ExpoBot has some suggestions labels Oct 4, 2023
try {
return !!require.resolve(name);
} catch (error: any) {
if (error.code === 'MODULE_NOT_FOUND' && error.message.includes(name)) {
Copy link
Member

Choose a reason for hiding this comment

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

Better to make this check more precise:

error.message.includes(`'${name}'`)

Don't want false positives.

packages/babel-preset-expo/src/common.ts Outdated Show resolved Hide resolved
packages/babel-preset-expo/src/common.ts Outdated Show resolved Hide resolved
packages/babel-preset-expo/src/index.ts Outdated Show resolved Hide resolved
packages/babel-preset-expo/src/index.ts Show resolved Hide resolved
packages/babel-preset-expo/src/web.ts Show resolved Hide resolved
require('@babel/preset-env'),
{
modules: platformOptions.disableImportExportTransform ? false : 'commonjs',
exclude: ['transform-typeof-symbol'],
Copy link
Member

Choose a reason for hiding this comment

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

Why is this excluded? FWIW my best guess is that it adds a bunch of extra bytes for a somewhat obscure feature - indicator that this may need a comment

packages/babel-preset-expo/src/web.ts Outdated Show resolved Hide resolved
packages/babel-preset-expo/src/web.ts Outdated Show resolved Hide resolved
packages/babel-preset-expo/src/web.ts Outdated Show resolved Hide resolved
EvanBacon added a commit that referenced this pull request Oct 31, 2023
…tion (#25125)

# Why

As reported by @gaearon, the React JSX transform should use a
dev-specific import when bundling for development. This dev-specific
import will add additional error reporting props to React components
like ` { fileName: _jsxFileName, lineNumber: 4, columnNumber: 12 }` when
bundling in development.

## Update

After more digging, I found that we were intentionally not adding the
additional dev plugins because they were rolled in to
`@babel/preset-react` and should be used directly. Since we already
disable nearly all of the React transforms in the Metro plugin, it seems
logical to just use `@babel/preset-react` directly and provide
standardized behavior.

We may want to consider adding an option like `preset-react` (used in
Next.js) in the future to allow users to fully customize the React
options. For now, they can only modify the `jsxImportSource` and
`jsxRuntime`.

<!--
Please describe the motivation for this PR, and link to relevant GitHub
issues, forums posts, or feature requests.
-->

# How

- Pull in the `isDev` caller property from
#24725 to ensure we can explicitly
target development without having to rely on the environment variables.
- ~~Add `@babel/plugin-transform-react-jsx-development` as a dependency
of `babel-preset-expo`. This follows the Babel preset for React.~~
- ~~Use `@babel/plugin-transform-react-jsx-development` instead of
`@babel/plugin-transform-react-jsx` when bundling for development. We do
this here because the upstream is always disabled to support how expo
uses the automatic JSX import by default.~~
- Add `@babel/preset-react` for all React transformations.
- Drop unused `native.withDevTools` and `web.withDevTools` options.
These never did anything anyways.
- Remove support for `native.useTransformReactJSXExperimental` and
`web.useTransformReactJSXExperimental` option in favor of `jsxRuntime:
'classic'`. React support can no longer be removed. An error will be
thrown if the removed option is used.

<!--
How did you build this feature or fix this bug and why?
-->

# Test Plan

- Split `jsxImport` tests to a new file and add additional testing for
dev/prod cases.


<!--
Please describe how you tested this change and how a reviewer could
reproduce your test, especially if this PR does not include automated
tests! If possible, please also provide terminal output and/or
screenshots demonstrating your test/reproduction.
-->

# Checklist

<!--
Please check the appropriate items below if they apply to your diff.
This is required for changes to Expo modules.
-->

- [ ] Documentation is up to date to reflect these changes (eg:
https://docs.expo.dev and README.md).
- [ ] Conforms with the [Documentation Writing Style
Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md)
- [ ] This diff will work correctly for `npx expo prebuild` & EAS Build
(eg: updated a module plugin).

---------

Co-authored-by: Expo Bot <34669131+expo-bot@users.noreply.github.com>
@EvanBacon
Copy link
Contributor Author

Closing in favor of the simpler PR #27907

@EvanBacon EvanBacon closed this Mar 28, 2024
@EvanBacon EvanBacon deleted the @evanbacon/babel/rewrite-for-web branch March 28, 2024 06:27
EvanBacon added a commit that referenced this pull request Apr 3, 2024
# Why

- Alternative to #24725 which takes a
less radical approach.
- Instead of reimplementing all the functionality, we'll simply remove
the unused babel transforms from the existing preset.

<!--
Please describe the motivation for this PR, and link to relevant GitHub
issues, forums posts, or feature requests.
-->

# Test Plan

- Hopefully all language tests catch any changes.
- This only applies to web and ssr.

---------

Co-authored-by: Expo Bot <34669131+expo-bot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: fingerprint changed bot: passed checks ExpoBot has nothing to complain about
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants