Before a module bundler, keeping a page's script tags in the correct dependency order was a manual, fragile job. Webpack instead reads import/require statements directly out of the code, builds a dependency graph, and outputs one bundle in the correct order automatically.
// webpack.config.js
module.exports = {
entry: './src/index.js',
output: { filename: 'bundle.js', path: __dirname + '/dist' },
};
Loaders extend what "JavaScript" can import
Loaders let import reach beyond .js files - pulling in Sass, images, even fonts - so the whole front-end dependency graph, not just scripts, is expressed in one place:
module.exports = {
module: {
rules: [{ test: /\.scss$/, use: ['style-loader', 'css-loader', 'sass-loader'] }]
}
};
It's a steeper learning curve than a simple task runner, but for any project with real dependency complexity, it pays that curve back quickly.
What bundling is actually solving
module.exports = {
entry: './src/index.js',
output: { filename: 'bundle.js', path: __dirname + '/dist' },
module: { rules: [{ test: /\.css$/, use: ['style-loader', 'css-loader'] }] }
};
Browsers didn't support ES module imports natively for years, so bundlers walked a project's import graph and combined everything into files a browser could actually load - while also handling non-JS assets like CSS and images through the loader system, which is why a "JavaScript bundler" ended up processing an entire project's assets rather than just script files.
Code splitting is the feature that justified the configuration complexity
Rather than shipping one enormous bundle for an entire application, dynamic imports let webpack split code into chunks loaded on demand - a route's code only downloads when a user actually navigates to it. That capability is most of why webpack's genuinely intimidating configuration became standard practice despite newer, simpler tools like Vite existing: large applications needed the splitting far more than they needed simplicity.