|
| 1 | +Preventing Duplication by "Splitting" Shared Code into Separate Files |
| 2 | +===================================================================== |
| 3 | + |
| 4 | +Suppose you have multiple entry files and *each* requires ``jquery``. In this |
| 5 | +case, *each* output file will contain jQuery, making your files much larger than |
| 6 | +necessary. To solve this, you can ask webpack to analyze your files and *split* them |
| 7 | +into additional files, which will contain "shared" code. |
| 8 | + |
| 9 | +To enable this, call ``splitEntryChunks()``: |
| 10 | + |
| 11 | +.. code-block:: diff |
| 12 | +
|
| 13 | + Encore |
| 14 | + // ... |
| 15 | +
|
| 16 | + // multiple entry files, which probably import the same code |
| 17 | + .addEntry('app', './assets/js/app.js') |
| 18 | + .addEntry('homepage', './assets/js/homepage.js') |
| 19 | + .addEntry('blog', './assets/js/blog.js') |
| 20 | + .addEntry('store', './assets/js/store.js') |
| 21 | +
|
| 22 | + + .splitEntryChunks() |
| 23 | +
|
| 24 | +
|
| 25 | +Now, each output file (e.g. ``homepage.js``) *may* be split into multiple file |
| 26 | +(e.g. ``homepage.js``, ``vendor~homepage.js``). This means that you *may* need to |
| 27 | +include *multiple* ``script`` tags (or ``link`` tags for CSS) in your template. |
| 28 | +Encore creates an :ref:`entrypoints.json <encore-entrypointsjson-simple-description>` |
| 29 | +file that lists exactly which CSS and JavaScript files are needed for each entry. |
| 30 | + |
| 31 | +If you're using the ``encore_entry_link_tags()`` and ``encore_entry_script_tags()`` |
| 32 | +Twig functions from WebpackEncoreBundle, you don't need to do anything else! These |
| 33 | +functions automatically read this file and render as many ``script`` or ``link`` |
| 34 | +tags as needed: |
| 35 | + |
| 36 | +.. code-block:: twig |
| 37 | +
|
| 38 | + {# |
| 39 | + May now render multiple script tags: |
| 40 | + <script src="/build/homepage.js"></script> |
| 41 | + <script src="/build/vendor~homepage.js"></script> |
| 42 | + #} |
| 43 | + {{ encore_entry_script_tags('homepage') }} |
| 44 | +
|
| 45 | +Controlling how Assets are Split |
| 46 | +-------------------------------- |
| 47 | + |
| 48 | +The logic for *when* and *how* to split the files is controlled by the |
| 49 | +`SplitChunksPlugin from Webpack`_. You can control the configuration passed to |
| 50 | +this plugin with the ``configureSplitChunks()`` function: |
| 51 | + |
| 52 | +.. code-block:: js |
| 53 | +
|
| 54 | +.. code-block:: diff |
| 55 | +
|
| 56 | + Encore |
| 57 | + // ... |
| 58 | +
|
| 59 | + .splitEntryChunks() |
| 60 | + + .configureSplitChunks(function(splitChunks) { |
| 61 | + + // change the configuration |
| 62 | + + splitChunks.minSize = 0; |
| 63 | + + } |
| 64 | +
|
| 65 | +- talk about SplitChunksPlugin & how you talk to it in Encore |
| 66 | +- also look at the runtime chunk stuff |
| 67 | + |
| 68 | +.. _`SplitChunksPlugin from Webpack`: https://webpack.js.org/plugins/split-chunks-plugin/ |
0 commit comments