Skip to content

fix: babel-plugin-react-compiler cannot be imported when used in a framework#1270

Open
smeng9 wants to merge 3 commits into
vitejs:mainfrom
smeng9:patch-2
Open

fix: babel-plugin-react-compiler cannot be imported when used in a framework#1270
smeng9 wants to merge 3 commits into
vitejs:mainfrom
smeng9:patch-2

Conversation

@smeng9

@smeng9 smeng9 commented Jun 26, 2026

Copy link
Copy Markdown

Description

I am a meta-framework developer that my library incorporates vite's javascript api.
My library is a cli based tool, calls vite's build() function internally, and processes user files.
It is supposed to be able to run regardless of whether there is a local node_module folder in the user's cwd.

I encountered the following issue issue

@rolldown/plugin-babel calls babel.loadOptionsAsync with { babelrc: false, configFile: false }. When there is no config file, Babel uses process.cwd() as the dirname for resolving plugins in programmatic presets — not the directory of the file being transformed.

reactCompilerPreset at @vitejs/plugin-react/dist/index.js:46 returns:
preset: () => ({ plugins: [["babel-plugin-react-compiler", options]] })
// ^ bare string, not a pre-resolved absolute path

Babel must then locate babel-plugin-react-compiler via:
import.meta.resolve("babel-plugin-react-compiler",
pathToFileURL(path.join(process.cwd(), "babel-virtual-resolve-base.js")).href)

When process.cwd() is a directory that doesn't have babel-plugin-react-compiler in its node_modules ancestry — e.g., / (the Docker container default) — the resolution fails.

It needs to ensure Babel receive an absolute path, so it never needs to do CWD-relative resolution

@smeng9 smeng9 changed the title fix babel-plugin-react-compiler cannot be imported when used in a framework fix: babel-plugin-react-compiler cannot be imported when used in a framework Jun 27, 2026
smeng9

This comment was marked as resolved.

@ArnaudBarre

Copy link
Copy Markdown
Member

In your setup, if there is no node_modules, where is located the code for the react compiler plugin?

@smeng9

smeng9 commented Jun 28, 2026

Copy link
Copy Markdown
Author

Hi @ArnaudBarre

My tool is supposed to be installed using npm install -g, it will be installed into npm root -g location.

That means, in a docker node environment, if I run my tool with the cwd as /, it cannot find the babel-plugin-react-compiler from /, but the babel-plugin-react-compiler is indeed installed at the global node_modules.

@ArnaudBarre

Copy link
Copy Markdown
Member

Ok thanks for the clarification I see. @sapphi-red what's your opinion on us doing the resolve of babel plugin instead of Babel? We are doing this for SWC but I looked at the PR dropping Babel and we didn't for the Babel plugins

@sapphi-red

Copy link
Copy Markdown
Member

I think we should have been doing it given that babel-plugin-react-compiler is a peer dep of plugin-react.

Comment thread packages/plugin-react/src/reactCompilerPreset.ts Outdated
Resolve babel-plugin-react-compiler via import.meta.resolve and convert
the file URL to a path with fileURLToPath, dropping the createRequire
boilerplate.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@smeng9 smeng9 requested a review from sapphi-red July 1, 2026 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants