TL;博士如何使用艾斯建与ReactJS.NET
长版本
ReactJS.NET期望从一个“包”中获得以下信息:
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'Webpack到底做了什么对我来说一直都很不清楚,但是看看ReactJS.NET Webpack教程这里,这是一个设置:
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 };在示例webpack-config 这里中,还有这样的输出设置:
{
[...]
globalObject: 'this',
}在esbuild中镜像它将使用iife和esbuild.globalName = 'this',但是它会引发一个错误:
> (global name):1:0: error: Expected identifier but found "this"
1 │ thisEsbuild非常严厉地认为它是一个绑定器(不是一个transpile和--级联器),那么我如何调整Esbuild,使其将global对象转变成React.NET所期望的?--甚至有可能吗?
谢谢,弗莱明
发布于 2021-11-08 10:47:55
找到了解决办法。
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})`,
},
})https://stackoverflow.com/questions/69753111
复制相似问题