Friday 24 February 2012

Get Eclipse to automatically compile your Sass

Using the Aptana plugin for Eclipse, I was able to get syntax highlighting working in Sass files (*.sass and now *.scss). It also allows you to compile a Sass file at the click of a button, which is great, but what we all really want is automatic compilation every time we make save a change.

Automatic compilation can be done using the sass --watch command, but this requires you to set it up every time you start working on a project. In Eclipse, I couldn't find an existing way to automate the compile process, so I wrote an ant build script to do it, and then set that as a Project Builder and set it to run on "Auto Build", meaning every time a change is saved.

1) The ant build script. Put this in a file called build.xml in the root of your project. It just says to run sass (/var/lib/gems/1.9.1/bin/sass) on the directory "scss" and output the result to the directory "css". Don't forget to update the path to your sass executable, as well as your css and scss directories.


    
        
            <srcfile/>
            <targetfile/>
            <fileset dir="scss" includes="*.scss"/>
            <mapper from="*.scss" to="*.css" type="glob"/>
        
    

Update: bare in mind you can add an <arg> element inside the element in order to send any additional arguments to Sass e.g. <arg value="--style=compressed"/> will output compressed CSS (useful for your production server).

2) Add it as a Project Builder: in the project properties, click Builders, then New, and select Ant Builder. Give it a name, and select the Buildfile (click Browse Workspace and navigate to your build.xml). Under Targets, click the Set Targets button next to Auto Build, and select sass and click OK. Finally, under Build Options, check "Specify working set of relevant resources" and click the button to specify only your scss directory - this ensures the build script will only be run when you change your Sass files.

6 comments:

  1. Above ant target is not quite right. It should be:








       
           
               
               
               
               
           
       

    ReplyDelete
  2. Looks like there is a problem with the formatter!

    ReplyDelete
  3. Thank you for spotting this - unfortunately the formatter is not accepting self-closing elements. I've now implemented a workaround (using HTML character codes for less-than and greater-than signs).

    ReplyDelete
  4. Need some help with this. New to sass in general and have never used ant. I get this error when it executes the xml.


    srcfile doesn't support the nested "targetfile" element.

    ReplyDelete
  5. Well I just found that my sass executable is nested a little more than yours on my system but that didn't help either.

    ReplyDelete
  6. Michaƫl Picovschi4 March 2013 at 10:20

    What about adding a builder with sass executable and "--update scss css" option ? Did the trick for me

    ReplyDelete