-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Assetic/uglifyjs cookbook article #2406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d2ed217
Adding a first draft for the uglifyjs filter
Sgoettschkes 10fa57b
Making minor changes suggested by @WouterJ
Sgoettschkes 40be7db
Fixing npm install for uglifyjs (wrong command, wrong version)
Sgoettschkes b5a4fdf
Fixing typo
Sgoettschkes 4cf68f2
Adding UglifyCss and changing some wording
Sgoettschkes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,6 @@ Assetic | |
|
||
asset_management | ||
yuicompressor | ||
uglifyjs | ||
jpeg_optimize | ||
apply_to_option |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
.. index:: | ||
single: Assetic; UglifyJs | ||
|
||
How to Minify CSS/JS Files (using UglifyJs and UglifyCss) | ||
========================================================= | ||
|
||
`UglifyJs`_ is a javascript parser/compressor/beautifier toolkit. It can be used | ||
to combine and minify javascript assets so they need less HTTP requests and makes | ||
the website load faster. `UglifyCss`_ is a css compressor/beautifier much like | ||
`UglifyJs`. | ||
|
||
In this cookbook, the installation, configuration and usage of `UglifyJs` is shown | ||
in detail. `UglifyCss` works pretty much the same way and is only talked about briefly. | ||
|
||
Install UglifyJs | ||
---------------- | ||
|
||
UglifyJs is build as an node.js npm module and can be installed using npm. First, | ||
you need to `install node.js`_. Afterwards you can install UglifyJs using npm: | ||
|
||
.. code-block:: bash | ||
|
||
$ npm install -g uglify-js@1 | ||
|
||
.. note:: | ||
|
||
It's also possible to install UglifyJs for your symfony project only. To do this, | ||
install it without the ``-g`` option and specify the path where to put the module: | ||
|
||
.. code-block:: bash | ||
|
||
$ npm install uglify-js@1 /path/to/symfony/app/Resources | ||
|
||
It is recommended that you install UglifyJs in your ``app/Resources`` folder | ||
and add the ``node_modules`` folder to version control. | ||
|
||
.. tip:: | ||
|
||
This cookbook uses UglifyJs 1 instead of the newer version 2 to be compatible | ||
with old assetic versions. If you want to use UglifyJs version 2, make sure | ||
to also use the assetic filter for this version and apply the correct configuration. | ||
|
||
Configure the UglifyJs Filter | ||
----------------------------- | ||
|
||
Now we need to configure symfony2 to use the UglifyJs Filter when processing your | ||
stylesheets: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/config.yml | ||
assetic: | ||
filters: | ||
uglifyjs: | ||
bin: /usr/local/bin/uglifyjs | ||
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/config.xml --> | ||
<assetic:config> | ||
<assetic:filter | ||
name="uglifyjs" | ||
bin="/usr/local/bin/uglifyjs" /> | ||
</assetic:config> | ||
|
||
.. code-block:: php | ||
|
||
// app/config/config.php | ||
$container->loadFromExtension('assetic', array( | ||
'filters' => array( | ||
'uglifyjs' => array( | ||
'bin' => '/usr/local/bin/uglifyjs', | ||
), | ||
), | ||
)); | ||
|
||
.. note:: | ||
|
||
The path where UglifyJs is installed may vary depending on your system. | ||
To find out where npm stores the ``bin`` folder, you can use the following | ||
command: | ||
|
||
.. code-block:: bash | ||
|
||
$ npm bin -g | ||
|
||
It should output a folder on your system, inside which you should find | ||
the UglifyJs executable. | ||
|
||
If you installed UglifyJs locally, you can find the bin folder inside | ||
the ``node_modules`` folder. It's called ``.bin`` in this case. | ||
|
||
You now have access to the ``uglifyjs`` Filter in your application. | ||
|
||
Minify your Assets | ||
------------------ | ||
|
||
In order to use UglifyJs on your assets, you need to apply it to them. Since | ||
your assets are a part of the view layer, this work is done in your templates: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: html+jinja | ||
|
||
{% javascripts '@AcmeFooBundle/Resources/public/js/*' filter='uglifyjs' %} | ||
<script src="{{ asset_url }}"></script> | ||
{% endjavascripts %} | ||
|
||
.. code-block:: html+php | ||
|
||
<?php foreach ($view['assetic']->javascripts( | ||
array('@AcmeFooBundle/Resources/public/js/*'), | ||
array('uglifyjs') | ||
) as $url): ?> | ||
<script src="<?php echo $view->escape($url) ?>"></script> | ||
<?php endforeach; ?> | ||
|
||
.. note:: | ||
|
||
The above example assumes that you have a bundle called ``AcmeFooBundle`` | ||
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 ``uglifyjs`` filter to the asset tags above, you should | ||
now see minified JavaScripts coming over the wire much faster. | ||
|
||
Install, configure and use UglifyCss | ||
------------------------------------ | ||
|
||
The usage of `UglifyCss` works the same way as `UglifyJs`. First, make sure | ||
the node package is installed: | ||
|
||
.. code-block:: bash | ||
|
||
$ npm install -g uglifycss | ||
|
||
Next, add the configuration for this filter: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/config.yml | ||
assetic: | ||
filters: | ||
uglifycss: | ||
bin: /usr/local/bin/uglifycss | ||
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/config.xml --> | ||
<assetic:config> | ||
<assetic:filter | ||
name="uglifycss" | ||
bin="/usr/local/bin/uglifycss" /> | ||
</assetic:config> | ||
|
||
.. code-block:: php | ||
|
||
// app/config/config.php | ||
$container->loadFromExtension('assetic', array( | ||
'filters' => array( | ||
'uglifycss' => array( | ||
'bin' => '/usr/local/bin/uglifycss', | ||
), | ||
), | ||
)); | ||
|
||
To use the filter for your css files, make sure to use the assetics helper in | ||
your template: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: html+jinja | ||
|
||
{% javascripts '@AcmeFooBundle/Resources/public/css/*' filter='uglifycss' %} | ||
<link rel="stylesheet" href="{{ asset_url }}" /> | ||
{% endjavascripts %} | ||
|
||
.. code-block:: html+php | ||
|
||
<?php foreach ($view['assetic']->javascripts( | ||
array('@AcmeFooBundle/Resources/public/css/*'), | ||
array('uglifycss') | ||
) as $url): ?> | ||
<link rel="stylesheet" href="<?php echo $view->escape($url) ?>" /> | ||
<?php endforeach; ?> | ||
|
||
Disable Minification in Debug Mode | ||
---------------------------------- | ||
|
||
Minified JavaScripts are very difficult to read, let alone | ||
debug. Because of this, Assetics 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 Assetics to only | ||
apply this filter when debug mode is off. | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: html+jinja | ||
|
||
{% javascripts '@AcmeFooBundle/Resources/public/js/*' filter='?uglifyjs' %} | ||
<script src="{{ asset_url }}"></script> | ||
{% endjavascripts %} | ||
|
||
.. code-block:: html+php | ||
|
||
<?php foreach ($view['assetic']->javascripts( | ||
array('@AcmeFooBundle/Resources/public/js/*'), | ||
array('?uglifyjs') | ||
) as $url): ?> | ||
<script src="<?php echo $view->escape($url) ?>"></script> | ||
<?php endforeach; ?> | ||
|
||
.. 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 ``uglifyjs`` 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 :ref:`cookbook-assetic-apply-to`. | ||
|
||
|
||
.. _`UglifyJs`: https://github.com/mishoo/UglifyJS | ||
.. _`UglifyCss`: https://github.com/fmarcia/UglifyCSS | ||
.. _`install node.js`: http://nodejs.org/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we merge this into the 2.2 branch, I think we'll just update this entry to show Uglify2 (since that's where the assetic config first showed up).