Esbuild with ReactJS.NET? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Esbuild with ReactJS.NET? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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


Found a solution.

import { build } from 'esbuild'

const globalName = 'whatever'

    format: 'iife',
    footer: {
        // Important! Assigns the raw export of the bundle to whatever `this` is in the given context
        js: `Object.assign(this, ${globalName})`,
We are here to answer your question about Esbuild with ReactJS.NET? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji