[javascript] Create-React-App 애플리케이션에서 index.html과 index.js 간의 연결은 어디에 있습니까?

나는 반작용 만들기 앱으로 재생 시작 해요,하지만 난 어떻게 이해할 수없는 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. 연결은 어디입니까? 내가 무엇을 놓치고 있습니까?



답변

내부적으로 Create React App은 html-webpack-plugin 과 함께 Webpack을 사용합니다 .

우리의 구성 은 Webpack 이 “진입 점” 으로 사용 하도록 지정 합니다 . 이것이 첫 번째 모듈이며, 다른 모듈로 이동하여 단일 번들로 컴파일합니다.src/index.js

웹팩이 자산을 컴파일 할 때 단일 (또는 코드 분할을 사용하는 경우 여러 개) 번들을 생성합니다. 모든 플러그인에서 최종 경로를 사용할 수 있습니다. 우리는 HTML에 스크립트를 삽입하기 위해 이러한 플러그인을 사용하고 있습니다.

HTML 파일을 생성하기 위해 html-webpack-plugin 을 활성화했습니다 . 구성에서 템플릿으로 읽어야한다고 지정 했습니다 public/index.html. 또한 inject 옵션 을로 설정 했습니다 true. 이 옵션 을 사용하여 최종 HTML 페이지에 Webpack에서 제공 한 경로를 html-webpack-plugin추가합니다 <script>. 이 마지막 페이지는 build/index.html실행 후 표시되는 페이지이고 실행 시 npm run build제공되는 페이지 입니다./npm start

도움이 되었기를 바랍니다! Create React App의 장점은 실제로 생각할 필요가 없다는 것입니다.


답변