|
1 | 1 | ---
|
2 | 2 | title: Loaders
|
| 3 | +contributors: |
| 4 | + - ev1stensberg |
| 5 | + - TheLarkInn |
| 6 | +sort: 1 |
3 | 7 | ---
|
4 | 8 |
|
5 |
| -?> TODO |
| 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: |
| 15 | + |
| 16 | +```js |
| 17 | +require('style-loader!css-loader!less-loader!./someStyle.less') |
| 18 | +``` |
| 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 | + |
| 23 | +## Rules |
| 24 | + |
| 25 | +[`Module.rules`](https://webpack.js.org/configuration/module/#module-rules) allow you to specify several loaders within your Webpack configuration. |
| 26 | +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. |
| 28 | + |
| 29 | +```js |
| 30 | + module: { |
| 31 | + rules: [ |
| 32 | + { loader: 'css-loader', options: { |
| 33 | + modules: true |
| 34 | + } |
| 35 | + }, |
| 36 | + { loader: 'postcss-loader'}, |
| 37 | + { loader: 'sass-loader'} |
| 38 | + ] |
| 39 | + } |
| 40 | +``` |
| 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 | + |
| 44 | +## CLI |
| 45 | + |
| 46 | +Optionally, you could also use loaders through the CLI. |
| 47 | + |
| 48 | +`$ webpack --module-bind jade --module-bind 'css=style!css'` |
| 49 | + |
| 50 | +This uses the loader “jade” for “.jade” files and the loaders “style” and “css” for “.css” files. |
| 51 | + |
| 52 | +## Loader Features |
| 53 | + |
| 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 |
| 72 | + |
| 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) |
0 commit comments