Symfony
sponsored by SensioLabs
Menu
  • About
  • Documentation
  • Screencasts
  • Cloud
  • Certification
  • Community
  • Businesses
  • News
  • Download
  1. Home
  2. Documentation
  3. Frontend
  4. Preventing Duplication by "Splitting" Shared Code into Separate Files
  • Documentation
  • Book
  • Reference
  • Bundles
  • Cloud
Search by Algolia
  • Controlling how Assets are Split

Preventing Duplication by "Splitting" Shared Code into Separate Files

Edit this page

Warning: You are browsing the documentation for Symfony 4.3, which is no longer maintained.

Read the updated version of this page for Symfony 6.2 (the current stable version).

Preventing Duplication by "Splitting" Shared Code into Separate Files

Suppose you have multiple entry files and each requires jquery. In this case, each output file will contain jQuery, making your files much larger than necessary. To solve this, you can ask webpack to analyze your files and split them into additional files, which will contain "shared" code.

To enable this, call splitEntryChunks():

1
2
3
4
5
6
7
8
9
10
Encore
    // ...

    // multiple entry files, which probably import the same code
    .addEntry('app', './assets/js/app.js')
    .addEntry('homepage', './assets/js/homepage.js')
    .addEntry('blog', './assets/js/blog.js')
    .addEntry('store', './assets/js/store.js')

+     .splitEntryChunks()

Now, each output file (e.g. homepage.js) may be split into multiple file (e.g. homepage.js, vendor~homepage.js). This means that you may need to include multiple script tags (or link tags for CSS) in your template. Encore creates an entrypoints.json file that lists exactly which CSS and JavaScript files are needed for each entry.

If you're using the encore_entry_link_tags() and encore_entry_script_tags() Twig functions from WebpackEncoreBundle, you don't need to do anything else! These functions automatically read this file and render as many script or link tags as needed:

1
2
3
4
5
6
7
{#
    May now render multiple script tags:
        <script src="/build/runtime.js"></script>
        <script src="/build/homepage.js"></script>
        <script src="/build/vendor~homepage.js"></script>
#}
{{ encore_entry_script_tags('homepage') }}

Controlling how Assets are Split

The logic for when and how to split the files is controlled by the SplitChunksPlugin from Webpack. You can control the configuration passed to this plugin with the configureSplitChunks() function:

1
2
3
4
5
6
7
8
Encore
    // ...

    .splitEntryChunks()
+     .configureSplitChunks(function(splitChunks) {
+         // change the configuration
+         splitChunks.minSize = 0;
+     })
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
We stand with Ukraine.
Version:
Online exam, become Symfony certified today

Online exam, become Symfony certified today

Check Code Performance in Dev, Test, Staging & Production

Check Code Performance in Dev, Test, Staging & Production

↓ Our footer now uses the colors of the Ukrainian flag because Symfony stands with the people of Ukraine.

Avatar of Zac Sturgess, a Symfony contributor

Thanks Zac Sturgess (@zsturgess) for being a Symfony contributor

3 commits • 71 lines changed

View all contributors that help us make Symfony

Become a Symfony contributor

Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

Learn how to contribute

Symfony™ is a trademark of Symfony SAS. All rights reserved.

  • What is Symfony?
    • Symfony at a Glance
    • Symfony Components
    • Case Studies
    • Symfony Releases
    • Security Policy
    • Logo & Screenshots
    • Trademark & Licenses
    • symfony1 Legacy
  • Learn Symfony
    • Symfony Docs
    • Symfony Book
    • Reference
    • Bundles
    • Best Practices
    • Training
    • eLearning Platform
    • Certification
  • Screencasts
    • Learn Symfony
    • Learn PHP
    • Learn JavaScript
    • Learn Drupal
    • Learn RESTful APIs
  • Community
    • SymfonyConnect
    • Support
    • How to be Involved
    • Code of Conduct
    • Events & Meetups
    • Projects using Symfony
    • Downloads Stats
    • Contributors
    • Backers
  • Blog
    • Events & Meetups
    • A week of symfony
    • Case studies
    • Cloud
    • Community
    • Conferences
    • Diversity
    • Documentation
    • Living on the edge
    • Releases
    • Security Advisories
    • SymfonyInsight
    • Twig
    • SensioLabs
  • Services
    • SensioLabs services
    • Train developers
    • Manage your project quality
    • Improve your project performance
    • Host Symfony projects
    Deployed on
Follow Symfony
Search by Algolia