Developed with love by KnpLabs Hire us for your project!
64

webpack-encore-bundle

by symfony

WebpackEncoreBundle: Symfony integration with Webpack Encore!

This bundle allows you to use the splitEntryChunks() feature
from Webpack Encore
by reading an entrypoints.json file and helping you render all of
the dynamic script and link tags needed.

Install the bundle with:

composer require symfony/webpack-encore-bundle

Configuration

If you're using Symfony Flex, you're done! The recipe will
pre-configure everything you need in the config/packages/webpack_encore.yaml
file:

# config/packages/webpack_encore.yaml
webpack_encore:
    # The path where Encore is building the assets - i.e. Encore.setOutputPath()
    output_path: '%kernel.public_dir%/build'

Usage

First, enable the "Split Chunks" functionality in Webpack Encore:

// webpack.config.js
// ...
    .setOutputPath('public/build/')
    .setPublicPath('/build')
    .setManifestKeyPrefix('build/')

    .addEntry('entry1', './assets/some_file.js')

+   .splitEntryChunks()
// ...

When you enable splitEntryChunks(), instead of just needing 1 script tag
for entry1.js and 1 link tag for entry1.css, you may now need multiple
script and link tags. This is because Webpack "splits" your files
into smaller pieces for greater optimization.

To help with this, Encore writes a entrypoints.json file that contains
all of the files needed for each "entry".

For example, to render all of the script and link tags for a specific
"entry" (e.g. entry1), you can:

{# any template or base layout where you need to include a JavaScript entry #}

{% block javascripts %}
    {{ parent() }}

    {{ encore_entry_script_tags('entry1') }}
{% endblock %}

{% block stylesheets %}
    {{ parent() }}

    {{ encore_entry_link_tags('entry1') }}
{% endblock %}

Assuming that entry1 required two files to be included - build/vendor~entry1~entry2.js
and build/entry1.js, then encore_entry_script_tags() is equivalent to:

<script src="{{ asset('build/vendor~entry1~entry2.js') }}"></script>
<script src="{{ asset('build/entry1.js') }}"></script>

If you want more control, you can use the encore_entry_js_files() and
encore_entry_css_files() methods to get the list of files needed, then
loop and create the script and link tags manually.

Copyright (c) 2004-2018 Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
  • minor #15 Don't self-close link tag (Seb33300)
    By weaverryan, 24 days ago
  • Don't self-close link tag
    By weaverryan, 24 days ago
  • feature #6 Create interfaces to make EntryLookup and ManifestLookup are interchangable (codayblue, weaverryan)
    By weaverryan, 1 month ago
  • Minor cleanup
    By weaverryan, 1 month ago
  • Made the interfaces
    By , 1 month ago
  • minor #9 Removing ManifestLookup (weaverryan)
    By weaverryan, 1 month ago
  • Removing ManifestLookup
    By weaverryan, 1 month ago
  • minor #11 Read data from an "entrypoints" key instead of the root of the entrypoints.json file (Lyrkan)
    By weaverryan, 1 month ago
  • feature #7 Add a dependency on symfony/asset (weaverryan)
    By weaverryan, 1 month ago
  • Put current data under an "entrypoints" key in the entrypoints.json file
    By , 1 month ago
  • Add a dependency on symfony/asset
    By weaverryan, 1 month ago
  • Updating phpcs to fix license headers
    By weaverryan, 1 month ago
  • bug #5 Fix the issue where keys might be missing if no files are found by webpack plugin (codayblue)
    By weaverryan, 1 month ago
  • Altered the entry point lookup to handle if a key is not found in the entry point file
    By codayblue, 1 month ago
  • feature #1 Initial bundle setup & feature implementation (weaverryan)
    By weaverryan, 1 month ago
  • Initial bundle setup & feature implementation
    By weaverryan, 1 month ago
  • Adding the license
    By weaverryan, 3 months ago