|
1 | 1 | ---
|
2 | 2 | title: Loaders
|
| 3 | +sort: 1 |
3 | 4 | contributors:
|
4 | 5 | - ev1stensberg
|
5 | 6 | - TheLarkInn
|
6 |
| -sort: 1 |
| 7 | + - manekinekko |
| 8 | + - SpaceK33z |
7 | 9 | ---
|
8 | 10 |
|
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. |
15 | 12 |
|
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 |
18 | 17 | ```
|
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. |
22 | 18 |
|
23 |
| -## Rules |
| 19 | +There are three ways to use loaders in your application: |
24 | 20 |
|
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. |
26 | 28 | 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. |
28 | 30 |
|
29 | 31 | ```js
|
30 | 32 | module: {
|
31 | 33 | 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 | + } |
38 | 46 | ]
|
39 | 47 | }
|
40 | 48 | ```
|
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. |
43 | 49 |
|
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 `!`. |
45 | 59 |
|
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"}`). |
47 | 61 |
|
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. |
49 | 63 |
|
50 |
| -This uses the loader “jade” for “.jade” files and the loaders “style” and “css” for “.css” files. |
| 64 | +## Via CLI |
51 | 65 |
|
52 |
| -## Loader Features |
| 66 | +Optionally, you could also use loaders through the CLI: |
53 | 67 |
|
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 | +``` |
72 | 71 |
|
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