javascript - 用 ReactJS.NET 构建 Esbuild?
问题描述
TL;DR我如何将esbuild与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
and esbuild.globalName = 'this'
,但它会引发错误:
> (global name):1:0: error: Expected identifier but found "this"
1 │ this
Esbuild 非常严厉,因为它是一个捆绑器(而不是转译和连接器),那么我将如何调整 Esbuild 以使捆绑包将global
对象更改为 React.NET 所期望的?- 甚至有可能吗?
谢谢,弗莱明
解决方案
找到了解决方案。
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})`,
},
})
推荐阅读
- react-native - 如何在 React-Native 中将屏幕从 App.js 导航到第一个屏幕
- android - 片段中的 OnCreateView 仅被第一次调用,不再被调用
- html - HTML / CSS 标头问题?
- python - 我想向你们学习如何修改我的代码
- dll - 未知的 DLL 文件 Windows 8.1 64 位
- r - 如何将密度图中的标签更改为侧面的图例?
- azure - Azure 数据工厂 - 迭代多个数据集选项
- reactjs - AwaitFor 不等待回调被调用
- sql - 在访问查询中将组合框值作为比较运算符传递?
- reactjs - react-select 异步请求方法