Compressing linked JavaScript
and CSS
files is no big deal when using Assetic
in my Symfony 2.8
project. But what about scripts that are directly embedded in the page using a script
tag. These scripts are not modified in any way.
Is it possible to compress/modify/uglify these scripts as well?
Of course I could simply move these scripts into separat files and thus apply the Assetic
filters to them as well. But in some cases it is just handy to have the scripts directly within the HTML / Twig template.
So, is there any existing solution to filter these scripts without moving them?
Since I found no solution for this, I finally managed to create my own: Add a custom tag to my (existing) Twig extension to apply Assetics
uglifyJS2
filter to the embedded scripts:Step 1: Create a Twig
TokenParser
andNode
Step 2: Add the
Uglifier
class, which is used by theNode
to pass the content to the AsseticUglifyJS2Filter
Step 3: Create
Uglifier
as sService, pass it to theTwig Extension
and implement custom tag within the extensionStep 4: Wrap embedded scripts with the new `{% uglify %} tag
DONE