I would like to understand how browsers handle JavaScript once bundled with Webpack for example. In my understanding of the thing, is transmitted to the browser JavaScript code understandable by the browser, after transpilation of the React code with Babel and bundle of the different parts of an application, which means that there is no more code related to the React library right? And yet it is still possible to benefit from the functionalities for which React is used, but then where does React sit, how does the exchange with the browser happen? Is the React library somehow included in the bundled code?
I can imagine a scenario like this:
edit:
import {useState} from 'react';
const [state, setState] = useState();
I just tested this code and on Babel'site and it gives:
"use strict";
var _react = require("react");
const [state, setState] = (0, _react.useState)();
Does this mean that React is included in the bundle? If yes, how are the imports done by the browser (I've heard about module loaders but I have no idea if that's what it is, a search on the web doesn't give me any result)?
Thanks in advance for your answers
In short answer, Webpack does everything for you. It does all the dependency check to make sure what are needed and should go to the bundle. (It also makes sure the unused libraries won't go into the bundle, even your own modules if they are not referenced) It bundles you own code and all the necessary libraries, including React, you used into one optimizely minimized file for you. Your app load that file in the index.html so the browser can execute the code that you desire.
When you run you react app in you local env, in the home page, ie: http://localhost:3000/, click on view-source, you will see this line
<script defer src="/static/js/bundle.js"></script>
that's where your code and the libraries are.
If you run npm run build, it will generate a even more optimized build for production env in the build folder. The structure of the index.html will be different than local env as well!