Convert Figma logo to code with AI

webpack logowebpack

A bundler for javascript and friends. Packs many modules into a few bundled assets. Code Splitting allows for loading parts of the application on demand. Through "loaders", modules can be CommonJs, AMD, ES6 modules, CSS, Images, JSON, Coffeescript, LESS, ... and your custom stuff.

65,133
9,019
65,133
241

Top Related Projects

Set up a modern web app by running one command.

43,725

The zero configuration build tool for the web. 📦🚀

25,651

Next-generation ES module bundler

38,730

An extremely fast bundler for the web

71,578

Next generation frontend tooling. It's fast!

27,269

Build system optimized for JavaScript and TypeScript, written in Rust

Quick Overview

Webpack is a powerful and highly configurable module bundler for JavaScript applications. It takes modules with dependencies and generates static assets representing those modules, allowing developers to build complex, modular applications efficiently. Webpack is widely used in modern web development, particularly for single-page applications and complex front-end projects.

Pros

  • Highly flexible and customizable through a rich plugin ecosystem
  • Supports code splitting and lazy loading for improved performance
  • Handles various asset types beyond JavaScript (CSS, images, fonts, etc.)
  • Provides a development server with hot module replacement for rapid development

Cons

  • Steep learning curve, especially for complex configurations
  • Can be slow for large projects without proper optimization
  • Configuration can become complex and difficult to maintain
  • Initial setup and boilerplate code can be overwhelming for beginners

Code Examples

  1. Basic webpack configuration:
const path = require('path');

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
};

This example shows a minimal webpack configuration file that specifies an entry point and output location for the bundled JavaScript.

  1. Using loaders to handle non-JavaScript files:
module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader'],
      },
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: ['file-loader'],
      },
    ],
  },
};

This configuration adds rules to handle CSS and image files using appropriate loaders.

  1. Code splitting with dynamic imports:
import('./module').then(module => {
  // Use the module
});

This example demonstrates how to use dynamic imports for code splitting, which webpack will automatically handle to create separate chunks.

Getting Started

To start using webpack in your project:

  1. Install webpack and webpack-cli:

    npm install webpack webpack-cli --save-dev
    
  2. Create a webpack.config.js file in your project root:

    const path = require('path');
    
    module.exports = {
      entry: './src/index.js',
      output: {
        filename: 'main.js',
        path: path.resolve(__dirname, 'dist'),
      },
    };
    
  3. Add a build script to your package.json:

    "scripts": {
      "build": "webpack"
    }
    
  4. Run the build command:

    npm run build
    

This will bundle your application starting from src/index.js and output the result to dist/main.js.

Competitor Comparisons

Set up a modern web app by running one command.

Pros of Create React App

  • Simplified setup and configuration for React projects
  • Includes pre-configured development environment with hot reloading
  • Abstracts away complex build processes, making it easier for beginners

Cons of Create React App

  • Less flexibility for custom configurations without ejecting
  • Larger bundle sizes due to included dependencies
  • Limited control over the underlying build system

Code Comparison

Create React App:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(<App />, document.getElementById('root'));

Webpack:

const path = require('path');

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
};

Summary

Create React App provides a streamlined approach for setting up React projects, offering a pre-configured environment that's ideal for beginners and rapid prototyping. It abstracts away complex build processes, making it easier to get started quickly.

On the other hand, Webpack offers more flexibility and control over the build process, allowing for fine-tuned configurations. It's better suited for advanced users who need custom setups or want to optimize their build process for specific requirements.

While Create React App simplifies React development, Webpack provides a more powerful and customizable build tool that can be used for various types of projects beyond just React applications.

43,725

The zero configuration build tool for the web. 📦🚀

Pros of Parcel

  • Zero configuration required out of the box
  • Faster build times due to multicore processing
  • Automatic code splitting without additional setup

Cons of Parcel

  • Less flexibility and customization options
  • Smaller ecosystem and community compared to Webpack
  • Limited support for advanced optimization techniques

Code Comparison

Webpack configuration:

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
};

Parcel usage:

parcel index.html

Webpack requires a configuration file to define entry points, output, and other settings. Parcel, on the other hand, can bundle your application with a single command, automatically detecting entry points and dependencies.

Webpack offers more control over the bundling process, allowing for fine-tuned optimizations and custom loaders. Parcel aims for simplicity and ease of use, with many features working out of the box.

While Webpack has a larger ecosystem and more extensive documentation, Parcel is gaining popularity due to its simplicity and speed. The choice between the two depends on project requirements, team expertise, and desired level of customization.

25,651

Next-generation ES module bundler

Pros of Rollup

  • Simpler configuration and faster build times for smaller projects
  • Better tree-shaking capabilities, resulting in smaller bundle sizes
  • Native ES module support, ideal for library authors

Cons of Rollup

  • Less extensive plugin ecosystem compared to Webpack
  • Limited support for code splitting and dynamic imports
  • Not as well-suited for complex applications with many non-JavaScript assets

Code Comparison

Rollup configuration:

export default {
  input: 'src/main.js',
  output: {
    file: 'bundle.js',
    format: 'iife'
  }
};

Webpack configuration:

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  }
};

Both Webpack and Rollup are popular JavaScript module bundlers, but they have different strengths and use cases. Rollup excels in creating smaller, more efficient bundles for libraries and simple applications, while Webpack offers more features and flexibility for complex projects with diverse asset types. The choice between the two depends on the specific requirements of your project and the ecosystem you're working in.

38,730

An extremely fast bundler for the web

Pros of esbuild

  • Significantly faster build times due to its Go-based implementation
  • Simpler configuration with fewer options, making it easier to set up
  • Built-in support for TypeScript and JSX without additional plugins

Cons of esbuild

  • Less mature ecosystem with fewer plugins and integrations
  • Limited customization options compared to webpack's extensive configuration
  • May not support some advanced features or edge cases that webpack handles

Code Comparison

esbuild configuration:

require('esbuild').build({
  entryPoints: ['app.js'],
  bundle: true,
  outfile: 'out.js',
}).catch(() => process.exit(1))

webpack configuration:

module.exports = {
  entry: './app.js',
  output: {
    filename: 'out.js',
  },
  mode: 'production',
};

Both esbuild and webpack are popular bundling tools for JavaScript projects. esbuild focuses on speed and simplicity, offering blazing-fast build times and a straightforward configuration. It's an excellent choice for projects that prioritize build performance and don't require extensive customization.

webpack, on the other hand, provides a more mature and flexible ecosystem with a wide range of plugins and loaders. It offers greater customization options and supports complex build scenarios, making it suitable for large-scale applications with specific requirements.

The choice between esbuild and webpack depends on project needs, with esbuild excelling in speed and simplicity, while webpack offers more advanced features and extensibility.

71,578

Next generation frontend tooling. It's fast!

Pros of Vite

  • Significantly faster build and development times due to native ES modules
  • Simpler configuration with sensible defaults
  • Hot Module Replacement (HMR) that's faster and more reliable

Cons of Vite

  • Less mature ecosystem and plugin availability compared to Webpack
  • Limited support for older browsers that don't support ES modules
  • Potential compatibility issues with some existing projects or libraries

Code Comparison

Webpack configuration:

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist'),
  },
  module: {
    rules: [
      { test: /\.css$/, use: ['style-loader', 'css-loader'] },
    ],
  },
};

Vite configuration:

export default {
  root: './src',
  build: {
    outDir: '../dist',
  },
};

The Vite configuration is notably simpler, with many features working out-of-the-box without explicit configuration. Webpack requires more detailed setup but offers greater customization options.

Both tools serve similar purposes in modern web development, with Vite focusing on speed and simplicity while Webpack provides a more established and flexible ecosystem.

27,269

Build system optimized for JavaScript and TypeScript, written in Rust

Pros of Turborepo

  • Optimized for monorepo management and build caching
  • Faster build times through intelligent task orchestration
  • Seamless integration with other Vercel tools and services

Cons of Turborepo

  • Limited to JavaScript/TypeScript ecosystems
  • Steeper learning curve for teams new to monorepo structures
  • Less flexibility for custom build configurations compared to Webpack

Code Comparison

Turborepo configuration (turbo.json):

{
  "pipeline": {
    "build": {
      "dependsOn": ["^build"],
      "outputs": ["dist/**"]
    }
  }
}

Webpack configuration (webpack.config.js):

module.exports = {
  entry: './src/index.js',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'dist')
  }
};

Turborepo focuses on defining pipelines and dependencies between packages in a monorepo, while Webpack configurations center around bundling and asset processing for individual projects. Turborepo's approach is more high-level and suited for managing complex project structures, whereas Webpack provides granular control over the build process for single applications or libraries.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README



npm

node builds1 builds2 dependency-review coverage PR's welcome compatibility-score downloads install-size backers sponsors contributors discussions discord

webpack

Webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable of transforming, bundling, or packaging just about any resource or asset.

Table of Contents

Install

Install with npm:

npm install --save-dev webpack

Install with yarn:

yarn add webpack --dev

Introduction

Webpack is a bundler for modules. The main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable of transforming, bundling, or packaging just about any resource or asset.

TL;DR

  • Bundles ES Modules, CommonJS, and AMD modules (even combined).
  • Can create a single bundle or multiple chunks that are asynchronously loaded at runtime (to reduce initial loading time).
  • Dependencies are resolved during compilation, reducing the runtime size.
  • Loaders can preprocess files while compiling, e.g. TypeScript to JavaScript, Handlebars strings to compiled functions, images to Base64, etc.
  • Highly modular plugin system to do whatever else your application requires.

Learn about webpack through videos!

Get Started

Check out webpack's quick Get Started guide and the other guides.

Browser Compatibility

Webpack supports all browsers that are ES5-compliant (IE8 and below are not supported). Webpack also needs Promise for import() and require.ensure(). If you want to support older browsers, you will need to load a polyfill before using these expressions.

Concepts

Plugins

Webpack has a rich plugin interface. Most of the features within webpack itself use this plugin interface. This makes webpack very flexible.

NameStatusInstall SizeDescription
mini-css-extract-pluginmini-css-npmmini-css-sizeExtracts CSS into separate files. It creates a CSS file per JS file which contains CSS.
compression-webpack-plugincompression-npmcompression-sizePrepares compressed versions of assets to serve them with Content-Encoding
html-webpack-pluginhtml-plugin-npmhtml-plugin-sizeSimplifies creation of HTML files (index.html) to serve your bundles
pug-pluginpug-plugin-npmpug-plugin-sizeRenders Pug files to HTML, extracts JS and CSS from sources specified directly in Pug.

Loaders

Webpack enables the use of loaders to preprocess files. This allows you to bundle any static resource way beyond JavaScript. You can easily write your own loaders using Node.js.

Loaders are activated by using loadername! prefixes in require() statements, or are automatically applied via regex from your webpack configuration.

Files

NameStatusInstall SizeDescription
val-loaderval-npmval-sizeExecutes code as module and considers exports as JS code

JSON

NameStatusInstall SizeDescription
cson-npmcson-sizeLoads and transpiles a CSON file

Transpiling

NameStatusInstall SizeDescription
babel-npmbabel-sizeLoads ES2015+ code and transpiles to ES5 using Babel
type-npmtype-sizeLoads TypeScript like JavaScript
coffee-npmcoffee-sizeLoads CoffeeScript like JavaScript

Templating

NameStatusInstall SizeDescription
html-npmhtml-sizeExports HTML as string, requires references to static resources
pug-npmpug-sizeLoads Pug templates and returns a function
pug3-npmpug3-sizeCompiles Pug to a function or HTML string, useful for use with Vue, React, Angular
md-npmmd-sizeCompiles Markdown to HTML
posthtml-npmposthtml-sizeLoads and transforms a HTML file using PostHTML
hbs-npmhbs-sizeCompiles Handlebars to HTML

Styling

NameStatusInstall SizeDescription
<style>style-npmstyle-sizeAdd exports of a module as style to DOM
css-npmcss-sizeLoads CSS file with resolved imports and returns CSS code
less-npmless-sizeLoads and compiles a LESS file
sass-npmsass-sizeLoads and compiles a Sass/SCSS file
stylus-npmstylus-sizeLoads and compiles a Stylus file
postcss-npmpostcss-sizeLoads and transforms a CSS/SSS file using PostCSS

Frameworks

NameStatusInstall SizeDescription
vue-npmvue-sizeLoads and compiles Vue Components
polymer-npmpolymer-sizeProcess HTML & CSS with preprocessor of choice and require() Web Components like first-class modules
angular-npmangular-sizeLoads and compiles Angular 2 Components
riot-npmriot-sizeRiot official webpack loader
svelte-npmsvelte-sizeOfficial Svelte loader

Performance

Webpack uses async I/O and has multiple caching levels. This makes webpack fast and incredibly fast on incremental compilations.

Module Formats

Webpack supports ES2015+, CommonJS and AMD modules out of the box. It performs clever static analysis on the AST of your code. It even has an evaluation engine to evaluate simple expressions. This allows you to support most existing libraries out of the box.

Code Splitting

Webpack allows you to split your codebase into multiple chunks. Chunks are loaded asynchronously at runtime. This reduces the initial loading time.

Optimizations

Webpack can do many optimizations to reduce the output size of your JavaScript by deduplicating frequently used modules, minifying, and giving you full control of what is loaded initially and what is loaded at runtime through code splitting. It can also make your code chunks cache friendly by using hashes.

Contributing

We want contributing to webpack to be fun, enjoyable, and educational for anyone, and everyone. We have a vibrant ecosystem that spans beyond this single repo. We welcome you to check out any of the repositories in our organization or webpack-contrib organization which houses all of our loaders and plugins.

Contributions go far beyond pull requests and commits. Although we love giving you the opportunity to put your stamp on webpack, we also are thrilled to receive a variety of other contributions including:

To get started have a look at our documentation on contributing.

Creating your own plugins and loaders

If you create a loader or plugin, we would <3 for you to open source it, and put it on npm. We follow the x-loader, x-webpack-plugin naming convention.

Support

We consider webpack to be a low-level tool used not only individually but also layered beneath other awesome tools. Because of its flexibility, webpack isn't always the easiest entry-level solution, however we do believe it is the most powerful. That said, we're always looking for ways to improve and simplify the tool without compromising functionality. If you have any ideas on ways to accomplish this, we're all ears!

If you're just getting started, take a look at our new docs and concepts page. This has a high level overview that is great for beginners!!

If you have discovered a 🐜 or have a feature suggestion, feel free to create an issue on GitHub.

Current project members

For information about the governance of the Node.js project, see GOVERNANCE.md.

TSC (Technical Steering Committee)

Core Collaborators

Sponsoring

Most of the core team members, webpack contributors and contributors in the ecosystem do this open source work in their free time. If you use webpack for a serious task, and you'd like us to invest more time on it, please donate. This project increases your income/productivity too. It makes development and applications faster and it reduces the required bandwidth.

This is how we use the donations:

  • Allow the core team to work on webpack
  • Thank contributors if they invested a large amount of time in contributing
  • Support projects in the ecosystem that are of great value for users
  • Support projects that are voted most (work in progress)
  • Infrastructure cost
  • Fees for money handling

Premium Partners

Other Backers and Sponsors

Before we started using OpenCollective, donations were made anonymously. Now that we have made the switch, we would like to acknowledge these sponsors (and the ones who continue to donate using OpenCollective). If we've missed someone, please send us a PR, and we'll add you to this list.

Gold Sponsors

Become a gold sponsor and get your logo on our README on GitHub with a link to your site.

Silver Sponsors

Become a silver sponsor and get your logo on our README on GitHub with a link to your site.

Bronze Sponsors

Become a bronze sponsor and get your logo on our README on GitHub with a link to your site.

Backers

Become a backer and get your image on our README on GitHub with a link to your site.

Special Thanks to

(In chronological order)

  • @google for Google Web Toolkit (GWT), which aims to compile Java to JavaScript. It features a similar Code Splitting as webpack.
  • @medikoo for modules-webmake, which is a similar project. webpack was born because of the desire for code splitting for modules such as Webmake. Interestingly, the Code Splitting issue is still open (thanks also to @Phoscur for the discussion).
  • @substack for browserify, which is a similar project and source for many ideas.
  • @jrburke for require.js, which is a similar project and source for many ideas.
  • @defunctzombie for the browser-field spec, which makes modules available for node.js, browserify and webpack.
  • @sokra for creating webpack.
  • Every early webpack user, which contributed to webpack by writing issues or PRs. You influenced the direction.
  • All past and current webpack maintainers and collaborators.
  • Everyone who has written a loader for webpack. You are the ecosystem...
  • Everyone not mentioned here but that has also influenced webpack.

NPM DownloadsLast 30 Days