Kush's Blog

Kush's Blog

Intro to Webpack

Intro to Webpack

Webpack is a static module bundler for modern JavaScript applications. When webpack processes your application, it internally builds a dependency graph which maps every module your project needs and generates one or more bundles.

Webpack bundles multiple JS files into one file. It also has modules and plugins you can, well, plugin to the library to do other things such as convert ES6+ code to es5, or transpile LESS to CSS etc.

webpack1.JPG

So, if you would like to write more modular code— code that is broken across multiple files for resuability, readability, and/or maintain ability (i.e. you don’t want to dig through one file and 1000 lines of code, when you could divide your code into multiple files)—then webpack is perfect.

WebPack is not just a module compiler, it is also capable of performing a bunch of operations, say, linting or testing, for which you would generally need other tools. Besides, WebPack supports stylesheets, html, etc. There’s also Hot Module Replacement tool which allows checking your progress without a full reload of your applications.

webpack2.JPG

Webpack has some fundamental concepts Entry , Output , Modules, Loaders and Plugins.

  • Entry : The first JS file in the reference links.
  • Output : The final bundle you want to consume.
  • Modules : Pre-processing logic before bundling process.
  • Loaders : They are transformations that are applied to the source code of a module. They allow you to pre-process files as you import or “load” them.
  • Plugins : Post-processing logic after bundling.

What are the advantages of using Webpack

  • It helps you bundle your resources.
  • It can run Babel transpilation to ES5, allowing you to use the latest JavaScript features without worrying about browser support.
  • It can transpile CoffeeScript to JavaScript
  • It can convert inline images to data URIs. allows you to use require() for CSS files.
  • It can run a development webserver.
  • It can handle hot module replacement.
  • It can split the output files into multiple files, to avoid having a huge js file to load in the first page hit.
  • It can perform tree shaking (Tree shaking means removing “dead code” from the bundle you ship to your users ).

webpack3.JPG

Alternatives of Webpack

Gulp

Gulp is the best alternative for webpack. It is an open-source JavaScript toolkit, used for streaming build systems in front-end web development. It is a task runner which is built on nom and node.js. Mainly used for automation to save time and repeat certain tasks involved in web development like cache busting, linting, concatenation, minification, unit testing, optimization

Parcel

Parcel is another JavaScript tool that can take any type of file as an entry point. It utilizes worker processes to compile your cod in parallel, utilizing modern multicore processors. It boosts the speed for initial builds. It also has a file system cache that saves the compiled results per file, for even subsequent startups.

RequireJS

RequireJS is an JavaScript library and file loader that manages the dependencies between JavaScript files and in modular programming. It improves the speed and the quality of the code. RequireJS can load nested dependencies and provides asynchronous module loading.

Brunch

Brunch is an assembler for HTML5 applications. It is a simple but powerful build process and pipeline. It’s agnostic to frameworks, libraries, programming, stylesheet & templating languages and backend technology.

Esbuild

Esbuild is 10-100x faster than another bundlers. The main goal of the esbuild bundler project is to bring about a new era of build tool performance, and create an easy-to-use modern bundler along the way.

Interested in reading more such articles from Kushagra Sharma?

Support the author by donating an amount of your choice.