Full usage guide
Because CoffeeScript is an external project, we won't attempt to reproduce its documentation here. Go to the official documentation; you'll be glad you did!
Set up CoffeeScript in Scalatra
Install the plugin by adding the dependency to your
resolvers += Resolver.url("sbt-plugin-snapshots", new URL("http://scalasbt.artifactoryonline.com/scalasbt/sbt-plugin-snapshots/"))( Resolver.ivyStylePatterns) addSbtPlugin("com.bowlingx" %% "xsbt-wro4j-plugin" % "0.1.0-SNAPSHOT")
Add these imports to the very top of your
import com.bowlingx.sbt.plugins.Wro4jPlugin._ import Wro4jKeys._
Now enable to plugin by adding these lines to your
build.sbt, after all your imports.
Don't attempt to remove the blank line!
seq(wro4jSettings: _*) (webappResources in Compile) <+= (targetFolder in generateResources in Compile)
Unfortunately, the plugin we're using does require a bit of configuration.
Create the file
src/main/webapp/WEB-INF/wro.xml, telling the compiler where our
CoffeeScript files are:
<?xml version="1.0" encoding="UTF-8"?> <groups xmlns="http://www.isdc.ro/wro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.isdc.ro/wro wro.xsd" > <group name="scripts"> <js>/coffee/*.coffee</js> </group> </groups>
Also create the file
src/main/webapp/WEB-INF/wro.properties, requesting CoffeeScript
preProcessors = coffeeScript postProcessors =
With installation out of the way, you're free to put your CoffeeScript files in
It doesn't matter what the file/s are called, so long as they have the
extension we said we'd use in the
For example, create
src/main/webapp/less/main.coffee, which shows off the
optionality of parentheses and semicolons in CoffeeScript:
alert "Hello CoffeeScript!"
That file will be compiled to one called
compiled/scripts.js, so you can refer to it
in your app like this:
See it in action
Now compile and run your app as usual. If all has gone well, and you've used the above example, an annoying popup should launch on page load.
$ cd scalatra-coffeescript $ ./sbt > jetty:start > browse
browse doesn't launch your browser, manually open
http://localhost:8080/ in your browser.
The resources processor we've been using, wro4j, is extensively configurable. See the wro4j documentation for more information.