Where#39;s the connection between index.html and index.js in a Create-React-App application?(Create-React-App 应用程序中 index.html 和 index.js 之间的联系在哪里?)
问题描述
I'm starting to play with the Create React App, but I can't understand how the index.js
is loaded inside index.html
. This is the html code:
<!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>
But I can't see anywhere the import of the index.js
. Where is the connection? What am I missing?
Under the hood, Create React App uses Webpack with html-webpack-plugin.
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.
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.
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
.
Hope this helps! The beauty of Create React App is you don’t actually need to think about it.
这篇关于Create-React-App 应用程序中 index.html 和 index.js 之间的联系在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Create-React-App 应用程序中 index.html 和 index.js 之间的联系在哪里?


- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01