facebook / react-native

A framework for building native applications using React
https://reactnative.dev
MIT License
119.55k stars 24.37k forks source link

[0.76 RC] Babel preset is incomplete without `hermes-parser` #46601

Closed jbroma closed 2 months ago

jbroma commented 2 months ago

Description

Problem

JavaScript files using newer Flow syntax (e.g., as type casts) fail to transpile correctly in React Native's Metro preset when hermesParser is disabled. This is because babel-parser no longer fully supports Flow, and Hermes parser is now required for handling the latest Flow features.

Background

  1. babel-parser no longer fully supports newer Flow features due to the Flow plugin being unmaintained: https://github.com/babel/babel/issues/16264.
  2. React Native has enabled hermesParser by default in 0.76.0: https://github.com/facebook/react-native/pull/46318

Proposed Solution

To make the Metro preset self-sufficient, we should consider including babel-plugin-syntax-hermes-parser in the preset. This would ensure that even when used without metro, babel can still transpile newer flow syntax properly.

Steps to reproduce

  1. yarn install
  2. yarn react-native start
  3. Trigger compilation by going to http://localhost:8081/index.bundle?platform=ios in the browser or by running the app

React Native Version

0.76.0-rc.1

Affected Platforms

Build - MacOS, Build - Windows, Build - Linux

Output of npx react-native info

System:
  OS: macOS 14.6.1
  CPU: (10) arm64 Apple M1 Pro
  Memory: 148.48 MB / 32.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 20.13.1
    path: ~/Library/Caches/fnm_multishells/27637_1727087645332/bin/node
  Yarn:
    version: 4.5.0
    path: ~/Library/Caches/fnm_multishells/27637_1727087645332/bin/yarn
  npm:
    version: 10.5.2
    path: ~/Library/Caches/fnm_multishells/27637_1727087645332/bin/npm
  Watchman:
    version: 2024.09.09.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.15.2
    path: /Users/jbroma/.rbenv/shims/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 24.0
      - iOS 18.0
      - macOS 15.0
      - tvOS 18.0
      - visionOS 2.0
      - watchOS 11.0
  Android SDK: Not Found
IDEs:
  Android Studio: 2024.1 AI-241.18034.62.2412.12266719
  Xcode:
    version: 16.0/16A242d
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.12
    path: /Users/jbroma/.jenv/shims/javac
  Ruby:
    version: 2.7.6
    path: /Users/jbroma/.rbenv/shims/ruby
npmPackages:
  "@react-native-community/cli":
    installed: 15.0.0-alpha.2
    wanted: 15.0.0-alpha.2
  react:
    installed: 18.3.1
    wanted: 18.3.1
  react-native:
    installed: 0.76.0-rc.1
    wanted: 0.76.0-rc.1
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: Not found
  newArchEnabled: false

Stacktrace or Logs

error: SyntaxError: /Users/jbroma/Developer/RN76BabelPresetReproducer/node_modules/react-native/Libraries/vendor/emitter/EventEmitter.js: Unexpected token, expected "]" (39:5)

  37 |
  38 | type Registry<TEventToArgsMap: {...}> = {
> 39 |   [K in keyof TEventToArgsMap]: Set<Registration<TEventToArgsMap[K]>>,
     |      ^
  40 | };
  41 |
  42 | /**

Reproducer

https://github.com/jbroma/RN76BabelPresetReproducer

Screenshots and Videos

No response

cipolleschi commented 2 months ago

We just landed a couple of picks to fix that. We are going to release RC.2 between today and tomorrow morning. Is it okay to wait for that?

jbroma commented 2 months ago

We just landed a couple of picks to fix that. We are going to release RC.2 between today and tomorrow morning. Is it okay to wait for that?

absolutely, this just needs to be addressed before stable release of 0.76 👍

cipolleschi commented 2 months ago

can you double check if this is fixed, now that RC2 is out? If yes, can we close this issue? 🙏

jbroma commented 2 months ago

tried the RC.2 today and it's still not fixed, btw I've upgraded the repro to RC.2

jpudysz commented 2 months ago

I can confirm that! I was trying to test out the new debugger, but I'm also seeing flow errors

huntie commented 2 months ago

Looks like Riccardo was referencing these picked commits which made it into RC2 — these affect Jest only.

I think I came across this also being hit by our CI, recently in https://github.com/facebook/react-native/pull/45960 — but I forget my own context 😅. Anyway, will try and repro + land the fix.

huntie commented 2 months ago

Hey all, this is fixed! Will need to be picked into the next 0.76 RC also, tracked here: https://github.com/reactwg/react-native-releases/issues/538

oblador commented 2 months ago

Perhaps unpopular opinion, but isn't it time for react-native core to ship transpiled code? Shipping flow source code prevents use of modern javascript tooling and makes builds slower than it has to.

huntie commented 2 months ago

@oblador Oh I'm absolutely for this, and we're making some progress internally (prioritised behind other things) to be able to correctly build + emit types (including TypeScript!).

At the same time, 1/ this won't be ready soon, 2/ keeping complete type information may be important for Static Hermes. Need to understand, before we leap.

ZainaliSyed commented 1 month ago

I am still getting Error .

NODE VERSION : v18.18.2

"react-native": "0.76.1",
"react": "18.3.1",
rowanarcher commented 1 month ago

I am seeing the same on node 22.7, react-native 0.76.1, react 18.3.1.

I tried adding babel-plugin-syntax-hermes-parser to my babel config, but this did not resolve this

huntie commented 1 month ago

@ZainaliSyed @rowanarcher Can either of you link a repro project?

bogrees commented 1 month ago

I am getting this same error as well when running an app. After the bundle finishes loading, the errors appears.

"react": "18.3.1", "react-native": "0.76.1",

New Architecture enabled or disabled does not seem to affect it. The error does not occur with RN 0.75.3 (which I upgraded from).

bogrees commented 3 weeks ago

@huntie, is there a temp solution that I can try locally? I see your PR hasn't merged yet, but I'm not clear where the change is being made. EDIT: nm, found it.. /node_modules/@react-native/eslint-config --- making the parser changes there did not fix the issue.

Ajithkumar2203 commented 3 weeks ago

Im using "react-native": "^0.76.1", "react": "18.3.1",

Im still getting this error when try to build Can't able to run the app in iOS

ravindraguptacapgemini commented 4 days ago

flow typed file started generating errors with latest version of RN and @react-native/babel-preset

Error while parsing /Users/ravindragupta/Desktop/Projects/mobilev2/native/src/mobileCore/services/syncData/checkConnection.js Line undefined, column undefined: Cannot read properties of undefined (reading 'forEach') parseForESLint from parser /Users/ravindragupta/Desktop/Projects/mobilev2/native/node_modules/@babel/eslint-parser/lib/index.cjs is invalid and will just be ignored

[https://github.com/facebook/react-native/issues/47933]