Skip to content

[babel-plugin] use globalThis in the compile-time evaluator - #1844

Closed
henryqdineen wants to merge 2 commits into
facebook:mainfrom
henryqdineen:hqd-fix-playground-global-not-defined
Closed

[babel-plugin] use globalThis in the compile-time evaluator#1844
henryqdineen wants to merge 2 commits into
facebook:mainfrom
henryqdineen:hqd-fix-playground-global-not-defined

Conversation

@henryqdineen

@henryqdineen henryqdineen commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

What changed / motivation ?

This only affects the playground. It's a bug in the browser build of the plugin (lib/index.browser.js), which the playground is the only known consumer of. Node builds were never affected, since global exists there.

The compile-time evaluator read globals off Node's global object in three places in src/utils/evaluate-path.js. global doesn't exist in browsers, so those reads throw ReferenceError: global is not defined when the plugin runs client-side, as it does in the docs playground. Babel prefixes the filename, giving the reported /App.tsx: global is not defined.

Swapped all three to globalThis, which is equivalent in Node and available in browsers.

It broke now because of the read added in #1818: isBlockedFunction compares against global.eval and runs for every call expression, which is why removing the stylex.when(...) call from the example works around it. The other two reads are older and equally broken in a browser, but sit behind the isValidCallee allowlist (String, Number, Math, Object, Array) that no playground example hits.

Linked PR/Issues

Fixes #1838

Additional Context

globalThis behaves identically to global in Node, so this is strictly a widening. No behavior change for any Node consumer of the plugin.

Pre-flight checklist

🤖 Generated with Claude Code

`global` only exists in Node, so reading globals off it throws a
ReferenceError when the plugin runs in a browser (the docs playground).
`globalThis` is equivalent in Node and works everywhere.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 3, 2026
@vercel

vercel Bot commented Sep 3, 2026

Copy link
Copy Markdown

@henryqdineen is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@mellyeliu

Copy link
Copy Markdown
Member

can we fix the flow types?

Flow types `global` as `any`, so these three lines were never actually
typechecked. `globalThis` is a real namespace type, which surfaced two
things it can't express: indexing with an arbitrary string, and `eval`,
which is absent from the namespace even though the bare binding is fine.

Cast at each read to keep the escape hatch local to the three sites.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@henryqdineen
henryqdineen force-pushed the hqd-fix-playground-global-not-defined branch from 1016a30 to cc26f99 Compare September 4, 2026 03:58
@henryqdineen

Copy link
Copy Markdown
Collaborator Author

can we fix the flow types?

Thanks. I always forget about Flow. Should be fixed in cc26f99. I considered just having a transform from global -> globalThis as part of the rollup of the browser bundle but it seems like globalThis is generally preferred for NodeJS code.

@henryqdineen

Copy link
Copy Markdown
Collaborator Author

This was fixed by #1847

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Playground is broken

2 participants