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
Conversation
Co-authored-by: Expo Bot <34669131+expo-bot@users.noreply.github.com>
try { | ||
return !!require.resolve(name); | ||
} catch (error: any) { | ||
if (error.code === 'MODULE_NOT_FOUND' && error.message.includes(name)) { |
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.
Better to make this check more precise:
error.message.includes(`'${name}'`)
Don't want false positives.
require('@babel/preset-env'), | ||
{ | ||
modules: platformOptions.disableImportExportTransform ? false : 'commonjs', | ||
exclude: ['transform-typeof-symbol'], |
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.
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
…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>
Closing in favor of the simpler PR #27907 |
# 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>
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 inmetro-react-native-babel-preset
.How
@babel/preset-env
,react
, andtypescript
.plugin-transform-runtime
,babel-plugin-react-native-web
,@babel/plugin-syntax-export-default-from
are also added on web.browserslist
by usingpreset-env
.Test Plan