How to Minify JavaScripts and Stylesheets with YUI Compressor
Warning: You are browsing the documentation for Symfony 2.x, which is no longer maintained.
Read the updated version of this page for Symfony 7.1 (the current stable version).
Caution
The YUI Compressor is no longer maintained by Yahoo. That's why you are strongly advised to avoid using YUI utilities unless strictly necessary. Read How to Minify CSS/JS Files (Using UglifyJS and UglifyCSS) for a modern and up-to-date alternative.
Caution
Starting from Symfony 2.8, Assetic is no longer included by default in the Symfony Standard Edition. Refer to this article to learn how to install and enable Assetic in your Symfony application.
Yahoo! provides an excellent utility for minifying JavaScripts and stylesheets so they travel over the wire faster, the YUI Compressor. Thanks to Assetic, you can take advantage of this tool very easily.
Download the YUI Compressor JAR
The YUI Compressor is written in Java and distributed as a JAR. Download the JAR
from the Yahoo! website and save it to app/Resources/java/yuicompressor.jar
.
Configure the YUI Filters
Now you need to configure two Assetic filters in your application, one for minifying JavaScripts with the YUI Compressor and one for minifying stylesheets:
1 2 3 4 5 6 7 8
# app/config/config.yml
assetic:
# java: '/usr/bin/java'
filters:
yui_css:
jar: '%kernel.root_dir%/Resources/java/yuicompressor.jar'
yui_js:
jar: '%kernel.root_dir%/Resources/java/yuicompressor.jar'
Note
Windows users need to remember to update config to proper Java location.
In Windows7 x64 bit by default it's C:
.
You now have access to two new Assetic filters in your application:
yui_css
and yui_js
. These will use the YUI Compressor to minify
stylesheets and JavaScripts, respectively.
Minify your Assets
You have YUI Compressor configured now, but nothing is going to happen until you apply one of these filters to an asset. Since your assets are a part of the view layer, this work is done in your templates:
1 2 3
{% javascripts '@AppBundle/Resources/public/js/*' filter='yui_js' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
Note
The above example assumes that you have a bundle called AppBundle and your
JavaScript files are in the Resources/public/js
directory under your
bundle. This isn't important however - you can include your JavaScript
files no matter where they are.
With the addition of the yui_js
filter to the asset tags above, you should
now see minified JavaScripts coming over the wire much faster. The same process
can be repeated to minify your stylesheets.
1 2 3
{% stylesheets '@AppBundle/Resources/public/css/*' filter='yui_css' %}
<link rel="stylesheet" type="text/css" media="screen" href="{{ asset_url }}" />
{% endstylesheets %}
Disable Minification in Debug Mode
Minified JavaScripts and stylesheets are very difficult to read, let alone
debug. Because of this, Assetic lets you disable a certain filter when your
application is in debug mode. You can do this by prefixing the filter name
in your template with a question mark: ?
. This tells Assetic to only
apply this filter when debug mode is off.
1 2 3
{% javascripts '@AppBundle/Resources/public/js/*' filter='?yui_js' %}
<script src="{{ asset_url }}"></script>
{% endjavascripts %}
Tip
Instead of adding the filter to the asset tags, you can also globally
enable it by adding the apply_to
attribute to the filter configuration, for
example in the yui_js
filter apply_to: "\.js$"
. To only have the filter
applied in production, add this to the config_prod
file rather than the
common config file. For details on applying filters by file extension,
see How to Apply an Assetic Filter to a specific File Extension.