Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Frontend
  4. Encore
  5. Preventing Duplication by "Splitting" Shared Code with Webpack Encore

Preventing Duplication by "Splitting" Shared Code with Webpack Encore

Edit this page

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
11
// webpack.config.js
  Encore
      // ...

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

+     .splitEntryChunks()

Now, each output file (e.g. homepage.js) may be split into multiple file (e.g. homepage.js & vendors-node_modules_jquery_dist_jquery_js.js - the filename of the second will be less obvious when you build for production). 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" defer></script>
        <script src="/build/vendors-node_modules_jquery_dist_jquery_js.js" defer></script>
        <script src="/build/homepage.js" defer></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
9
// webpack.config.js
  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.
TOC
    Version

    Symfony 7.1 is backed by

    Check Code Performance in Dev, Test, Staging & Production

    Check Code Performance in Dev, Test, Staging & Production

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).

    Version:
    • Controlling how Assets are Split

    Symfony footer

    Avatar of Neil Peyssard, a Symfony contributor

    Thanks Neil Peyssard (@nepey) for being a Symfony contributor

    4 commits • 498 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?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • 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

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony