Assetic Symfony2 less+compress filter
Asked Answered
P

1

5

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.

Perplexed answered 5/1, 2013 at 10:3 Comment(0)
B
9

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!

Beriberi answered 5/1, 2013 at 12:25 Comment(1)
I was struggling with this until I realised you need to clear the cache for prod before running assetic:dump. After that, the above method worked for me.Shaven

© 2022 - 2024 — McMap. All rights reserved.