DEV Community

Cover image for 🚀Understanding Webpack: A begineer friendly Guide with Examples
Selvi Parasakthi K
Selvi Parasakthi K

Posted on

🚀Understanding Webpack: A begineer friendly Guide with Examples

Webpack is one of the most powerful tools in modern JavaScript development. If you're building a front-end application, you've almost certainly seen it in action—even if you didn't notice it.

This blog breaks down what Webpack does, why it matters, and how to set up a basic Webpack configuration with loaders, CSS/SASS support, and JS minification.

What is Webpack?

Webpack is a static module bundler for modern JavaScript applications.
When Webpack processes your application, it creates an internal dependency graph starting from one or more entry points—then bundles all modules (JS, CSS, images, etc.) into one or more optimized files.

In simple words:

✅ Webpack bundles your project's files (JS, CSS, images…)
into fewer, optimized files so your application loads faster.

Sample Project Setup

Reference repository:
👉https://github.com/Selvi-Parasakthi-K/webpack-practice

1. Initialize the Project

Create a package.json file:

npm init -y
Enter fullscreen mode Exit fullscreen mode

Install Webpack and Webpack CLI:

npm i webpack webpack-cli --save-dev
Enter fullscreen mode Exit fullscreen mode

Add "build": "webpack" in the scripts section, so running npm run build will trigger Webpack.

"scripts": {
  "build": "webpack"
}
Enter fullscreen mode Exit fullscreen mode

2. Create webpack.config.js

const path = require("path");

module.exports = {
  entry: "./src/index.js",
  output: {
    filename: "bundle.js",
    path: path.resolve(__dirname, "./dist"),
  },
  mode: "none",
};
Enter fullscreen mode Exit fullscreen mode

This tells webpack:

  • entry → starting file of your app

  • output → where to store bundled files

  • mode → can be "development", "production", or "none"

Loaders in Webpack

Loaders allow Webpack to handle non-JavaScript files like images, CSS, and SASS.

Think of loaders as translators—Webpack doesn’t understand images or CSS by default, so loaders help convert them into modules.

Image Loader Example

Install file-loader:

npm i file-loader --save-dev
Enter fullscreen mode Exit fullscreen mode

Add loader rule:

module: {
  rules: [
    {
      test: /\.(jfif|png)$/,
      use: ["file-loader"],
    },
  ],
}
Enter fullscreen mode Exit fullscreen mode

Setting the Public Path

Public path tells Webpack where the final bundled files (like images) will be loaded from in the browser. It basically sets the base URL for all assets so the browser knows the correct location to fetch them.

output: {
  filename: "bundle.js",
  path: path.resolve(__dirname, "./dist"),
  publicPath: "./dist/",
},
Enter fullscreen mode Exit fullscreen mode

This ensures images get the correct URL in the final bundle.

Adding CSS Support

Install these loaders:

  • css-loader → lets Webpack understand CSS files
  • style-loader → injects CSS into the browser

Order matters → loaders run from right to left

{
  test: /\.css$/,
  use: ["style-loader", "css-loader"], 
}
Enter fullscreen mode Exit fullscreen mode

If reversed, Webpack will fail.

Adding SASS/SCSS Support

Install:

npm i sass sass-loader style-loader css-loader --save-dev
Enter fullscreen mode Exit fullscreen mode

Add the rule:

{
  test: /\.scss$/,
  use: [
    "style-loader",
    "css-loader",
    {
      loader: "sass-loader",
      options: {
        implementation: require("sass"),
      },
    },
  ],
}
Enter fullscreen mode Exit fullscreen mode

Plugins in Webpack

Plugins in Webpack allow you to perform extra tasks during the build process—like optimizing, minifying, or transforming files—that go beyond what loaders can do.

Minifying JavaScript (Production Optimization)

Use terser-webpack-plugin to minify JS.

Install:

npm install terser-webpack-plugin --save-dev
Enter fullscreen mode Exit fullscreen mode

Add to your config:

const path = require("path");
const TerserPlugin = require("terser-webpack-plugin");

module.exports = {
  entry: "./src/index.js",
  output: {
    filename: "bundle.js",
    path: path.resolve(__dirname, "./dist"),
    publicPath: "./dist/",
  },
  mode: "none",
  module: {
    rules: [],
  },
  plugins: [new TerserPlugin()],
};
Enter fullscreen mode Exit fullscreen mode

This reduces file size and improves performance.

🎯 Conclusion

Webpack may look complex at first, but once you understand the basics—entry, output, loaders, and plugins—it becomes an incredibly powerful tool.

With this setup, you can:

âś” Bundle JavaScript
âś” Load images
âś” Add CSS & SASS support
âś” Minify and optimize output

You now have a complete Webpack setup suitable for any small or medium JavaScript project!

Top comments (0)