Luciano Mammino 𝕏 Node.js Design Patterns Profile picture
Aug 4, 2020 β€’ 16 tweets β€’ 7 min read β€’ Read on X
I spoke about how #JavaScript module bundlers (those things like #Webpack) work under the hood yesterday at @coderful!

πŸ—’Here's a short-ish summary:

1/
TLDR: Module bundlers like Webpack are not "complicated" or "magic" per se, the practice of module bundling is a complicated matter and you need to understand it before you can fully understand what a module bundler does and how to configure it correctly! πŸ¦„

2/
Why do we need module bundlers? Several reasons:

- Today we rely a lot on third-party software (e.g. modules from #npm)
- There are many different standards for JavaScript modules
- We want to write code in a way that is convenient and just "compile" it for the browser

3/
IIFE (Immediately Invoked Function Expression) is at the heart of many JavaScript module systems and module bundlers. To truly understand module bundlers, make sure you are comfortable with this concept

4/ Image
Again, there are many module formats in JavaScript. Just to name a few:

- Global variables
- CommonJS (Node.js)
- AMD (Require.js / Dojo)
- UMD
- ES2015 Modules (ESM)
- Many others (SystemJS, ...)

5/
A module bundler takes your code (generally written using Common.js or ESM) and converts it into one or more "static" assets that can be fully understood and executed in the browser environment.

6/
In order to do that, A module bundler generally performs 2 steps:

1. Dependency Resolution
2. Packing

7/
In the dependency resolution step, the bundler analyses the source code starting from your main file (the "entrypoint") and walks through all the require/import statements to build a representation of the application dependency graph

8/ Image
Internally the dependency graph can be represented using a Map containing the normalized module path as key and a factory function to bootstrap the module as value. This data structure is generally called "Modules map"

9/ ImageImageImageImage
The packing operation (second step) takes the modules map as input and produces one or more JavaScript files that can be understood and executed by the browser.

10/ Image
This step is the "magic" πŸ§™β€β™‚οΈ behind module bundlers!

The gist of it is that all the application code is already available in the modules map. We just need to wrap it in such a way that we can bootstrap all the modules in the right order.

11/ Image
The first thing you should notice is that we are using an IIFE statement to pass the modules map into our "packed" application scope.

12/ Image
Then we define a custom `require` function that allows us to require any module available in the modules map.

13/ Image
Our custom `require` function does a few simple things:

1. It creates an empty `module` object that will be populated by the actual module factory function
2. Then the module factory function is invoked

πŸ’β€β™€οΈThis is an application of the service locator pattern

14/ ImageImageImageImage
Finally, the entire application is bootstrapped by requiring the entrypoint module, which in turn will require and initialize all the submodules in the right order (recursively).

✨ THIS IS THE MAGIC OF MODULE BUNDLERS! ✨

15/
If you want to check out the slides (which contain a lot of material including a super quick intro to Webpack)...

HERE ➑️ loige.link/bundle-coderful

Video (in Italian) is available here: or an older version (in English) here: vimeo.com/311519217

FINE/

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Luciano Mammino 𝕏 Node.js Design Patterns

Luciano Mammino 𝕏 Node.js Design Patterns Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @loige

Sep 15, 2022
Here are 6 of my favorite third-party libraries when building CLI applications using #NodeJS

Want to know why?πŸ‘‡πŸ§΅
commander ➑️ npm.im/commander

Simple yet powerful command-line argument parser. It automatically generates help messages and can be configured to support multiple commands.

I ❀️ it bc it's not prescriptive, you can organize the code as you want (1 file or more).
conf ➑️ npm.im/conf

How many times did you have to store settings (creds & other preferences)? Where do you save the conf file? Which format do you use? How do you load and update the file?

Conf takes care of all of this (and more!) with an extremely simple API
Read 10 tweets
Aug 3, 2022
"Yo, why are #JavaScript and its ecosystem so messy?!" 😑

Well, I am glad you asked... Let me tell you a story! πŸ€“

πŸ§΅πŸ‘‡
For starting... #JavaScript was not designed to be the language that it is today!

JS was created in 1995 by @BrendanEich for Netscape, a web browser that was trying to come up with a language to make the web more interactive
@BrendanEich #JS wasn't related w/ #Java, so why did they call it Java-Script?! Duh! 😳

Java was trendy! it was possible to build interactive sites by embedding Java apps in pages (applets). So it was mostly a #mktg move: "JS: the lightweight Java alternative" or something like that I guess
Read 40 tweets
May 25, 2021
I have been confused for a while on what's the best way to implement a "to string" functionality for a #Rust struct. πŸ¦€

The reason why this has been confusing to me is that there are indeed many ways to do that and they all have different purposes!

This is what I learned

πŸ‘‡πŸ§΅
The first thing that you can do is to just implement a simple `to_string()` function directly on your struct!

play.rust-lang.org/?version=stabl…
This is easy and it works! But the implementation is very specific to our struct.

The rest of the codebase doesn't really know that this type can be converted to a String and therefore you cannot build abstractions on top of this... 🀨
Read 23 tweets
Oct 1, 2020
Do you know what's in a #JWT token?

Here's a quick thread to learn something about this! πŸ‘‡
A JWT token (JSON Web Token) is just a string with a well-defined format. A sample token might look like this:

```
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJoZWxsbyI6ImZyb20gSldUIn0.XoByFQCJvii_iOTO4xlz23zXmb4yuzC3gqrWNt3EHrg
```
There are 3 parts separated by a `.` (dot) character:

- header: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9
- body: eyJoZWxsbyI6ImZyb20gSldUIn0
- signature: XoByFQCJvii_iOTO4xlz23zXmb4yuzC3gqrWNt3EHrg
Read 13 tweets
Sep 17, 2020
@eleven_ty micro tip: Give eleventy some credit by injecting a "generator" meta tag in your HTML! πŸ”₯

Frontpage and Dreamweaver used to get credit this way, why shouldn't we give Eleventy some credit too? πŸ˜‡

(a short thread in 3 steps πŸ‘‡) Image
STEP 1. Create a data file in `_data/site.js` with the following content: Image
STEP 2. Create your base layout `_includes/layout.njk` and use the variable `{{ site.generator }}`: Image
Read 8 tweets
Aug 18, 2020
✨ Rob Pike's 5 Rules of #programming ✨

#golang #simplicity

πŸ§΅πŸ‘‡
1️⃣ You can't tell where a program is going to spend its time. Bottlenecks occur in surprising places, so don't try to second guess and put in a speed hack until you've proven that's where the bottleneck is.
2️⃣ Measure. Don't tune for speed until you've measured, and even then don't unless one part of the code overwhelms the rest.
Read 7 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(