@@ -3,53 +3,55 @@ Creating a Shared Commons Entry
33
44Suppose you have multiple entry files and *each * requires ``jquery ``. In this
55case, *each * output file will contain jQuery, slowing down your user's experience.
6- In this case , you can *extract * these common libraries to a "shared" entry file
7- that's included on every page:
6+ To solve this , you can *extract * the common libraries to a "shared" entry file
7+ that's included on every page.
88
9- .. code-block :: javascript
9+ Suppose you already have an entry called ``app `` that's included on every page.
10+ Update your code to use ``createSharedEntry() ``:
11+
12+ .. code-block :: diff
1013
1114 Encore
1215 // ...
13- .addEntry (' page1' , ' assets/js/page1.js' )
14- .addEntry (' page2' , ' assets/js/page2.js' )
15-
16- // this creates a 'vendor.js' file with jquery and the bootstrap JS module
17- // these modules will *not* be included in page1.js or page2.js anymore
18- .createSharedEntry (' vendor' , [
19- ' jquery' ,
20- ' bootstrap' ,
21-
22- // you can also extract CSS - this will create a 'vendor.css' file
23- // this CSS will *not* be included in page1.css or page2.css anymore
24- ' bootstrap/scss/bootstrap.scss'
25- ])
16+ - .addEntry('app', 'assets/js/app.js')
17+ + .createSharedEntry('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')
2621
27- As soon as you make this change, you need to include two extra JavaScript files
28- on your page before any other JavaScript file :
22+ As soon as you make this change, you need to include * one * extra JavaScript file
23+ in your layout, * before * `` app.js `` :
2924
3025.. _encore-shared-entry-script :
3126
3227.. code-block :: twig
3328
34- <!-- these two files now must be included in every page -->
29+ {# templates/base.html.twig #}
30+ <!-- these two files now must be included on every page -->
3531 <script src="{{ asset('build/manifest.js') }}"></script>
36- <script src="{{ asset('build/vendor.js') }}"></script>
37-
38- <!-- here you link to the specific JS files needed by the current page -->
3932 <script src="{{ asset('build/app.js') }}"></script>
4033
41- <!-- if you extracted some CSS, include vendor.css -->
42- <link rel="stylesheet" href="{{ asset('build/vendor.css') }}" />
34+ <!-- you can still include page-specific JavaScript, like normal -->
35+ <script src="{{ asset('build/store.js') }}"></script>
36+
37+ <!-- continue including app.css on every page -->
38+ <link rel="stylesheet" href="{{ asset('build/app.css') }}" />
39+
40+ Before making this change, if both ``app.js `` and ``store.js `` require ``jquery ``,
41+ then ``jquery `` would be packaged into *both * files, which is wasteful. By making
42+ ``app.js `` your "shard" entry, *any * code required by ``app.js `` (like jQuery) will
43+ *no longer * be packaged into any other files. The same is true for any CSS.
4344
44- The ``vendor.js `` file contains all the common code that has been extracted from
45- the other files, so it's obvious that it must be included. The other file (``manifest.js ``)
46- is less obvious: it's needed so that Webpack knows how to load those shared modules.
45+ Because ``app.js `` contains all the common code that other entry files depend on,
46+ it's obvious that its script (and link) tag must be on every page. The other file
47+ (``manifest.js ``) is less obvious: it's needed so that Webpack knows how to load
48+ these shared modules.
4749
4850.. tip ::
4951
50- The ``vendor .js `` file works best when its contents are changed *rarely *
52+ The ``app .js `` file works best when its contents are changed *rarely *
5153 and you're using :ref: `long-term caching <encore-long-term-caching >`. Why?
52- If ``vendor .js `` contains application code that *frequently * changes, then
54+ If ``app .js `` contains application code that *frequently * changes, then
5355 (when using versioning), its filename hash will frequently change. This means
5456 your users won't enjoy the benefits of long-term caching for this file (which
5557 is generally quite large).
0 commit comments