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
uglifyJS2filter to the embedded scripts:Step 1: Create a Twig
TokenParserandNodeStep 2: Add the
Uglifierclass, which is used by theNodeto pass the content to the AsseticUglifyJS2FilterStep 3: Create
Uglifieras sService, pass it to theTwig Extensionand implement custom tag within the extensionStep 4: Wrap embedded scripts with the new `{% uglify %} tag
DONE