参考:未用包裹捆绑器定义React

发布于 2025-02-03 18:06:26 字数 1080 浏览 2 评论 0原文

我正在关注此 actrace 可以将作为小部件嵌入到任何网站的单个JS文件,

这是我的package.json.json文件显示构建脚本的文件的编辑版本

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "build:widget": "parcel build src/index.js --no-source-maps -d widget" //This
  }

,因此在运行yarn build:wide> widget ,我希望包裹能将整个应用构建到一个index.js文件中,我可以将其嵌入常规HTML文件中。

构建有效,但现在唯一的问题是我在引用输出index.js文件后,我 react 未定义 react files index indece> react react react。 .html 文件如下所示,

<body>
    <p>Hello World</p>
    <div id="widget-section"></div>
</body>
<link href="../index.css" rel="stylesheet" />
<script src="../index.js"></script> //ReferenceError: React is not defined gets thrown here

是否有更好的工作?

I am following this Article to bundle my react app as a single js file that can be embedded as a widget into any website

Here is a redacted version of my package.json file showcasing the build scripts

"scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject",
    "build:widget": "parcel build src/index.js --no-source-maps -d widget" //This
  }

So, on running yarn build:widget, I want parcel to build the entire App into a single index.js file that I can embed in a regular html file.

The build worked, but the only issue now is that I am getting the error ReferenceError: React is not defined after referencing the output index.js file inside my index.html file as shown below

<body>
    <p>Hello World</p>
    <div id="widget-section"></div>
</body>
<link href="../index.css" rel="stylesheet" />
<script src="../index.js"></script> //ReferenceError: React is not defined gets thrown here

Is there a better work around to this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

本宫微胖 2025-02-10 18:06:26

即使您的JavaScript不明确调用React IE 从“ React”导入React ,如果您使用的是JSX,您仍然需要添加此导入。这是由于您的JSX转板器将JSX插入react.createelement调用。

Even if your JavaScript does not explicitly call React i.e. import React from "react", you still need to add this import if you are using JSX. This is due to your JSX transpiler inlining JSX as React.createElement calls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文