在 Create-React-App 应用程序中,index.html 和 index.js 之间的连接在哪里?

我开始玩创建反应应用程序,但我不能理解 index.js是如何装载在 index.html内。这是 html 代码:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
<!--
Notice the use of %PUBLIC_URL% in the tag above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.


Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.


You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.


To begin the development, run `npm start`.
To create a production bundle, use `npm run build`.
-->
</body>
</html>

但我看不到任何地方进口的 index.js。在哪里的连接? 我错过了什么?

79535 次浏览

在底层,CreateReact 应用程序使用带有 Html-webpack-plugin的 Webpack。

Webpack 的配置 指定使用 src/index.js作为 “入口点”。所以这是它读取的第一个模块,然后从这个模块到其他模块,将它们编译成一个包。

当 webpack 编译资产时,它会生成一个(如果使用代码分割,则会生成几个)包。它使他们的最终路径可用于所有插件。我们正在使用这样一个插件将脚本注入到 HTML 中。

我们已经启用了 Html-webpack-plugin来生成 HTML 文件。在我们的配置中,我们 inject0,它应该读取 public/index.html作为一个模板。我们还将 injectinject1设置为 true。使用这个选项,html-webpack-plugin将使用 Webpack 提供的路径添加一个 <script>到最终的 HTML 页面。这最后一页是运行 npm run build之后在 build/index.html中得到的页面,以及运行 npm start时从 /中得到的页面。

希望这能有所帮助! 创建反应应用程序的美丽之处在于你实际上不需要考虑它。