Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to understand Webpack

I'm an HTML/CSS/PHP/MYSQL programmer, now trying to learn some javascript. I'm doing that by digging trough the code of the webmail I'm currently using (open source) and trying to understand how it works. I'm trying to understand how the different parts of the page are getting loaded (without the page reload you would get in PHP). If I'm not wrong it's using webpack to do that. Every part of the page is loaded as a module if I'm not wrong.

/******/ (function(modules) { // webpackBootstrap
/******/    // The module cache
/******/    var installedModules = {};
/******/
/******/    // The require function
/******/    function __webpack_require__(moduleId) {
/******/
/******/        // Check if module is in cache
/******/        if(installedModules[moduleId])
/******/            return installedModules[moduleId].exports;
/******/
/******/        // Create a new module (and put it into the cache)
/******/        var module = installedModules[moduleId] = {
/******/            exports: {},
/******/            id: moduleId,
/******/            loaded: false
/******/        };
/******/
/******/        // Execute the module function
/******/        modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/        // Flag the module as loaded
/******/        module.loaded = true;
/******/
/******/        // Return the exports of the module
/******/        return module.exports;
/******/    }
/******/
/******/
/******/    // expose the modules object (__webpack_modules__)
/******/    __webpack_require__.m = modules;
/******/
/******/    // expose the module cache
/******/    __webpack_require__.c = installedModules;
/******/
/******/    // __webpack_public_path__
/******/    __webpack_require__.p = "webmail/v/0.0.0/static/js/";
/******/
/******/    // Load entry module and return exports
/******/    return __webpack_require__(0);
/******/ })

This seems to be (part of) the code that makes that magic possible. Now, later on in the code webpack is used like this:

AbstractSystemDropDownUserView.prototype.settingsClick = function ()
{
    __webpack_require__(/*! Knoin/Knoin */ 5).setHash(Links.settings());
};

If I'm correct, this function loads a module when clicking on the button liked to settingsClick. However, where are the module numbers defined/assigned?

Any help with getting me on my way is greatly appreciated!

like image 919
Sander Avatar asked Feb 27 '15 23:02

Sander


People also ask

What is webpack in simple terms?

Webpack is a free and open-source module bundler for JavaScript. It is made primarily for JavaScript, but it can transform front-end assets such as HTML, CSS, and images if the corresponding loaders are included. Webpack takes modules with dependencies and generates static assets representing those modules.

What does webpack do exactly?

Webpack is an aggressive and powerful module bundler for JavaScript applications. It packages all the modules in your application into one or more bundles (often, just one) and serves it to the browser.

What are four core concepts of webpack?

There are four basic concepts in webpack: entry , output , modules and plug-ins . These configurations are added in webpack.

Do people still use webpack?

Loved by many, hated by some, known to all. And still the most popular bundler in 2021. With more than 15 million weekly downloads (at the time of writing this post), there's no doubt that Webpack is still the bundler par excellence in 2021.


1 Answers

Webpack starts with a primary JS file, and its configured addons (allow for additional resources to be required in), compiles these resources into a merged script, as well as watchers for hot-swap updates when JS or CSS changes. Odds are, if you look at the source code for the project, it will be setup as a number of CommonJS/Node-Style modules that use require or ES6 style module import/export directives.

Some places to get started:

  • WebPack Docs -> Motivation
  • React/WebPack Cookbook

You should also look at:

  • Introduction to NPM - NPM is where most modules that you will likely want to use reside.
  • NodeJS - Server-Side JS environment, most of these tools run via node.
  • Browserify - Webpack is pretty much browserify + more, You may prefer a more direct approach
  • Gulp - Gulp is a stream based build tool, webpack has its' own, but it's worth looking at for additional ideas.
  • BabelJS - formerly 6to5 - lets you use modern JS features in browsers today.
  • ES5 Shims - if you need to support IE8, you'll need these.

Webpack relies on modules, and tools from node/iojs, it's also similar to browserify with extras.

like image 146
Tracker1 Avatar answered Oct 23 '22 14:10

Tracker1