Esbuild with ReactJS.NET?

TL;DR How can I use esbuild with ReactJS.NET?


Long version

ReactJS.NET expects the following from a “bundle”:

React.Exceptions.ReactNotInitialisedException: 'React has not been loaded correctly: missing (React, ReactDOM, ReactDOMServer). Please expose your version of React as global variables named 'React', 'ReactDOM', and 'ReactDOMServer'

What Webpack actually does is always quite unclear to me, but looking at the ReactJS.NET Webpack tutorial here, this is the setup:

import React from 'react';
import ReactDOM from 'react-dom';
import ReactDOMServer from 'react-dom/server';

import RootComponent from './home.jsx';

global.React = React;
global.ReactDOM = ReactDOM;
global.ReactDOMServer = ReactDOMServer;

global.Components = { RootComponent };

Further at the sample webpack-config here, there is this output-setting:

{
  [...]
  globalObject: 'this',
}

Mirroring this in esbuild would be using iife and esbuild.globalName = 'this', but it throws an error:

 > (global name):1:0: error: Expected identifier but found "this"
    1 │ this

Esbuild is quite stern on that it is a bundler (not a transpile-and-concatenator), so how would I tweak Esbuild to have the bundle mutate the global object to what React.NET expects? – And is it even possible?

Thanks, Flemming

Answer

Found a solution.

import { build } from 'esbuild'

const globalName = 'whatever'

build({
    [...]
    format: 'iife',
    globalName,
    footer: {
        // Important! Assigns the raw export of the bundle to whatever `this` is in the given context
        js: `Object.assign(this, ${globalName})`,
    },
})