Code Monkey home page Code Monkey logo

how-webpack-works-client-side's Introduction

This repository contains an annotated /ref/bundle.js file that explains a minimal Webpack client-side bundle file. The /build/bundle.js is the original webpack generated file.

First, on Line 90 - the last IIFE, it inserts the Module metadata for webpack itself.

Then, it runs __webpack_require__(1), where 1 is the module ID in the __webpack_modules__ array on the top (which contains the instructions to create a module export object for a module).

A module export object includes the metadata for the module, as well as a default property that includes a function to find the entry function for that module () => __WEBPACK_DEFAULT_EXPORT__

__webpack_require__ returns a module export object, which it either fetches from its module cache, or creates from scratch using the instruction from __webpack_modules__ array.

If it chooses to create from scratch (which it does on load), it will first start from an empty export object, then pass it to the function inside __webpack_modules__ where it adds Module metadata, then adds a property default to the exports object, as well as set its getter to () => __WEBPACK_DEFAULT_EXPORT__.

Inside the array's function, it executes the rest of the bundled code and sets __WEBPACK_DEFAULT_EXPORT__ to the bundle's entry function.

After the code in the array finishes running, it returns control back to __webpack_require__, where it returns the fully formed export object to the last IIFE.

On line 100, it then executes the default function (with Closure of the array's environment), and with this set to undefined (in strict mode).

This is how Webpack executes bundled code client-side.


Note: The reason why Webpack prefixes all its internal functions with __ is because as they are in the same Closure as bundled code, bundled code can actually reference Webpack internal variables. The __ provides obscurity, preventing accidental access from user-supplied code.

how-webpack-works-client-side's People

Contributors

leohku avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.