[英]Where's the connection between index.html and index.js in a Create-React-App application?
I'm starting to play with the Create React App, but I can't understand how the index.js
is loaded inside index.html
.我开始玩 Create React App,但我不明白
index.js
是如何加载到index.html
中的。 This is the html code:这是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>
But I can't see anywhere the import of the index.js
.但是我在任何地方都看不到
index.js
的导入。 Where is the connection?连接在哪里? What am I missing?
我错过了什么?
Under the hood, Create React App uses Webpack with html-webpack-plugin .在幕后,Create React App 使用带有html-webpack-plugin 的 Webpack 。
Our configuration specifies that Webpack uses src/index.js
as an “entry point” .我们的配置指定Webpack 使用
src/index.js
作为“入口点” 。 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.当 webpack 编译资产时,它会生成一个(或多个,如果您使用代码拆分)包。 It makes their final paths available to all plugins.
它使所有插件都可以使用它们的最终路径。 We are using one such plugin for injecting scripts into HTML.
我们正在使用一个这样的插件来将脚本注入 HTML。
We have enabled html-webpack-plugin to generate the HTML file.我们已经启用html-webpack-plugin来生成 HTML 文件。 In our configuration, we specified that it should read
public/index.html
as a template.在我们的配置中,我们指定它应该读取
public/index.html
作为模板。 We have also set inject
option to true
.我们还将
inject
选项设置为true
。 With that option, html-webpack-plugin
adds a <script>
with the path provided by Webpack right into the final HTML page.使用该选项,
html-webpack-plugin
将带有 Webpack 提供的路径的<script>
添加到最终的 HTML 页面中。 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
.最后一页是您在运行
npm run build
后在build/index.html
获得的页面,以及在您运行npm start
时从/
获得的页面。
Hope this helps!希望这可以帮助! The beauty of Create React App is you don't actually need to think about it.
Create React App 的美妙之处在于你实际上不需要考虑它。
You are not missing anything, as there is no connection as the technologies are completely unrelated and are not germane to each other.您不会遗漏任何东西,因为没有任何联系,因为这些技术完全不相关并且彼此之间没有密切关系。 Webpack/npm/node/json/next/react/typescript/web3 are something entirely from simply using Apache and HTML.
Webpack/npm/node/json/next/react/typescript/web3 完全来自简单地使用 Apache 和 HTML。 Quite frankly, you're best utilizing KISS and not using those other things that are not only a massive security risk, but not really worth developing with.
坦率地说,您最好使用 KISS,而不是使用那些不仅会带来巨大安全风险而且不值得开发的其他东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.