Skip to content

Metro watcher doesn't trigger in time for files created during Babel transpilation #1488

Description

@tjzel

I'm changing the way how the Babel plugin works in react-native-reanimated/react-native-worklets. These changes include creating a new file, that's immediately required in the newly transformed code.

To exemplify, let's say I have a file:

// file.js
function foo() {
  function bar(){};
  bar();
};

I'm transpiling it to:

// file.js
function foo() {
  const bar = require("react-native-worklets/generated/1111111.js").default;
  bar();
}

Creating respective file before manipulating the original AST:

// "react-native-worklets/generated/1111111.js"
export default function bar(){};

Such flow causes Metro to fail to resolve "react-native-worklets/generated/1111111.js" because the watcher triggers too late. From my debugging the flow is as follows:

  1. file.js is picked up by Metro for Babel transpilation.
  2. "react-native-worklets/generated/1111111.js" is created.
  3. AST is manipulated by Worklets Babel plugin.
  4. new AST is traversed by Babel, require("react-native-worklets/generated/1111111.js") is discovered.
  5. Metro attempts to resolve this file using this method. It wasn't present in its filesystem snapshot, so it fails to resolve it.
  6. Metro filesystem watcher kicks in here and adds an entry for "react-native-worklets/generated/1111111.js" using this method.

I understand this might be problematic to fix quickly since the issue spans towards the Watchman itself. I wonder if there's some elegant way to trigger the discovery event earlier so the resolution would succeed.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions