Webpack's Core Concepts: Entry, Output, Loaders, Plugins
What you'll learn: The four foundational pillars that every Webpack configuration is built upon.
The Four Pillars
Webpack's entire configuration model revolves around four core concepts. Think of building a web app like running a factory: you need raw materials (entry), a shipping dock (output), processing machines (loaders), and quality control systems (plugins).
Entry
The entry point tells Webpack where to start building your dependency graph. It's the "root" file that imports everything else your app needs.
module.exports = {
entry: './src/index.js'
};
Webpack starts here and follows all import or require statements to discover every module your app depends on.
Output
The output specifies where Webpack should place the bundled files and what to name them.
module.exports = {
output: {
filename: 'bundle.js',
path: '/dist'
}
};
This is your shipping address—where the final product goes.
Loaders
Loaders transform non-JavaScript files into modules Webpack can understand. Remember from previous lessons: browsers only understand JS, HTML, and CSS. Loaders convert things like TypeScript, SASS, or images into formats Webpack can bundle.
module.exports = {
module: {
rules: [
{ test: /\.css$/, use: ['style-loader', 'css-loader'] }
]
}
};
Loaders are the processing machines on the assembly line—they transform inputs.
Plugins
Plugins extend Webpack's capabilities beyond transformation. They optimize bundles, inject environment variables, generate HTML files, and more. While loaders transform individual files, plugins operate on the entire bundle.
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
plugins: [new HtmlWebpackPlugin()]
};
Key Takeaway: Webpack's configuration centers on four concepts: entry (where to start), output (where to save), loaders (how to transform files), and plugins (how to extend functionality). Master these and you understand Webpack's entire mental model.