|
| 1 | +Inlining files in CSS with Webpack URL Loader |
| 2 | +============================================= |
| 3 | + |
| 4 | +A simple technique to improve the performance of web applications is to reduce |
| 5 | +the number of HTTP requests inlining small files as base64 encoded URLs in the |
| 6 | +generated CSS files. |
| 7 | + |
| 8 | +Webpack Encore provides this feature via Webpack's `URL Loader`_ plugin, but |
| 9 | +it's disabled by default. First, add the URL loader to your project: |
| 10 | + |
| 11 | +.. code-block:: terminal |
| 12 | +
|
| 13 | + $ yarn add --dev url-loader |
| 14 | +
|
| 15 | +Then enable it in your ``webpack.config.js``: |
| 16 | + |
| 17 | +.. code-block:: javascript |
| 18 | +
|
| 19 | + // webpack.config.js |
| 20 | + // ... |
| 21 | +
|
| 22 | + Encore |
| 23 | + // ... |
| 24 | + .configureUrlLoader({ |
| 25 | + fonts: { limit: 4096 }, |
| 26 | + images: { limit: 4096 } |
| 27 | + }) |
| 28 | + ; |
| 29 | +
|
| 30 | +The ``limit`` option defines the maximum size in bytes of the inlined files. In |
| 31 | +the previous example, font and image files having a size below or equal to 4 KB |
| 32 | +will be inlined and the rest of files will be processed as usual. |
| 33 | + |
| 34 | +You can also use all the other options supported by the `URL Loader`_. If you |
| 35 | +want to disable this loader for either images or fonts, remove the corresponding |
| 36 | +key from the object that is passed to the ``configureUrlLoader()`` method: |
| 37 | + |
| 38 | +.. code-block:: javascript |
| 39 | +
|
| 40 | + // webpack.config.js |
| 41 | + // ... |
| 42 | +
|
| 43 | + Encore |
| 44 | + // ... |
| 45 | + .configureUrlLoader({ |
| 46 | + // 'fonts' is not defined, so only images will be inlined |
| 47 | + images: { limit: 4096 } |
| 48 | + }) |
| 49 | + ; |
| 50 | +
|
| 51 | +.. _`URL loader`: https://github.com/webpack-contrib/url-loader |
0 commit comments