Agenda:
- What is Webpack?
- Why use Webpack?
- Core concepts and features.
- Hands-on examples.
- Alternatives to Webpack.
1. What is Webpack?
- Definition: Webpack is a static module bundler for JavaScript applications. It bundles multiple files (JavaScript, CSS, images, etc.) into fewer files for efficient use in a browser.
- Use Case: Transform modular code into a format suitable for the browser, enhancing performance and manageability.
2. Why Use Webpack?
Benefits:
- Efficient Module Bundling: Combines multiple modules into fewer files, reducing HTTP requests.
- Tree Shaking: Removes unused code to optimize bundle size.
- Asset Optimization: Supports minification and compression of files like JS, CSS, and images.
- Hot Module Replacement (HMR): Updates modules in a running app without a full reload.
- Code Splitting: Splits code into smaller chunks loaded on demand.
- Cross-Browser Compatibility: Enables modern JavaScript to run in older browsers through transpilation.
3. Core Concepts and Features
-
Entry: Specifies the starting point of the application.
module.exports = { entry: './src/index.js', };
-
Output: Defines where the bundled files will be stored.
module.exports = { output: { filename: 'bundle.js', path: __dirname + '/dist', }, };
-
Loaders: Transform non-JavaScript files into modules (e.g., CSS, images).
module.exports = { module: { rules: [ { test: /\.css$/, use: ['style-loader', 'css-loader'], }, ], }, };
-
Plugins: Extend functionality, e.g., for optimization or injecting scripts.
const HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { plugins: [new HtmlWebpackPlugin({ template: './src/index.html' })], };
-
Mode: Choose between
development
andproduction
modes.module.exports = { mode: 'development', // or 'production' };
4. Hands-On Examples
Example 1: Basic Setup
- Initialize a project:
npm init -y npm install webpack webpack-cli --save-dev
- Create files:
src/index.js
console.log('Hello, Webpack!');
src/style.css
body { background-color: lightblue; }
- Configure Webpack:
webpack.config.js
const path = require('path'); module.exports = { entry: './src/index.js', output: { filename: 'bundle.js', path: path.resolve(__dirname, 'dist'), }, module: { rules: [ { test: /\.css$/, use: ['style-loader', 'css-loader'], }, ], }, mode: 'development', };
- Run Webpack:
npx webpack
Example 2: Code Splitting
- Split vendor code from application code:
module.exports = { entry: { app: './src/index.js', vendor: './src/vendor.js', }, output: { filename: '[name].bundle.js', path: path.resolve(__dirname, 'dist'), }, };
5. Alternatives to Webpack
- Vite:
- Faster development environment with native ES module support.
- Focused on modern browsers and faster build times.
- Parcel:
- Zero-config bundler with built-in support for HMR.
- Rollup:
- Best for libraries due to its efficient tree-shaking.
- ESBuild:
- Extremely fast bundler and minifier.
Comparison Table:
Feature | Webpack | Vite | Parcel | Rollup | ESBuild |
---|---|---|---|---|---|
Configurable | High | Moderate | Low | High | Low |
Speed | Moderate | Very Fast | Fast | Moderate | Extremely Fast |
HMR Support | Yes | Yes | Yes | Limited | Limited |
Tree Shaking | Yes | Yes | Yes | Yes | Yes |
Bonus Resources:
Thank you for reading!