The Four Core Concepts of webpack (2024)

Patricia Nicole Opetina

Posted on

The Four Core Concepts of webpack (3) The Four Core Concepts of webpack (4) The Four Core Concepts of webpack (5) The Four Core Concepts of webpack (6) The Four Core Concepts of webpack (7)

#webpack #webdev #codenewbie #javascript

webpack is a famous static module bundler. Module bundlers are used to bundle Javascript modules into a single file, which can then be executed by the browser. They help manage the dependencies in your code and load assets following the dependency order.

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

These configurations are added in webpack.config.js of a project.

1. entry

entry and output are related to each other.

webpack.config.js

module.exports = { entry: './path/to/my/entry/file.js',};

The snippet above is an example of an entry configuration. You are basically telling webpack the very first file it needs to look at when it starts creating the dependency graph. The dependency graph starts from this entry file and then builds its dependencies and the dependencies of its dependencies and so on. webpack goes through all of these dependencies and creates modules then repeats this entire process all over the entire application.

2. output

The output configuration tells webpack how and where it should put the bundles and its format. For instance with the output value below, you are telling webpack to put the bundles in a javascript file named my-first-webpack.bundle.js, in a dist folder under the same directory to where the webpack.config.js is located.

webpack.config.js

const path = require('path');module.exports = { entry: './path/to/my/entry/file.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'my-first-webpack.bundle.js', },};

3. rules and loaders

The rules and loaders config instruct webpack how it should process different file types and convert them into valid modules before they are added in the dependency graph.

loaders usually have two properties, namely:

  1. test . The test property tells the type of file that will be processed.
  2. use . The use property tells webpack what loader will be used in processing the file.

For example, building from the config written earlier, the modules property below is telling webpack this:

"Hey webpack compiler, when you come across a path that resolves to a .css file inside of a require()/import statement, use the css-loader to transform it before you add it to the bundle."

webpack.config.js

const path = require('path');const ExamplePlugin = require('ExamplePlugin.js')module.exports = { entry: './path/to/my/entry/file.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'my-first-webpack.bundle.js', }, module: { rules: [ { test: /\.css$/, use: 'css-loader' } ], }, plugins: [ new ExamplePlugin() ]};

4. plugins

Plugins allow you to perform any kind of functionality. While loaders are used to transform certain types of modules, plugins can be leveraged to perform a wider range of tasks like bundle optimization, asset management, injection of environment variables, minifying files, etc. For instance, the example plugin below will print the message "Hello I am learning" everytime you run webpack. Webpack already has a rich collection of plugins, so developers can also check them out before reinventing the wheel.

class ExamplePlugin { apply(compiler) { compiler.plugin("run", (compiler, callback) { console.log("Hello I am learning"); callback(); }); }}

I am still in the process of learning webpack but I believe that simply understanding these concepts will help in confidently creating webpack configurations fitting the developer's needs.

REFERENCES
[1] Webpack Official Documentation
[2] Webpack Academy

Top comments (4)

Subscribe

Nasratullah Talash

Nasratullah Talash

Sophom*ore CS student and a newbie programmer/webdev.

  • Location

    Kabul, Afghanistan

  • Work

    Student

  • Joined

Jun 7 '21

  • Copy link

Nice and simple explanation of the Webpack basics ♥
Can you share with us what resources you are using for learning Webpack?

Patricia Nicole Opetina

Patricia Nicole Opetina

A DEV fairy hehe

  • Location

    Cebu, Philippines

  • Work

    Junior Software Developer

  • Joined

Jun 8 '21

  • Copy link

Hi there, i used the official webpack documentation and this free course as references. Happy learning too!! :)

Arvind Padmanabhan

Arvind Padmanabhan

Developer. Trainer. Social entrepreneur.

  • Joined

Jun 8 '21

  • Copy link

Nice one. Consider writing an article on Webpack at devopedia.org
Thanks.

Usman Khalil

Usman Khalil

Web Developer. Dead

  • Location

    Multan

  • Education

    Bachelor Mechanical Engineering

  • Work

    Frontend engineer at Copilot

  • Joined

Jun 10 '21

  • Copy link

Very valuable writing for beginners

For further actions, you may consider blocking this person and/or reporting abuse

The Four Core Concepts of webpack (2024)

FAQs

The Four Core Concepts of webpack? ›

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

What is webpack 4? ›

A detailed run-through of this module bundler

Webpack is a module bundler. In other words, Webpack takes a bunch of different assets or files (like CSS, JS, SASS, JPG, SVG, PNG …) and it combines them into bundles, a separate bundle for each type.

What is the main purpose of webpack? ›

This is why webpack exists. It's a tool that lets you bundle your JavaScript applications (supporting both ESM and CommonJS), and it can be extended to support many different assets such as images, fonts and stylesheets.

What are modules in webpack? ›

In modular programming, developers break programs up into discrete chunks of functionality called a module. Each module has a smaller surface area than a full program, making verification, debugging, and testing trivial.

What is the feature of webpack? ›

One of webpack's specific features is the ability to import any type of module, e.g. .css files, which may not be supported by other bundlers or task runners. We feel this extension of the language is warranted as it allows developers to build a more accurate dependency graph.

What's new in webpack 4? ›

Support for various Module Types

Webpack 4 now supports five module types. These module types are: javascript/auto: Webpack 3 shipped with support for this module type with all module systems enabled, CommonJS , AMD , ESM . javascript/esm: Supports only ECMAScript modules.

What is webpack explained simply? ›

Webpack is a module bundler for JavaScript applications. It processes your application's modules and packages them into one or more bundles, often a single bundle. js file, optimizing loading time for browsers.

What are the key benefits of webpack? ›

It provides splitting of code:
  • Since it supports a module system that's why files will be treated as modules hence we can use one file's features into another.
  • Despite having different files, we can access the same benefit. So it actually helps us in splitting our code into different modules.
Mar 4, 2024

What is better than webpack? ›

Vite stands out with its built-in, out-of-the-box development server, often eliminating the need for extensive configuration. In contrast, Webpack offers flexibility but requires additional setup.

Why Babel is used in webpack? ›

Babel and Webpack are often used together in modern JavaScript development. Babel is used to transpile the JavaScript code, making it compatible with older browsers, while Webpack is used to bundle and optimize the code.

What is difference between webpack and Babel? ›

In summary, Babel is a tool that is used to convert newer JavaScript code into a version that is compatible with most modern browsers, while Webpack is a tool that is used to bundle and optimize your code and assets for the web.

What is the use of webpack in react? ›

Webpack in react is a JavaScript module bundler that is commonly used with React to bundle and manage dependencies. It takes all of the individual JavaScript files and other assets in a project, such as images and CSS, and combines them into a single bundle that can be loaded by the browser.

Does webpack use ES modules? ›

ECMAScript Modules (ESM) is a specification for using Modules in the Web. It's supported by all modern browsers and the recommended way of writing modular code for the Web. Webpack supports processing ECMAScript Modules to optimize them.

What is the disadvantage of using webpack? ›

What are the most significant drawbacks of Webpack? Complicated configuration: Webpack's configuration can be complex and time-consuming (also due to complicated documentation and difficult-to-understand source code), and may require some experimentation, especially for less advanced users.

What problem does webpack solve? ›

Webpack can understand the relationships between multiple files and "resolve" them in a process called dependency resolution. If one depends on another we call the latter a dependency of the former. The process of dependency resolution involves traversing all the files in your code starting from the entry point.

Is webpack good or bad? ›

Conclusion. Webpack as a bundler is excellent for use in a multitude of applications. Especially if someone else handles the config for you (Angular, React, Vue clis). It will hopefully become better, but like anything else in JS its roots and backwards compatibility will always bring it down.

How do I migrate from webpack 4 to webpack 5? ›

To v5 from v4
  1. Preparations. Webpack 5 requires at least Node. ...
  2. Upgrade webpack 4 and its plugins/loaders. Upgrade webpack 4 to the latest available version. ...
  3. Upgrade webpack to 5. Now let's upgrade webpack to version 5: ...
  4. Everything works? ...
  5. It is not working? ...
  6. Something missing in this guide? ...
  7. Changes to internals.

Do I really need webpack? ›

On the plus side, Webpack is extremely powerful & extremely flexible: it can really do just about anything you want to your JavaScript bundle through an extensible configuration file. But the core bundling strategy and incredible flexibility of Webpack also leads to its most serious downside: speed.

What is replacing webpack? ›

Vite: Swift and On-Demand Development

Instead of pre-bundling all code and assets, Vite leverages native ES modules in modern browsers, serving code directly to the browser during development. This leads to almost instant Hot Module Replacement (HMR) and reduced cold start times.

Top Articles
Latest Posts
Article information

Author: Aron Pacocha

Last Updated:

Views: 5846

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Aron Pacocha

Birthday: 1999-08-12

Address: 3808 Moen Corner, Gorczanyport, FL 67364-2074

Phone: +393457723392

Job: Retail Consultant

Hobby: Jewelry making, Cooking, Gaming, Reading, Juggling, Cabaret, Origami

Introduction: My name is Aron Pacocha, I am a happy, tasty, innocent, proud, talented, courageous, magnificent person who loves writing and wants to share my knowledge and understanding with you.