knpuniversity / webpack-encore-bundle
Integration with your Symfony app & Webpack Encore!
Installs: 38
Dependents: 0
Suggesters: 0
Security: 0
Stars: 10
Watchers: 8
Forks: 2
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^5.5.9|>=7.0.8
- symfony/config: ^3.4 || ^4.0
- symfony/dependency-injection: ^3.4 || ^4.0
- symfony/http-kernel: ^3.4 || ^4.0
Requires (Dev)
- symfony/asset: ^3.4 || ^4.0
- symfony/framework-bundle: ^3.4 || ^4.0
- symfony/phpunit-bridge: ^3.4 || ^4.0
- symfony/twig-bundle: ^3.4 || ^4.0
- twig/twig: ^1.35 || ^2.0
This package is not auto-updated.
Last update: 2022-02-01 13:14:05 UTC
README
WARNING This bundle IS NOT MAINTAINED anymore, use symfony/webpack-encore-bundle instead!!!
This bundle allows you to use the splitEntryChunks()
feature
from Webpack Encore
by reading an entrypoins.json
file and helping you render all of
the dynamic script
and link
tags needed.
Install the bundle with:
composer require knpuniversity/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' # The public prefix to your assets that you normally use with the asset() function (e.g. build/) - # should match the "setManifestKeyPrefix()" value in webpack.config.js, if set. asset_path_prefix: '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 - vendor~entry1~entry2.js
and 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>
The build/
public prefix to your assets is set in the config file.
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.