@@ -5,46 +5,60 @@ How to Minify CSS/JS Files (using UglifyJs and UglifyCss)
5
5
=========================================================
6
6
7
7
`UglifyJs `_ is a javascript parser/compressor/beautifier toolkit. It can be used
8
- to combine and minify javascript assets so they need less HTTP requests and makes
9
- the website load faster. `UglifyCss `_ is a css compressor/beautifier much like
10
- ` UglifyJs ` .
8
+ to combine and minify javascript assets so that they require less HTTP requests
9
+ and make your site load faster. `UglifyCss `_ is a css compressor/beautifier
10
+ that is very similar to UglifyJs.
11
11
12
- In this cookbook, the installation, configuration and usage of `UglifyJs ` is shown
13
- in detail. `UglifyCss ` works pretty much the same way and is only talked about briefly.
12
+ In this cookbook, the installation, configuration and usage of UglifyJs is
13
+ shown in detail. ``UglifyCss `` works pretty much the same way and is only
14
+ talked about briefly.
14
15
15
16
Install UglifyJs
16
17
----------------
17
18
18
- UglifyJs is build as an node.js npm module and can be installed using npm. First,
19
- you need to `install node.js `_. Afterwards you can install UglifyJs using npm:
19
+ UglifyJs is available as an `Node.js `_ npm module and can be installed using
20
+ npm. First, you need to `install node.js `_. Afterwards you can install UglifyJs
21
+ using npm:
20
22
21
23
.. code-block :: bash
22
24
23
- $ npm install -g uglify-js@1
24
-
25
+ $ npm install -g uglify-js
26
+
27
+ This command will install UglifyJs globally and you may need to run it as
28
+ a root user.
29
+
25
30
.. note ::
26
31
27
- It's also possible to install UglifyJs for your symfony project only. To do this,
28
- install it without the ``-g `` option and specify the path where to put the module:
29
-
32
+ It's also possible to install UglifyJs inside your project only. To do
33
+ this, install it without the ``-g `` option and specify the path where
34
+ to put the module:
35
+
30
36
.. code-block :: bash
31
-
32
- $ npm install uglify-js@1 /path/to/symfony/app/Resources
33
-
37
+
38
+ $ cd /path/to/symfony
39
+ $ mkdir app/Resources/node_modules
40
+ $ npm install uglify-js --prefix app/Resources
41
+
34
42
It is recommended that you install UglifyJs in your ``app/Resources `` folder
35
- and add the ``node_modules `` folder to version control.
36
-
37
- .. tip ::
38
-
39
- This cookbook uses UglifyJs 1 instead of the newer version 2 to be compatible
40
- with old assetic versions. If you want to use UglifyJs version 2, make sure
41
- to also use the assetic filter for this version and apply the correct configuration.
43
+ and add the ``node_modules `` folder to version control. Alternatively,
44
+ you can create an npm `package.json `_ file and specify your dependencies
45
+ there.
46
+
47
+ Depending on your installation method, you should either be able to execute
48
+ the ``uglifyjs `` executable globally, or execute the physical file that lives
49
+ in the ``node_modules `` directory:
50
+
51
+ .. code-block :: bash
52
+
53
+ $ uglifyjs --help
42
54
43
- Configure the UglifyJs Filter
44
- -----------------------------
55
+ $ ./app/Resources/node_modules/.bin/uglifyjs --help
45
56
46
- Now we need to configure symfony2 to use the UglifyJs Filter when processing your
47
- stylesheets:
57
+ Configure the uglifyjs2 Filter
58
+ ------------------------------
59
+
60
+ Now we need to configure Symfony2 to use the ``uglifyjs2 `` filter when processing
61
+ your javascripts:
48
62
49
63
.. configuration-block ::
50
64
@@ -53,15 +67,16 @@ stylesheets:
53
67
# app/config/config.yml
54
68
assetic :
55
69
filters :
56
- uglifyjs :
70
+ uglifyjs2 :
71
+ # the path to the uglifyjs executable
57
72
bin : /usr/local/bin/uglifyjs
58
73
59
74
.. code-block :: xml
60
75
61
76
<!-- app/config/config.xml -->
62
77
<assetic : config >
63
78
<assetic : filter
64
- name =" uglifyjs "
79
+ name =" uglifyjs2 "
65
80
bin =" /usr/local/bin/uglifyjs" />
66
81
</assetic : config >
67
82
@@ -70,7 +85,7 @@ stylesheets:
70
85
// app/config/config.php
71
86
$container->loadFromExtension('assetic', array(
72
87
'filters' => array(
73
- 'uglifyjs ' => array(
88
+ 'uglifyjs2 ' => array(
74
89
'bin' => '/usr/local/bin/uglifyjs',
75
90
),
76
91
),
@@ -92,7 +107,7 @@ stylesheets:
92
107
If you installed UglifyJs locally, you can find the bin folder inside
93
108
the ``node_modules `` folder. It's called ``.bin `` in this case.
94
109
95
- You now have access to the ``uglifyjs `` Filter in your application.
110
+ You now have access to the ``uglifyjs2 `` filter in your application.
96
111
97
112
Minify your Assets
98
113
------------------
@@ -104,15 +119,15 @@ your assets are a part of the view layer, this work is done in your templates:
104
119
105
120
.. code-block :: html+jinja
106
121
107
- {% javascripts '@AcmeFooBundle/Resources/public/js/*' filter='uglifyjs ' %}
122
+ {% javascripts '@AcmeFooBundle/Resources/public/js/*' filter='uglifyjs2 ' %}
108
123
<script src="{{ asset_url }}"></script>
109
124
{% endjavascripts %}
110
125
111
126
.. code-block :: html+php
112
127
113
128
<?php foreach ($view['assetic']->javascripts(
114
129
array('@AcmeFooBundle/Resources/public/js/*'),
115
- array('uglifyjs ')
130
+ array('uglifyj2s ')
116
131
) as $url): ?>
117
132
<script src="<?php echo $view->escape($url) ?>"></script>
118
133
<?php endforeach; ?>
@@ -121,16 +136,55 @@ your assets are a part of the view layer, this work is done in your templates:
121
136
122
137
The above example assumes that you have a bundle called ``AcmeFooBundle ``
123
138
and your JavaScript files are in the ``Resources/public/js `` directory under
124
- your bundle. This isn't important however - you can include your Javascript
139
+ your bundle. This isn't important however - you can include your JavaScript
125
140
files no matter where they are.
126
141
127
- With the addition of the ``uglifyjs `` filter to the asset tags above, you should
128
- now see minified JavaScripts coming over the wire much faster.
142
+ With the addition of the ``uglifyjs2 `` filter to the asset tags above, you
143
+ should now see minified JavaScripts coming over the wire much faster.
144
+
145
+ Disable Minification in Debug Mode
146
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
147
+
148
+ Minified JavaScripts are very difficult to read, let alone debug. Because of
149
+ this, Assetic lets you disable a certain filter when your application is in
150
+ debug (e.g. ``app_dev.php ``) mode. You can do this by prefixing the filter name
151
+ in your template with a question mark: ``? ``. This tells Assetic to only
152
+ apply this filter when debug mode is off (e.g. ``app.php ``):
153
+
154
+ .. configuration-block ::
155
+
156
+ .. code-block :: html+jinja
157
+
158
+ {% javascripts '@AcmeFooBundle/Resources/public/js/*' filter='?uglifyjs2' %}
159
+ <script src="{{ asset_url }}"></script>
160
+ {% endjavascripts %}
161
+
162
+ .. code-block :: html+php
163
+
164
+ <?php foreach ($view['assetic']->javascripts(
165
+ array('@AcmeFooBundle/Resources/public/js/*'),
166
+ array('?uglifyjs2')
167
+ ) as $url): ?>
168
+ <script src="<?php echo $view->escape($url) ?>"></script>
169
+ <?php endforeach; ?>
170
+
171
+ To try this out, switch to your ``prod `` environment (``app.php ``). But before
172
+ you do, don't forget to :ref: `clear your cache<book-page-creation-prod-cache-clear> `
173
+ and :ref: `dump your assetic assets<cookbook-asetic-dump-prod> `.
174
+
175
+ .. tip ::
176
+
177
+ Instead of adding the filter to the asset tags, you can also globally
178
+ enable it by adding the apply-to attribute to the filter configuration, for
179
+ example in the ``uglifyjs2 `` filter ``apply_to: "\.js$" ``. To only have
180
+ the filter applied in production, add this to the ``config_prod `` file
181
+ rather than the common config file. For details on applying filters by
182
+ file extension, see :ref: `cookbook-assetic-apply-to `.
129
183
130
184
Install, configure and use UglifyCss
131
185
------------------------------------
132
186
133
- The usage of ` UglifyCss ` works the same way as ` UglifyJs ` . First, make sure
187
+ The usage of UglifyCss works the same way as UglifyJs. First, make sure
134
188
the node package is installed:
135
189
136
190
.. code-block :: bash
@@ -169,8 +223,8 @@ Next, add the configuration for this filter:
169
223
),
170
224
));
171
225
172
- To use the filter for your css files, make sure to use the assetics helper in
173
- your template :
226
+ To use the filter for your css files, add the filter to the Assetic `` stylesheets ``
227
+ helper :
174
228
175
229
.. configuration-block ::
176
230
@@ -189,42 +243,12 @@ your template:
189
243
<link rel="stylesheet" href="<?php echo $view->escape($url) ?>" />
190
244
<?php endforeach; ?>
191
245
192
- Disable Minification in Debug Mode
193
- ----------------------------------
194
-
195
- Minified JavaScripts are very difficult to read, let alone
196
- debug. Because of this, Assetics lets you disable a certain filter when your
197
- application is in debug mode. You can do this by prefixing the filter name
198
- in your template with a question mark: ``? ``. This tells Assetics to only
199
- apply this filter when debug mode is off.
200
-
201
- .. configuration-block ::
202
-
203
- .. code-block :: html+jinja
204
-
205
- {% javascripts '@AcmeFooBundle/Resources/public/js/*' filter='?uglifyjs' %}
206
- <script src="{{ asset_url }}"></script>
207
- {% endjavascripts %}
208
-
209
- .. code-block :: html+php
210
-
211
- <?php foreach ($view['assetic']->javascripts(
212
- array('@AcmeFooBundle/Resources/public/js/*'),
213
- array('?uglifyjs')
214
- ) as $url): ?>
215
- <script src="<?php echo $view->escape($url) ?>"></script>
216
- <?php endforeach; ?>
217
-
218
- .. tip ::
219
-
220
- Instead of adding the filter to the asset tags, you can also globally
221
- enable it by adding the apply-to attribute to the filter configuration, for
222
- example in the ``uglifyjs `` filter ``apply_to: "\.js$" ``. To only have the filter
223
- applied in production, add this to the ``config_prod `` file rather than the
224
- common config file. For details on applying filters by file extension,
225
- see :ref: `cookbook-assetic-apply-to `.
226
-
246
+ Just like with the ``uglifyjs2 `` filter, if you prefix the filter name with
247
+ ``? `` (i.e. ``?uglifycss ``), the minification will only happen when you're
248
+ not in debug mode.
227
249
228
250
.. _`UglifyJs` : https://github.com/mishoo/UglifyJS
229
251
.. _`UglifyCss` : https://github.com/fmarcia/UglifyCSS
252
+ .. _`Node.js` : http://nodejs.org/
230
253
.. _`install node.js` : http://nodejs.org/
254
+ .. _`package.json` : http://package.json.nodejitsu.com/
0 commit comments