Loaders

Edit this Page   

Loaders are transformations that are applied on a resource file of your application. They are functions (running in Node.js) that take the source of a resource file as the parameter and return the new source.

Example

For example, you can use loaders to tell webpack to load a CSS file or to convert TypeScript to JavaScript. Firstly, install the corresponding loaders:

npm install --save-dev css-loader
npm install --save-dev ts-loader

Secondly, configure in your webpack.config.js that for every .css file the css-loader should be used and analogously for .ts files and the ts-loader:

webpack.config.js

module.exports = {
  module: {
    rules: [
      {test: /\.css$/, use: 'css-loader'},
      {test: /\.ts$/, use: 'ts-loader'}
    ]
  }
};

Note that according to the configuration options, the following specifications define the identical loader usage:

{test: /\.css$/, loader: 'css-loader'}
// or equivalently
{test: /\.css$/, use: 'css-loader'}
// or equivalently
{test: /\.css$/, use: {
  loader: 'css-loader',
  options: {}
}}

Configuration

There are three ways to use loaders in your application:

  • via configuration
  • explicit in the require statement
  • via CLI

Via webpack.config.js

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.

  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          { loader: 'style-loader'},
          {
            loader: 'css-loader',
            options: {
              modules: true
            }
          }
        ]
      }
    ]
  }

Via require

It's possible to specify the loaders in the require statement (or define, require.ensure, etc.). Separate loaders from the resource with !. Each part is resolved relative to the current directory.

require('style-loader!css-loader?modules!./styles.css');

It's possible to overwrite any loaders in the configuration by prefixing the entire rule with !.

Options can be passed with a query parameter, just like on the web (?key=value&foo=bar). It's also possible to use a JSON object (?{"key":"value","foo":"bar"}).

Use module.rules whenever possible, as this will reduce boilerplate in your source code and allows you to debug or locate a loader faster if something goes south.

Via CLI

Optionally, you could 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.

Loader Features

  • Loaders can be chained. They are applied in a pipeline to the resource. A chain of loaders are compiled chronologically. The first loader in a chain of loaders returns a value to the next. At the end loader, webpack expects JavaScript to be returned.
  • Loaders can be synchronous or asynchronous.
  • Loaders run in Node.js and can do everything that’s possible there.
  • Loaders accept query parameters. This can be used to pass configuration to the loader.
  • Loaders can also be configured with an options object.
  • Normal modules can export a loader in addition to the normal main via package.json with the loader field.
  • Plugins can give loaders more features.
  • Loaders can emit additional arbitrary files.

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.

Resolving Loaders

Loaders follow the standard module resolution. In most cases it will be loaders from the module path (think npm install, node_modules).

How to write a loader? A loader module is expected to export a function and to be written in Node.js compatible JavaScript. In the common case you manage loaders with npm, but you can also have loaders as files in your app.

By convention, loaders are usually named as XXX-loader, where XXX is the context name. For example, json-loader.

The loader name convention and precedence search order is defined by resolveLoader.moduleTemplates within the webpack configuration API.


Contributors