我开始玩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
。连接在哪里?我想念什么?
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 setinject
option totrue
. 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 inbuild/index.html
after runningnpm run build
, and the one that gets served from/
when you runnpm start
.Hope this helps! The beauty of Create React App is you don’t actually need to think about it.