fix: babel-plugin-react-compiler cannot be imported when used in a framework#1270
fix: babel-plugin-react-compiler cannot be imported when used in a framework#1270smeng9 wants to merge 3 commits into
Conversation
|
In your setup, if there is no node_modules, where is located the code for the react compiler plugin? |
|
Hi @ArnaudBarre My tool is supposed to be installed using 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. |
|
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 |
|
I think we should have been doing it given that |
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>
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