Assetic Symfony2 less+compress filter

user2742648 picture user2742648 · Jan 5, 2013 · Viewed 8.1k times · Source

Is there a way to make assetic automatically compile and compress .less files?

I tried this config:

assetic:
debug:          "%kernel.debug%"
use_controller: true
bundles:        ['BloggerBlogBundle', "FOSCommentBundle"]
#java: /usr/bin/java
filters:
    cssrewrite: ~
    less:
      node: /home/igor/nvm/v0.8.16/bin/node
      node_paths: [/home/igor/nvm/bin/node_modules]
      apply_to: "\.less$"
    yui_css:
      jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar
      apply_to: "\.css$|\.less"

But this results in .css files getting compressed and .less files getting compiled. I can't make it do both: compile and then compress my .less files.

Answer

guillaumepotier picture guillaumepotier · Jan 5, 2013

Here is my config and twig code for compiling + compressing .less files in the same time:

config.yml

# Assetic Configuration
assetic:
    debug:          %kernel.debug%
    use_controller: false
    java: %java_path%
    filters:
        less:
            node:       %node_base_path%
            node_paths: [%node_lib_path%, %node_modules_path%]
        cssrewrite: ~
        yui_css:
            jar: %kernel.root_dir%%jar_file%
        yui_js:
            jar: %kernel.root_dir%%jar_file%

In my twig:

{% block stylesheets %}
    {% stylesheets
        '@MyBundle/Resources/public/css/event.less'
        'css/colorpicker.css'
        filter='less,?yui_css'
        output='build/event_layout_2cols.css'
    %}
        <link href="{{ asset_url }}" rel="stylesheet" media="screen" />
    {% endstylesheets %}
{% endblock %}

And here is the command to both compile AND compress:

./app/console assetic:dump --env=prod --no-debug

Hope that help!