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

Where's the connection between index.html and index.js in a Create-React-App application?

我开始玩 Create React App,但我不明白 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 的导入。连接在哪里?我错过了什么?

在幕后,Create React App 使用带有 html-webpack-plugin 的 Webpack。

我们的配置specifies that Webpack uses src/index.js as an “entry point”。所以这是它读取的第一个模块,然后从它到其他模块,将它们编译成一个包。

当 webpack 编译资产时,它会生成一个(或多个,如果你使用代码拆分)包。它使所有插件都可以使用它们的最终路径。我们正在使用一个这样的插件将脚本注入 HTML.

我们已启用 html-webpack-plugin 生成 HTML 文件。在我们的配置中,我们 指定 它应该读取 public/index.html 作为模板。我们还将 inject 选项 设置为 true。使用该选项,html-webpack-plugin 添加一个带有 Webpack 提供的路径的 <script> 到最终的 HTML 页面。最后一页是您在 运行 宁 npm run build 之后进入 build/index.html 的那一页,以及当您 运行 npm start/ 获得的那一页].

希望对您有所帮助! Create React App 的美妙之处在于您实际上不需要考虑它。