Loaders are transformations that are applied on the source code of a module. They allow you to pre-process files as you import
or “load” them. Thus, loaders are kind of like “tasks” in other build tools and provide a powerful way to handle front-end build steps. Loaders can transform files from a different language (like TypeScript) to JavaScript or inline images as data URLs. Loaders even allow you to do things like import
CSS files directly from your JavaScript modules!
For example, you can use loaders to tell webpack to load a CSS file or to convert TypeScript to JavaScript. To do this, you would start by installing the loaders you need:
npm install --save-dev css-loader npm install --save-dev ts-loader
And then instruct webpack to use the css-loader
for every .css
file and the ts-loader
for all .ts
files:
webpack.config.js
module.exports = { module: { rules: [ { test: /\.css$/, use: 'css-loader' }, { test: /\.ts$/, use: 'ts-loader' } ] } };
There are three ways to use loaders in your application:
import
statement.module.rules
allows you to specify several loaders within your webpack configuration. This is a concise way to display loaders, and helps to maintain clean code. It also offers you a full overview of each respective loader.
Loaders are evaluated/executed from right to left. In the example below execution starts with sass-loader, continues with css-loader and finally ends with style-loader. See "Loader Features" for more information about loaders order.
module.exports = { module: { rules: [ { test: /\.css$/, use: [ { loader: 'style-loader' }, { loader: 'css-loader', options: { modules: true } }, { loader: 'sass-loader' } ] } ] } };
It's possible to specify loaders in an import
statement, or any equivalent "importing" method. Separate loaders from the resource with !
. Each part is resolved relative to the current directory.
import Styles from 'style-loader!css-loader?modules!./styles.css';
It's possible to override any loaders in the configuration by prefixing the entire rule with !
.
Options can be passed with a query parameter, e.g. ?key=value&foo=bar
, or a JSON object, e.g. ?{"key":"value","foo":"bar"}
.
Use module.rules
whenever possible, as this will reduce boilerplate in your source code and allow you to debug or locate a loader faster if something goes south.
You can also use loaders through the CLI:
webpack --module-bind jade-loader --module-bind 'css=style-loader!css-loader'
This uses the jade-loader
for .jade
files, and the style-loader
and css-loader
for .css
files.
options
object (using query
parameters to set options is still supported but has been deprecated).main
via package.json
with the loader
field.Loaders allow more power in the JavaScript ecosystem through preprocessing functions (loaders). Users now have more flexibility to include fine-grained logic such as compression, packaging, language translations and more.
Loaders follow the standard module resolution. In most cases it will be loaders from the module path (think npm install
, node_modules
).
A loader module is expected to export a function and be written in Node.js compatible JavaScript. They are most commonly managed with npm, but you can also have custom loaders as files within your application. By convention, loaders are usually named xxx-loader
(e.g. json-loader
). See "How to Write a Loader?" for more information.
© JS Foundation and other contributors
Licensed under the Creative Commons Attribution License 4.0.
https://webpack.js.org/concepts/loaders