Skip to content
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

Assetic/uglifyjs cookbook article #2406

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cookbook/assetic/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ Assetic

asset_management
yuicompressor
uglifyjs
jpeg_optimize
apply_to_option
164 changes: 164 additions & 0 deletions cookbook/assetic/uglifyjs.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
.. index::
single: Assetic; UglifyJs

How to Minify JavaScripts with UglifyJs
=======================================
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should rename this entry so that it's more like an example of using a minification filter, where we use ugllifyjs as an example:

How to Minify CSS/JS Files (using UglifyJs and UglifyCss)

Then we can make some slight changes to make this entry a bit more general:

  • An intro paragraph that talks about how you can use different filters to minify CSS and JS files. We could mention the other filters available to do this: yui css, yui js (both deprecated), Closure, cssmin, jsmin, jsminplus.
  • Add a section about using UglifyCss. But here, we can skip most of the details - i.e. we don't need to really show detailed install, since we've shown for UglifyJS. We can also have a shorter configuration section.
  • Tweak the "Disable Minification in Debug Mode" section to use UglifyJs as an example, but talk more generally about how this can be used on any filter.


`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.

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 wantt to use UglifyJs version 2, make sure
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Sgoettschkes There is a small typo with 'wantt'

to also use the assetic filter for this version and apply the correct configuration.
Copy link
Member

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).


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.

Disable Minification in Debug Mode
----------------------------------

Minified JavaScripts 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.

.. 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
.. _`install node.js`: http://nodejs.org/
1 change: 1 addition & 0 deletions cookbook/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* :doc:`/cookbook/assetic/asset_management`
* :doc:`/cookbook/assetic/yuicompressor`
* :doc:`/cookbook/assetic/uglifyjs`
* :doc:`/cookbook/assetic/jpeg_optimize`
* :doc:`/cookbook/assetic/apply_to_option`

Expand Down