This content originally appeared on DEV Community and was authored by Dharmendra Kumar
π Webpack: Before & After β A Game Changer! β‘
Discover how Webpack transforms your project!
Weβre diving into a side-by-side comparison of a project without Webpack vs. with Webpack. π οΈ This will show you just how much more efficient your workflow can be with bundling and optimizations!
Webpack Preview Link
π± Without Webpack: The Old Way πΎ
When you structure a project without Webpack, you manually manage everything β from scripts to modules. Here's how your project setup would look:
π§ Project Structure
webpack-example-no-webpack/
βββ dist/
β βββ index.html # Main HTML file
βββ src/
β βββ math.js # Utility functions
β βββ module.js # Dynamically loaded
β βββ index.js # Main entry point
βββ .gitignore
βοΈ How the Code Looks Without Webpack
1. Utility Functions (math.js)
This file handles simple math utilities.
// src/math.js
function add(a, b) { return a + b; } // Addition
function subtract(a, b) { return a - b; } // Subtraction
function multiply(a, b) { return a * b; } // Multiplication
function divide(a, b) { return a / b; } // Division
// Export as MathUtils to be used globally
export const MathUtils = { add, subtract, multiply, divide };
2. Dynamically Loaded Module (module.js)
This file loads dynamically when needed.
// src/module.js
function greet() {
console.log('Hello from dynamically loaded module!');
}
// Exposing greet globally
window.GreetModule = { greet };
3. Main Entry Point (index.js)
Manages interactions with the browser and handles dynamic module loading.
// src/index.js
import { MathUtils } from "./math.js";
document.getElementById('load-module').addEventListener('click', function () {
const script = document.createElement('script');
script.src = '../src/module.js'; // Load module.js dynamically
document.body.appendChild(script);
script.onload = function () {
GreetModule.greet(); // Trigger greet after loading
};
});
console.log(`5 + 10 = ${MathUtils.add(5, 10)}`);
β‘ With Webpack: The Modern Approach π₯
Webpack introduces bundling, code-splitting, and tree-shaking to make your code faster and more efficient. Here's what the structure looks like with Webpack:
π§ Project Structure
webpack-example-with-webpack/
βββ dist/
β βββ index.html # HTML file with bundled JS auto-included
β βββ main.bundle.js # Bundled JS
βββ src/
β βββ math.js # Utility functions (tree-shakable)
β βββ module.js # Dynamically loaded module
β βββ index.js # Main entry point
βββ webpack.config.js # Webpack configuration
βββ package.json
βοΈ How the Code Looks With Webpack
1. Utility Functions (math.js)
Functions are the same, but now Webpack will only include what's actually used.
// src/math.js
export function add(a, b) { return a + b; } // Tree-shakable utilities
export function subtract(a, b) { return a - b; }
export function multiply(a, b) { return a * b; }
export function divide(a, b) { return a / b; }
2. Dynamically Loaded Module (module.js)
Now loaded with Webpack's import()
for code splitting.
// src/module.js
export function greet() {
console.log('Hello from dynamically loaded module!');
}
3. Main Entry Point (index.js)
// src/index.js
import { add, multiply } from './math'; // Only used functions imported
console.log(`5 + 10 = ${add(5, 10)}`);
document.getElementById('load-module').addEventListener('click', () => {
import('./module').then(module => {
module.greet(); // Loaded dynamically by Webpack
});
});
π§ Webpack Configuration (webpack.config.js)
const path = require('path');
module.exports = {
mode: 'production', // Enables optimizations
entry: './src/index.js',
output: {
filename: 'main.bundle.js', // Bundled file
chunkFilename: '[name].chunk.js', // For dynamically imported modules
path: path.resolve(__dirname, 'dist'),
},
optimization: {
usedExports: true, // Enables tree shaking
},
};
π What's the Difference?
π― Without Webpack
- π Manually handle script loading.
- π No optimization, tree shaking, or code splitting.
- π Larger file sizes and unoptimized code.
β‘ With Webpack
- π Automatic bundling and minification.
- π² Tree shaking: Only used code gets included.
- β³ Code splitting: Load parts of the code only when needed.
π Want to Explore This?
Check out the full repository here:
https://github.com/dharam-gfx/webpack-example
If you find this helpful, give the repo a β! π
This content originally appeared on DEV Community and was authored by Dharmendra Kumar
Dharmendra Kumar | Sciencx (2024-09-25T07:17:19+00:00) Compare the Old-School Way of Manual Code Management vs. Webpack’s Magic: Reduce File Size and Maximize Efficiency! π§ββοΈ. Retrieved from https://www.scien.cx/2024/09/25/compare-the-old-school-way-of-manual-code-management-vs-webpacks-magic-reduce-file-size-and-maximize-efficiency-%f0%9f%a7%99%e2%99%82%ef%b8%8f/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.