创建React App index.html和index.js连接 [英] Create React App index.html and index.js connection

查看:170
本文介绍了创建React App index.html和index.js连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始玩Create React App,但我无法理解索引是如何在索引中加载的。code> index.js 。 HTML 。 html代码如下:

I'm starting to playing with the Create React App, but I can't understand how the index.js is loaded inside index.html. The html code is this:

<!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的导入。连接在哪里?我缺少什么?

But I can't see anywhere the import of the index.js. Where is the connection? what I'm missing?

推荐答案

创建React App使用Webpack与 html-webpack-plugin

Under the hood, Create React App uses Webpack with html-webpack-plugin.

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

Our configuration specifies that Webpack uses src/index.js as an "entry point". So that’s the first module it reads, and it follows from it to other modules to compile them into a single bundle.

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

When webpack compiles the assets, it produces a single (or several if you use code splitting) bundles. It makes their final paths available to all plugins. We are using one such plugin for injecting scripts into HTML.

我们启用了 html-webpack-plugin 生成HTML文件。在我们的配置中,我们指定它应该将 public / index.html 作为模板读取。我们还设置注入 选项 true 。使用该选项, html-webpack-plugin 添加一个< script> ,其中包含Webpack提供的路径最终的HTML页面。最后一页是运行 npm run build 之后进入 build / index.html 的页面,以及当你运行 npm start 时,从 / 获得服务。

We have enabled html-webpack-plugin to generate the HTML file. In our configuration, we specified that it should read public/index.html as a template. We have also set inject option to true. With that option, html-webpack-plugin adds a <script> with the path provided by Webpack right into the final HTML page. This final page is the one you get in build/index.html after running npm run build, and the one that gets served from / when you run npm start.

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

Hope this helps! The beauty of Create React App is you don’t actually need to think about it.

这篇关于创建React App index.html和index.js连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆