Skip to content

Commit e811652

Browse files
SpaceK33zTheLarkInn
authored andcommitted
Improve loader docs (#383)
1 parent 7ad0d64 commit e811652

File tree

2 files changed

+76
-52
lines changed

2 files changed

+76
-52
lines changed

content/concepts/loaders.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,35 @@
22
title: Loaders
33
sort: 4
44
contributors:
5-
5+
- manekinekko
6+
- ev1stensberg
7+
- SpaceK33z
68
---
79

8-
?> Write about how rules work and what's the purpose of loaders.
10+
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.
11+
12+
For example, you can use loaders to tell webpack to load a CSS file or to convert TypeScript to JavaScript.
13+
14+
## Loader Features
15+
16+
* 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 an value to the next and at the end loader, webpack expects JavaScript to be returned.
17+
* Loaders can be synchronous or asynchronous.
18+
* Loaders run in Node.js and can do everything that’s possible there.
19+
* Loaders accept query parameters. This can be used to pass configuration to the loader.
20+
* Loaders can also be configured with an `options` object.
21+
* Normal modules can export a loader in addition to the normal `main` via `package.json` with the `loader` field.
22+
* Plugins can give loaders more features.
23+
* Loaders can emit additional arbitrary files.
24+
25+
Loaders allows more power in the JavaScript ecosystem through preprocessing
26+
functions (loaders). Users now have more flexibility to include fine-grained logic such as compression, packaging, language translations and [more](/loaders).
27+
28+
## Resolving Loaders
29+
30+
Loaders are [resolved similar to modules](/concepts/module-resolution/). 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.
31+
32+
### Referencing Loaders
33+
34+
By convention, loaders are usually named as `XXX-loader`, where `XXX` is the context name. For example, `json-loader`.
35+
36+
The loader name convention and precedence search order is defined by [`resolveLoader.moduleTemplates`](/configuration/resolve#resolveloader) within the webpack configuration API.

content/loaders/index.md

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,72 @@
11
---
22
title: Loaders
3+
sort: 1
34
contributors:
45
- ev1stensberg
56
- TheLarkInn
6-
sort: 1
7+
- manekinekko
8+
- SpaceK33z
79
---
810

9-
Loaders are generic functions that makes builds more flexible. This is being
10-
done in the build step, and loaders get registered to the compiler through a
11-
`require` statement. To simplify source code, several loaders can be specified
12-
through [Module.rules](https://webpack.js.org/configuration/module/#module-rules)
13-
to have loaders explicitly in one place. One could do the following using
14-
a `require` statement to attain one or more loaders:
11+
As explained in detail on the [concepts page](/concepts/loaders), loaders are transformations that are applied on a resource file of your application. Loaders allow you to, for example, configure how webpack should handle a CSS file.
1512

16-
```js
17-
require('style-loader!css-loader!less-loader!./someStyle.less')
13+
A loader is typically a npm package, which you can install as a development dependency:
14+
15+
```sh
16+
npm install css-loader --save-dev
1817
```
19-
W> Avoid using the require convention if your scripts are meant to work
20-
without adopting environment specific rules in order to achieve functionality,
21-
such as node and the browser.
2218

23-
## Rules
19+
There are three ways to use loaders in your application:
2420

25-
[`Module.rules`](https://webpack.js.org/configuration/module/#module-rules) allow you to specify several loaders within your Webpack configuration.
21+
* via configuration
22+
* explicit in the `require` statement
23+
* via CLI
24+
25+
## Via Configuration
26+
27+
[`module.rules`](https://webpack.js.org/configuration/module/#module-rules) allows you to specify several loaders within your webpack configuration.
2628
This is a concise way to display loaders, and helps to have clean code as
27-
well as you have a full overview of each respective loader.
29+
well as you have a full overview of each respective loader.
2830

2931
```js
3032
module: {
3133
rules: [
32-
{ loader: 'css-loader', options: {
33-
modules: true
34-
}
35-
},
36-
{ loader: 'postcss-loader'},
37-
{ loader: 'sass-loader'}
34+
{
35+
test: /\.css$/,
36+
use: [
37+
{ loader: 'style-loader'},
38+
{
39+
loader: 'css-loader',
40+
options: {
41+
modules: true
42+
}
43+
}
44+
]
45+
}
3846
]
3947
}
4048
```
41-
T> Use module.rules whenever possible, as this will reduce boilerplate in your
42-
source code and allows you debug or locate a loader faster if something goes south.
4349

44-
## CLI
50+
## Via `require`
51+
52+
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.
53+
54+
```js
55+
require('style-loader!css-loader?modules!./styles.css');
56+
```
57+
58+
It's possible to overwrite any loaders in the configuration by prefixing the entire rule with `!`.
4559

46-
Optionally, you could also use loaders through the CLI.
60+
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"}`).
4761

48-
`$ webpack --module-bind jade --module-bind 'css=style!css'`
62+
T> 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.
4963

50-
This uses the loader “jade” for “.jade” files and the loaders “style” and “css” for “.css” files.
64+
## Via CLI
5165

52-
## Loader Features
66+
Optionally, you could also use loaders through the CLI:
5367

54-
---
55-
- Loaders can be chained. They are applied in a pipeline to the resource. A chain
56-
of loaders are compiled chronologically. The first loader in a chain of loaders
57-
returns an value to the next and at the end loader, webpack expects JavaScript
58-
to be returned.
59-
- Loaders can be synchronous or asynchronous.
60-
- Loaders run in Node.js and can do everything that’s possible there.
61-
- Loaders accept query parameters. This can be used to pass configuration to the loader.
62-
- Plugins can give loaders more features.
63-
- Loaders can emit additional arbitrary files.
64-
- Loaders can accept an options object
65-
---
66-
67-
Loaders allows more power in the JavaScript ecosystem through preprocessing
68-
functions(loaders). Users now have more flexibility to include fine-grained logic
69-
such as compression, packaging, language translations and [more](https://webpack.github.io/docs/list-of-loaders.html)!
70-
71-
## API Reference
68+
```sh
69+
webpack --module-bind jade --module-bind 'css=style!css'
70+
```
7271

73-
---
74-
- [List of Loaders](https://webpack.github.io/docs/list-of-loaders.html)
75-
- [module.rules](https://webpack.js.org/configuration/module/#module-rules)
76-
- [Using Loaders(Old Website)](https://webpack.github.io/docs/using-loaders.html)
72+
This uses the loader “jade” for “.jade” files and the loaders “style” and “css” for “.css” files.

0 commit comments

Comments
 (0)