参考:未用包裹捆绑器定义React
我正在关注此 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即使您的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 asReact.createElement
calls.