I'm Riyad Youssef. I'm the client architect at Dooda. And today, we'll talk a little bit about bundled world above and beyond. So how do we manage, how do we handle bundled JavaScript code? The first question you probably ask yourself is why? Why do we need to bundle at all? I have my code, I have it in the files, why do I need to bundle? And for that, we have to go a little bit back in history, a little bit back to maybe the dinosaur's age, where our code bases were unmaintained. And our files looked like that. We had huge files, a lot of lines. And if we did want to break into small files, we had to put a lot of script tags inside a document, and it was really tough to maintain. It was really tough to understand what's happening there.
So today, we want to write some sort of modular code. We have modules in the browser, but it's not fully ready yet, so we just write modules in our source code, right? We have a code, a file, let's say app.jsx, and it has dependencies. So it has app-reference dependency, top, bottom, and button. Maybe they have interdependencies between themselves, and maybe those dependencies have their own dependencies, and they connect between themselves. So how do we handle that? How do we handle all of these dependency chain, all of this dependency tree? We have Webpack. Webpack is today the standard way, and with an asterisk, because there are a lot of similar tools that make it a little bit better, but Webpack is the de facto standard for bundling modules. So we're talking a little bit about Webpack today, and we're talking a bit about how to use it. So just a little bit, a primer on Webpack, Webpack creates a single file, a single bundle, containing the entire code, and it does it, if we don't tell it, we just tell it, okay, we point it to the main entry, to the index, to the main file, and we tell it where to output.
Webpack works in this way. It goes to the entry, it tries to understand from the entry what are the dependencies, and it parses them, and recursively it builds the entire dependency tree and maybe Lodash has its own dependency, and then it just collects all of the dependencies, understands the dependencies, and bundles them. Bundles them in a nice package Bundle.js. It basically serializes the dependency with a little bit of magic from the top, so it serializes them to a single file, Bundle.js, and we can then take this Bundle.js file and use it in our HTML. We can put it in a script tag, because that's something that browsers know how to handle. But we have a problem, because Webpack, unless called otherwise, it will bundle everything to one file. So you can see here a dependency graph. It's an actual dependency graph of a medium-size application, and if you bundle all of this together, you get 15 megabytes of Bundle. So that's enough to get everyone desperate. But in Webpack, we can do code splits. So we can define for Webpack. We have split points in the code, so we don't want to bundle everything into one file, but we want, from these points, to bundle to this file, and then have a dynamic chunk. So split my code and include it only when I need it. So this is the easiest way to do that.
Comments