Create React App index.html and index.js

I am starting to play with the Create React application, but I do not understand how index.js loaded inside index.html . 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 do not see index.js import index.js . Where is the connection? What am I missing?

+7
javascript reactjs
source share
1 answer

Under the hood, the Create React application uses Webpack with the html-webpack-plugin .

Our configuration indicates that Webpack uses src/index.js as the "entry clause" . Thus, this is the first module that it reads, and it follows other modules from it in order to compile them into one package.

When webpack compiles assets, it creates single ones (or several if you use code splitting). This makes their final paths accessible to all plugins. We use one such plugin to enter scripts in HTML.

We created html-webpack-plugin to create the HTML file. In our configuration, we indicated that it should read public/index.html as a template. We also set the inject parameter to true . With this option, html-webpack-plugin adds <script> using the path provided by Webpack directly to the final HTML page. This final page is the one you got in build/index.html after starting npm run build , and the one that gets from / when starting npm start .

Hope this helps! The beauty of the Create React app - you really don't need to think about it.

+14
source share

All Articles