webpack is a module bundler for modern JavaScript applications. It is incredibly configurable, but to get started you only need to understand Four Core Concepts: entry, output, loaders, and plugins.
This document is intended to give a high-level overview of these concepts, while providing links to detailed concept specific use-cases.
webpack creates a graph of all of your application's dependencies. The starting point of this graph is known as an entry point. The entry point tells webpack where to start and follows the graph of dependencies to know what to bundle. You can think of your application's entry point as the contextual root or the first file to kick off your app.
In webpack we define entry points using the entry
property in our webpack configuration object.
The simplest example is seen below:
webpack.config.js
module.exports = { entry: './path/to/my/entry/file.js' };
There are multiple ways to declare your entry
property that are specific to your application's needs.
Once you've bundled all of your assets together, you still need to tell webpack where to bundle your application. The webpack output
property tells webpack how to treat bundled code.
webpack.config.js
const path = require('path'); module.exports = { entry: './path/to/my/entry/file.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'my-first-webpack.bundle.js' } };
In the example above, we use the output.filename
and the output.path
properties to tell webpack the name of our bundle and where we want it to be emitted to.
You may see the term emitted or emit used throughout our documentation and plugin API. This is a fancy term for "produced or discharged".
The output
property has many more configurable features, but let's spend some time understanding some of the most common use cases for the output
property.
The goal is to have all of the assets in your project to be webpack's concern and not the browser's. (This doesn't mean that they all have to be bundled together). webpack treats every file (.css, .html, .scss, .jpg, etc.) as a module. However, webpack only understands JavaScript.
Loaders in webpack transform these files into modules as they are added to your dependency graph.
At a high level, they have two purposes in your webpack config.
test
property)use
property)webpack.config.js
const path = require('path'); const config = { entry: './path/to/my/entry/file.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'my-first-webpack.bundle.js' }, module: { rules: [ {test: /\.(js|jsx)$/, use: 'babel-loader'} ] } }; module.exports = config;
The configuration above has defined a rules
property for a single module with two required properties: test
and use
. This tells webpack's compiler the following:
"Hey webpack compiler, when you come across a path that resolves to a '.js' or '.jsx' file inside of a
require()
/import
statement, use thebabel-loader
to transform it before you add it to the bundle".
It is important to remember when defining rules in your webpack config, you are defining them undermodule.rules
and notrules
. However webpack will yell at you when doing this incorrectly.
There are more specific properties to define on loaders that we haven't yet covered.
Since Loaders only execute transforms on a per-file basis, plugins
are most commonly used (but not limited to) performing actions and custom functionality on "compilations" or "chunks" of your bundled modules (and so much more). The webpack Plugin system is extremely powerful and customizable.
In order to use a plugin, you just need to require()
it and add it to the plugins
array. Most plugins are customizable via options. Since you can use a plugin multiple times in a config for different purposes, you need to create an instance of it by calling it with new
.
webpack.config.js
const HtmlWebpackPlugin = require('html-webpack-plugin'); //installed via npm const webpack = require('webpack'); //to access built-in plugins const path = require('path'); const config = { entry: './path/to/my/entry/file.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'my-first-webpack.bundle.js' }, module: { rules: [ {test: /\.(js|jsx)$/, use: 'babel-loader'} ] }, plugins: [ new webpack.optimize.UglifyJsPlugin(), new HtmlWebpackPlugin({template: './src/index.html'}) ] }; module.exports = config;
There are many plugins that webpack provides out of the box! Check out our list of plugins for more information.
Using plugins in your webpack config is straight-forward, however there are many use-cases that are worth discussing further.
© 2012–2016 Tobias Koppers
Licensed under the Creative Commons Attribution License 4.0.
https://webpack.js.org/concepts/