Skip to content

Commit acc37f9

Browse files
committed
concepts/loaders: improve, add code example
* Start from the textual example, and add usage instructions (`npm install`, `webpack.config.js`). * Sketch the different configuration options. * Clarify loader resolution.
1 parent 2774c98 commit acc37f9

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

content/concepts/loaders.md

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,47 @@ contributors:
66
- ev1stensberg
77
- SpaceK33z
88
- gangachris
9+
- simon04
910
---
1011

1112
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.
1213

13-
For example, you can use loaders to tell webpack to load a CSS file or to convert TypeScript to JavaScript.
14+
## Example
15+
16+
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:
17+
18+
```
19+
npm install --save-dev css-loader
20+
npm install --save-dev ts-loader
21+
```
22+
23+
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`:
24+
25+
**webpack.config.js**
26+
27+
```js-with-links-with-details
28+
module.exports = {
29+
module: {
30+
rules: [
31+
{test: /\.css$/, use: ['css-loader'](/loaders/css-loader)},
32+
{test: /\.ts$/, use: ['ts-loader'](https://github.com/TypeStrong/ts-loader)}
33+
]
34+
}
35+
};
36+
```
37+
38+
Note that according to the [configuration options](/configuration#options), the following specifications define the identical loader usage:
39+
40+
```js-with-links-with-details
41+
{test: /\.css$/, [loader](/configuration/module#rule-loader): 'css-loader'}
42+
// or equivalently
43+
{test: /\.css$/, [use](/configuration/module#rule-use): 'css-loader'}
44+
// or equivalently
45+
{test: /\.css$/, [use](/configuration/module#rule-use): {
46+
loader: 'css-loader',
47+
options: {}
48+
}}
49+
```
1450

1551
## Loader Features
1652

@@ -28,9 +64,9 @@ functions (loaders). Users now have more flexibility to include fine-grained log
2864

2965
## Resolving Loaders
3066

31-
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.
67+
Loaders follow the standard [module resolution](/concepts/module-resolution/). In most cases you will be loaders from the [module path](/concepts/module-resolution/#module-paths) (think `npm install`, `node_modules`).
3268

33-
### Referencing Loaders
69+
[How to write a loader?](/development/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.
3470

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

content/loaders/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ contributors:
1010

1111
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.
1212

13+
?> Move the general usage documentation to the [concepts page](/concepts/loaders) and focus here on describing the available loaders (similar to [plugins](/plugins)).
14+
1315
A loader is typically a npm package, which you can install as a development dependency:
1416

1517
```sh

0 commit comments

Comments
 (0)