Skip to content

Commit

Permalink
docs(api): clarify the pitch method and how it can be used in loade…
Browse files Browse the repository at this point in the history
…rs.md

Resolves #449
Resolves #116

Refactored and enhanced the existing section using much of @sokra's
explanation from:

webpack/webpack#360
  • Loading branch information
skipjack committed Dec 6, 2017
1 parent d6dee86 commit c0db521
Showing 1 changed file with 48 additions and 6 deletions.
54 changes: 48 additions & 6 deletions src/content/api/loaders.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,24 +90,66 @@ module.exports.raw = true;

### Pitching Loader

Loaders are **always** called from right to left. But, in some cases, loaders do not care about the results of the previous loader or the resource. They only care about **metadata**. The `pitch` method on the loaders is called from **left to right** before the loaders are called (from right to left).
Loaders are __always__ called from right to left. There are some instances where the loader only cares about the __metadata__ behind a request and can ignore the results of the previous loader. The `pitch` method on loaders is called from __left to right__ before the loaders are actually executed (from right to left). For the following [`use`](/configuration/module#rule-use) configuration:

If a loader delivers a result in the `pitch` method the process turns around and skips the remaining loaders, continuing with the calls to the more left loaders. `data` can be passed between pitch and normal call.
``` js
use: [
'a-loader',
'b-loader',
'c-loader'
]
```

These steps would occur:

``` diff
|- a-loader `pitch`
|- b-loader `pitch`
|- c-loader `pitch`
|- requested module is picked up as a dependency
|- c-loader normal execution
|- b-loader normal execution
|- a-loader normal execution
```

So why might a loader take advantage of the "pitching" phase?

First, the `data` passed to the `pitch` method is exposed in the execution phase as well under `this.data` and could be useful for capturing and sharing information from earlier in the cycle.

``` js
module.exports = function(content) {
return someSyncOperation(content, this.data.value);
};

module.exports.pitch = function(remainingRequest, precedingRequest, data) {
if(someCondition()) {
// fast exit
return "module.exports = require(" + JSON.stringify("-!" + remainingRequest) + ");";
}
data.value = 42;
};
```

Second, if a loader delivers a result in the `pitch` method the process turns around and skips the remaining loaders. In our example above, if the `b-loader`s `pitch` method returned something:

``` js
module.exports = function(content) {
return someSyncOperation(content);
};

module.exports.pitch = function(remainingRequest, precedingRequest, data) {
if (someCondition()) {
return "module.exports = require(" + JSON.stringify("-!" + remainingRequest) + ");";
}
};
```

The steps above would be shortened to:

``` diff
|- a-loader `pitch`
|- b-loader `pitch` returns a module
|- a-loader normal execution
```

See the [bundle-loader](https://github.com/webpack-contrib/bundle-loader) for a good example of how this process can be used in a more meaningful way.


## The Loader Context

Expand Down

0 comments on commit c0db521

Please sign in to comment.