简体   繁体   English

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

[英]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.

相关问题 是否可以将 html 主页转换为 React 组件以在 Index.js Create-React-App 中呈现 - Is it possible to convert html homepage to a react component to render in Index.js Create-React-App 反应入口点:Index.html vs index.js? 节点代码在哪里? - React Entry Point: Index.html vs index.js? Where is the node code? Heroku 中的 React/Node 应用程序,在服务器运行的情况下提供 index.html/index.js - React/Node app in Heroku, serving index.html/index.js with server running 更改/添加 index.js 的 create-react-app 入口点或位置 - Change/add create-react-app entry point or location of index.js 为什么 create-react-app 别名无法从文件夹中找到 index.js? - Why create-react-app alias is not able to find index.js from folder? 想确认对使用 create-react-app 导入 index.js 的默认行为的理解 - Would like to confirm understanding of default behaviour of importing index.js with create-react-app 我可以在没有 npm 启动和 npx create-react-app 的情况下使用 React 运行 index.html 吗? - Can I run index.html with React without npm start and npx create-react-app? React-snapshot 使用 create-react-app 和 serve 在所有路由上预渲染 index.html - React-snapshot pre-renders index.html on all routes with create-react-app and serve 如何将 create-react-app 与提供 index.html 的后端一起使用? - How to use create-react-app with a backend which serves index.html? reactjs create-react-app在src文件夹中缺少index.html - reactjs create-react-app missing index.html in src folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM