### Install and Run Psalm
Source: https://github.com/symfony/webpack-encore-bundle/blob/2.x/CONTRIBUTING.md
Install Psalm in the tools directory and run static analysis.
```bash
composer install --working-dir=tools/psalm
tools/psalm/vendor/bin/psalm
```
--------------------------------
### Install Webpack Encore Bundle
Source: https://github.com/symfony/webpack-encore-bundle/blob/2.x/doc/index.rst
Install the bundle using Composer.
```terminal
$ composer require symfony/webpack-encore-bundle
```
--------------------------------
### Install Webpack Encore Bundle
Source: https://context7.com/symfony/webpack-encore-bundle/llms.txt
Install the bundle using Composer. Symfony Flex will automatically generate the configuration file.
```bash
composer require symfony/webpack-encore-bundle
```
--------------------------------
### Install and Run PHPStan
Source: https://github.com/symfony/webpack-encore-bundle/blob/2.x/CONTRIBUTING.md
Install PHPStan in the tools directory and run static analysis. Use the --generate-baseline flag to update the baseline file if needed.
```bash
composer install --working-dir=tools/phpstan
tools/phpstan/vendor/bin/phpstan analyze
# Based on the results, you may want to update the baseline
tools/phpstan/vendor/bin/phpstan analyze --generate-baseline
```
--------------------------------
### Configure Remote Entrypoints.json for CDN/SSR
Source: https://context7.com/symfony/webpack-encore-bundle/llms.txt
Configure Webpack Encore to fetch `entrypoints.json` from a remote URL, such as a CDN. This requires setting `output_path` to `false` and defining a `builds` entry with the remote URL. Ensure `symfony/http-client` is installed.
```yaml
webpack_encore:
output_path: false # disable default build
builds:
cdn: 'https://cdn.example.com/assets/build'
# entrypoints.json is fetched from:
# https://cdn.example.com/assets/build/entrypoints.json
```
--------------------------------
### Get CSS Files Manually with encore_entry_css_files()
Source: https://context7.com/symfony/webpack-encore-bundle/llms.txt
Use `encore_entry_css_files()` to retrieve raw CSS file paths for an entry without rendering HTML. This allows for manual control over tag generation.
```twig
{% set cssFiles = encore_entry_css_files('app') %}
{% for file in cssFiles %}
{% endfor %}
{# cssFiles example value:
['/build/vendors~app.css', '/build/app.css']
#}
```
--------------------------------
### HTML Output for Script Tags
Source: https://github.com/symfony/webpack-encore-bundle/blob/2.x/doc/index.rst
Example of the generated HTML script tags when using encore_entry_script_tags() with split entry chunks enabled.
```html+twig
```
--------------------------------
### Enable HTTP/2 Preload Headers
Source: https://context7.com/symfony/webpack-encore-bundle/llms.txt
Configure Webpack Encore to automatically add `Link` preload headers to HTTP responses for all rendered scripts and stylesheets. This requires setting `preload: true` in `webpack_encore.yaml` and ensuring `symfony/web-link` is installed.
```yaml
webpack_encore:
output_path: '%kernel.project_dir%/public/build'
preload: true
```
--------------------------------
### Get JS Files Manually with encore_entry_js_files()
Source: https://context7.com/symfony/webpack-encore-bundle/llms.txt
Use `encore_entry_js_files()` to get raw JavaScript file paths for an entry without rendering HTML. This is useful for manual tag rendering.
```twig
{# Get file list and render tags manually #}
{% set jsFiles = encore_entry_js_files('app') %}
{% for file in jsFiles %}
{% endfor %}
{# From a specific build #}
{% set adminFiles = encore_entry_js_files('dashboard', 'admin') %}
{# adminFiles => ['/admin/build/runtime.js', '/admin/build/dashboard.js'] #}
```
--------------------------------
### Configure Webpack Encore Bundle
Source: https://context7.com/symfony/webpack-encore-bundle/llms.txt
Configure the bundle in `webpack_encore.yaml`. The `output_path` must match Encore's configuration.
```yaml
webpack_encore:
# Required: path where Encore writes its build output
output_path: '%kernel.project_dir%/public/build'
# Global attributes added to every
{% endfor %}
{# From a specific build #}
{% set adminFiles = encore_entry_js_files('dashboard', 'admin') %}
{# adminFiles => ['/admin/build/runtime.js', '/admin/build/dashboard.js'] #}
```
```
--------------------------------
### Configure Webpack Encore Bundle
Source: https://github.com/symfony/webpack-encore-bundle/blob/2.x/doc/index.rst
Default configuration for the Webpack Encore Bundle in Symfony Flex. Customize output path, attributes, and build configurations.
```yaml
# config/packages/webpack_encore.yaml
webpack_encore:
# The path where Encore is building the assets - i.e. Encore.setOutputPath()
# if you customize this, you will also need to change framework.assets.json_manifest_path (it usually lives in assets.yaml)
output_path: '%kernel.project_dir%/public/build'
# If multiple builds are defined (as shown below), you can disable the default build:
# output_path: false
# Set attributes that will be rendered on all script and link tags
script_attributes:
defer: true
# referrerpolicy: origin
# link_attributes:
# referrerpolicy: origin
# if using Encore.enableIntegrityHashes() and need the crossorigin attribute (default: false, or use 'anonymous' or 'use-credentials')
# crossorigin: 'anonymous'
# preload all rendered script and link tags automatically via the http2 Link header
# preload: true
# Throw an exception if the entrypoints.json file is missing or an entry is missing from the data
# strict_mode: false
# if you have multiple builds:
# builds:
# frontend: '%kernel.project_dir%/public/frontend/build'
# or if you use a CDN:
# frontend: 'https://cdn.example.com/frontend/build'
# pass the build name" as the 3rd argument to the Twig functions
# {{ encore_entry_script_tags('entry1', null, 'frontend') }}
# Cache the entrypoints.json (rebuild Symfony's cache when entrypoints.json changes)
# Available in version 1.2
# Put in config/packages/prod/webpack_encore.yaml
# cache: true
```
--------------------------------
### Render Link Tags with Twig
Source: https://context7.com/symfony/webpack-encore-bundle/llms.txt
Use `encore_entry_link_tags()` in Twig to render all stylesheet link tags for a given entry point. Merges global and per-call attributes, and supports non-default builds.
```twig
{# base.html.twig #}
{% block stylesheets %}
{{ parent() }}
{# Render all tags for the 'app' entry #}
{{ encore_entry_link_tags('app') }}
{# With extra attributes per call #}
{{ encore_entry_link_tags('app', null, '_default', { media: 'print' }) }}
{# From a non-default build #}
{{ encore_entry_link_tags('dashboard', null, 'admin') }}
{% endblock %}
```
--------------------------------
### RenderAssetTagEvent
Source: https://context7.com/symfony/webpack-encore-bundle/llms.txt
This event is dispatched for each `
// '
$linkHtml = $tagRenderer->renderWebpackLinkTags('app');
// => ''
// Inspect what was already rendered (e.g., to build preload lists)
$renderedScripts = $tagRenderer->getRenderedScripts();
// => ['/build/runtime.js', '/build/app.js']
$renderedStyles = $tagRenderer->getRenderedStyles();
// => ['/build/app.css']
// Get rendered scripts including all attributes
$scriptsWithAttrs = $tagRenderer->getRenderedScripts(includeAttributes: true);
// => [['src' => '/build/runtime.js', 'defer' => true, 'data-controller' => 'app'], ...]
return $this->render('page/index.html.twig', [
'scriptTags' => $scriptHtml,
'linkTags' => $linkHtml,
]);
}
}
```
--------------------------------
### TagRenderer Service
Source: https://context7.com/symfony/webpack-encore-bundle/llms.txt
The TagRenderer service provides low-level control for rendering `
// '
$linkHtml = $tagRenderer->renderWebpackLinkTags('app');
// => ''
// Inspect what was already rendered (e.g., to build preload lists)
$renderedScripts = $tagRenderer->getRenderedScripts();
// => ['/build/runtime.js', '/build/app.js']
$renderedStyles = $tagRenderer->getRenderedStyles();
// => ['/build/app.css']
// Get rendered scripts including all attributes
$scriptsWithAttrs = $tagRenderer->getRenderedScripts(includeAttributes: true);
// => [['src' => '/build/runtime.js', 'defer' => true, 'data-controller' => 'app'], ...]
return $this->render('page/index.html.twig', [
'scriptTags' => $scriptHtml,
'linkTags' => $linkHtml,
]);
}
}
```
```
--------------------------------
### Render JavaScript Entry Tags in Twig
Source: https://github.com/symfony/webpack-encore-bundle/blob/2.x/doc/index.rst
Use the encore_entry_script_tags() Twig function to render all necessary script tags for a given entry. Supports custom attributes.
```twig
{# any template or base layout where you need to include a JavaScript entry #}
{% block javascripts %}
{{ parent() }}
{{ encore_entry_script_tags('entry1') }}
{# or render a custom attribute #}
{#
{{ encore_entry_script_tags('entry1', attributes={
defer: true
}) }}
#}
{% endblock %}
```
--------------------------------
### Modify Asset Tags with RenderAssetTagEvent
Source: https://context7.com/symfony/webpack-encore-bundle/llms.txt
Subscribe to the RenderAssetTagEvent to dynamically add, modify, or remove attributes from script and link tags before they are rendered. This is useful for tasks like injecting CSP nonces or correcting inherited attributes.
```php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\WebpackEncoreBundle\Event\RenderAssetTagEvent;
class CspNonceSubscriber implements EventSubscriberInterface
{
public function __construct(private string $nonce) {}
public static function getSubscribedEvents(): array
{
return [RenderAssetTagEvent::class => 'onRenderAssetTag'];
}
public function onRenderAssetTag(RenderAssetTagEvent $event): void
{
if ($event->isScriptTag()) {
// Add a CSP nonce to every