What is Webpack?
-
Webpack is a module bundler for JavaScript applications.
-
It takes your JavaScript files (and other assets like CSS, images) and bundles them into one or more optimized files for the browser.
-
Helps manage dependencies and improve performance.
Why Use Webpack?
-
Bundles multiple files into fewer files (often one).
-
Supports modern JavaScript features (ES6 modules).
-
Allows use of loaders to process files (e.g., transpile JSX, SCSS).
-
Supports plugins to optimize build (e.g., minification, environment variables).
-
Enables hot module replacement (HMR) for fast development.
Basic Concepts
| Concept | Description |
|---|---|
| Entry | The starting point(s) for bundling |
| Output | Where the bundled files will be saved |
| Loaders | Transform files (e.g., Babel for JSX) |
| Plugins | Extend functionality (e.g., clean build folder) |
| Mode | ‘development’ or ‘production’ optimizations |
Basic Webpack Configuration Example
Running Webpack
-
Install dependencies:
-
Add a build script in
package.json:
-
Run build:
Summary
-
Webpack bundles your JS (and assets) into optimized files.
-
Entry point is where bundling starts.
-
Loaders transform files during bundling.
-
Plugins add extra capabilities.
-
Mode controls optimizations for dev or production.
