webpack
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.
Top Related Projects
Set up a modern web app by running one command.
The zero configuration build tool for the web. 📦🚀
Next-generation ES module bundler
An extremely fast bundler for the web
Next generation frontend tooling. It's fast!
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
- 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.
- 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.
- 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:
-
Install webpack and webpack-cli:
npm install webpack webpack-cli --save-dev
-
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'), }, };
-
Add a build script to your
package.json
:"scripts": { "build": "webpack" }
-
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.
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.
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.
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.
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.
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 designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
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
- Introduction
- Concepts
- Contributing
- Support
- Core Team
- Sponsoring
- Premium Partners
- Other Backers and Sponsors
- Gold Sponsors
- Silver Sponsors
- Bronze Sponsors
- Backers
- Special Thanks
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.
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.
Name | Status | Install Size | Description |
---|---|---|---|
mini-css-extract-plugin | Extracts CSS into separate files. It creates a CSS file per JS file which contains CSS. | ||
compression-webpack-plugin | Prepares compressed versions of assets to serve them with Content-Encoding | ||
html-webpack-plugin | Simplifies creation of HTML files (index.html ) to serve your bundles | ||
pug-plugin | Renders 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
Name | Status | Install Size | Description |
---|---|---|---|
val-loader | Executes code as module and considers exports as JS code |
JSON
Name | Status | Install Size | Description |
---|---|---|---|
Loads and transpiles a CSON file |
Transpiling
Name | Status | Install Size | Description |
---|---|---|---|
Loads ES2015+ code and transpiles to ES5 using Babel | |||
Loads TypeScript like JavaScript | |||
Loads CoffeeScript like JavaScript |
Templating
Name | Status | Install Size | Description |
---|---|---|---|
Exports HTML as string, requires references to static resources | |||
Loads Pug templates and returns a function | |||
Compiles Pug to a function or HTML string, useful for use with Vue, React, Angular | |||
Compiles Markdown to HTML | |||
Loads and transforms a HTML file using PostHTML | |||
Compiles Handlebars to HTML |
Styling
Name | Status | Install Size | Description |
---|---|---|---|
<style> | Add exports of a module as style to DOM | ||
Loads CSS file with resolved imports and returns CSS code | |||
Loads and compiles a LESS file | |||
Loads and compiles a Sass/SCSS file | |||
Loads and compiles a Stylus file | |||
Loads and transforms a CSS/SSS file using PostCSS |
Frameworks
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:
- Documentation updates, enhancements, designs, or bugfixes
- Spelling or grammar fixes
- README.md corrections or redesigns
- Adding unit, or functional tests
- Triaging GitHub issues -- especially determining whether an issue still persists or is reproducible.
- Searching #webpack on twitter and helping someone else who needs help
- Teaching others how to contribute to one of the many webpack's repos!
- Blogging, speaking about, or creating tutorials about one of webpack's many features.
- Helping others in our webpack gitter channel.
To get started have a look at our documentation on contributing.
If you are worried or don't know where to start, you can always reach out to Sean Larkin (@TheLarkInn) on Twitter or simply submit an issue and a maintainer can help give you guidance!
We have also started a series on our Medium Publication called The Contributor's Guide to webpack. We welcome you to read it and post any questions or responses if you still need help.
Looking to speak about webpack? We'd love to review your talk abstract/CFP! You can email it to webpack [at] opencollective [dot] com and we can give pointers or tips!!!
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!!
Looking for webpack 1 docs? Please check out the old wiki, but note that this deprecated version is no longer supported.
If you want to discuss something or just need help, here is our Gitter room where there are always individuals looking to help out!
If you are still having difficulty, we would love for you to post a question to StackOverflow with the webpack tag. It is much easier to answer questions that include your webpack.config.js and relevant files! So if you can provide them, we'd be extremely grateful (and more likely to help you find the answer!)
If you are twitter savvy you can tweet #webpack with your question and someone should be able to reach out and help also.
If you have discovered a ð or have a feature suggestion, feel free to create an issue on GitHub.
License
Core Team
Tobias Koppers Core Founder of webpack |
Johannes Ewald Loaders & Plugins Early adopter of webpack |
Sean T. Larkin Public Relations Founder of the core team |
Kees Kluskens Development Sponsor |
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 I wanted Code Splitting for modules-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.
- Every early webpack user, which contributed to webpack by writing issues or PRs. You influenced the direction...
- @shama, @jhnns and @sokra for maintaining this project
- Everyone who has written a loader for webpack. You are the ecosystem...
- Everyone I forgot to mention here, but also influenced webpack.
Top Related Projects
Set up a modern web app by running one command.
The zero configuration build tool for the web. 📦🚀
Next-generation ES module bundler
An extremely fast bundler for the web
Next generation frontend tooling. It's fast!
Build system optimized for JavaScript and TypeScript, written in Rust
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot