Getting esmExternalRequirePlugin to work with outputOptions.global in Vite 8 #22068
Replies: 3 comments 3 replies
-
|
This looks like The fix is usually to explicitly list your modules in build: {
rollupOptions: {
external: ['react', 'react-dom'],
output: {
globals: {
react: 'window.React',
'react-dom': 'window.ReactDOM'
}
}
}
}I believe the plugin relies on the static ` |
Beta Was this translation helpful? Give feedback.
-
|
Building on what @Nickalus12 said — the core idea is right, but one important detail: since you're on Vite 8, build.rollupOptions is deprecated and renamed to build.rolldownOptions. The compat layer usually handles it, but in edge cases like this where hook timing matters, using the new option name directly can make a difference: export default defineConfig({ Also externalize react/jsx-runtime — Vite 8 ships @vitejs/plugin-react v6 which uses Oxc instead of Babel, and it can inject react/jsx-runtime imports that bypass your globals map. If it's still stubborn after that, you can brute-force it with output.intro to inject the assignments at the top of the bundle: |
Beta Was this translation helpful? Give feedback.
-
|
Vite 8 replaced Rollup with Rolldown, and one of the breaking changes is that The fix: add import { defineConfig, esmExternalRequirePlugin } from 'vite'
export default defineConfig({
plugins: [
esmExternalRequirePlugin({
external: ['react', 'react-dom', 'react/jsx-runtime'],
}),
],
build: {
rolldownOptions: {
external: ['react', 'react-dom', 'react/jsx-runtime'],
output: { globals: { react: 'React', 'react-dom': 'ReactDOM' } },
},
},
})Also: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I've got a Vite plugin that currently uses the
outputOptionshook to set aglobalsresolver function.However, since upgrading from Vite 7 to Vite 8, it's leaving a
require('react')in my builds.The migration guide (https://vite.dev/guide/migration#require-calls-for-externalized-modules) says to use the
esmExternalRequirePluginplugin to keep the previous behaviour, but it's not working withreact.At the moment, the
globalsresolve in my plugin is mapping the following for React:Any ideas as to how to keep the previous behaviour in my case?
Beta Was this translation helpful? Give feedback.
All reactions