relay icon indicating copy to clipboard operation
relay copied to clipboard

`babel-plugin-relay` bug: `eagerEsModules: true` only works with single-project Relay configs.

Open tomfuhrer opened this issue 10 months ago • 3 comments

Summary of the bug

When "eagerEsModules": true is set in a multi-project Relay config, babel-plugin-relay transforms graphql tagged templates into require calls, like this:

var _MeQuery;
var MeQuery =
  _MeQuery !== void 0
    ? _MeQuery
    : ((_MeQuery = require("./__generated__/MeQuery.graphql")),
      _MeQuery.hash &&
        _MeQuery.hash !== "bce9f1cc4614c3f306814d78c53b009b" &&
        console.error(
          "The definition of 'MeQuery' appears to have changed. Run `relay-compiler` to update the generated files to receive the expected data."
        ),
      _MeQuery);

Expected behavior

Just like it does with a single-project Relay config, babel-plugin-relay should transform graphql tagged templates into import declarations when "eagerEsModules": true is set, like this:

import _MeQuery from "../__generated__/MeQuery.graphql";
var MeQuery =
  (_MeQuery.hash &&
    _MeQuery.hash !== "bce9f1cc4614c3f306814d78c53b009b" &&
    console.error(
      "The definition of 'MeQuery' appears to have changed. Run `relay-compiler` to update the generated files to receive the expected data."
    ),
  _MeQuery);

Reproducing the bug

I created a reduced test case to demonstrate this behavior. If you want to reproduce the bug yourself, you can clone the repo, run the build commands, and compare the output. The repo also includes the dist files (one each for single and multi-project configs), so if you want to take my word for It, you can just compare the outputs on GitHub. More detail in the README.

tomfuhrer avatar Feb 10 '25 03:02 tomfuhrer

Thanks for catching this! Do you want to work on PR to fix?

captbaritone avatar Feb 11 '25 00:02 captbaritone

I mean, yeah. It will take me a minute, though, since I'm enough of a beginner to have lots of stupid questions still. But I've been working on it, and I can open a PR once I figure it out.

tomfuhrer avatar Feb 11 '25 13:02 tomfuhrer

I think I got it. I opened #4934.

tomfuhrer avatar Mar 06 '25 15:03 tomfuhrer

I'm looking forward to this feature

livwvil avatar Jun 18 '25 21:06 livwvil