Search code examples
playframeworksbtjshint

Play Framework 2.3.4: How to Run JSHint as a Part of the Compile Task


I've a multi-project application and I want to make JSHint run as a part of the compile task. Here below is how I configured my project:

1) Added the JSHint plugin to myApp/project/plugins.sbt:

...

addSbtPlugin("com.typesafe.sbt" % "sbt-jshint" % "1.0.1")

2) Enabed it (SbtWeb) in myApp/build.sbt:

...

lazy val apidocs = project.in(file("modules/apidocs")).enablePlugins(play.PlayScala, SbtWeb).settings(
  javaOptions in Test += "-Dconfig.resource=apidocs-application.conf"
).dependsOn(
  common % "test->test;compile->compile"
)

I've also tried to run the assets task... but it looks like JSHint don't get invoked. How can I make JSHint run as a part of the compile task? Maybe an even better option would be to modify myApp/projects/Build.scala and let JSHint compress any *.js in any subproject.


Solution

  • First off, I'm assuming you're using this version of sbt-jshint (there seem to be at least 3 popular versions floating around).

    The general gist of this is that we need to have a task that is triggered by compilation. This is pretty easily adaptable from this answer. So we add the following to build.sbt:

    val triggeredTask = taskKey[Seq[sbt.File]]("Triggered by compile")
    
    triggeredTask <<= Def.task {
      JshintKeys.jshint.value
    }.triggeredBy(compile in Compile)
    

    Things to note here are that the taskKey Type is not unit like the answer I linked to, but the type of the jshint task. Second thing to note here is the weirdness of referencing jshint. JshintKeys.jshint versus just calling jshint. I kept getting a "error: not found: value jshint" until I did this. If you look in the source you'll see that JshintKeys is an object inside an object. I don't know if that's standard for writing sbt plugins, but I know it makes this neccesary. Last, the .value is how we say that our triggered task depends on another task.

    Once you have that in place, you should see the compilation step trigger jshint :)