External modules as global variables with esbuild
.
yarn add -D esbuild-plugin-external-global
or
npm i -D esbuild-plugin-external-global
Add to your esbuild plugins list:
const esbuild = require("esbuild");
const { externalGlobalPlugin } = require("esbuild-plugin-external-global");
esbuild.build({
...
plugins: [
externalGlobalPlugin({
'react': 'window.React',
'react-dom': 'window.ReactDOM',
'jQuery': '$',
'fetch-json': 'globalThis'
})
]
...
});