Minify client through Play Framework

2020-07-25 05:24发布

问题:

How can I minify JS & CSS through play-framework?

'Google Closure Compiler' is no longer an option since it was removed.

I am using play Activator 2.3.7

Note: This question is not duplicate since Google Closure Compiler was removed from activator 2.3.7.

回答1:

Play 2.4 is the same.

  • Add empty main.js to public folder

  • plugins.sbt:

      addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.7")
    
  • build.sbt:

      pipelineStages := Seq(rjs)
    
  • Generate production executable:

      $activator stage
    
  • Run results:

      ./target/universal/stage/bin/<yourproject>
    

Note: when running activator stage you should see it output the process of minifying/uglifying:

...

[info] Tracing dependencies for: main

[info] Uglify2 file: /path/to/your/project/public/something.js

...



回答2:

You can use RequireJS. It is documented here, but basically is just to add the following in the build.sbt

pipelineStages := Seq(rjs)

Then add this line to the file project/plugins.sbt

addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.7")

As RequireJS website states

Combines related scripts together into build layers and minifies them via UglifyJS (the default) or Closure Compiler (an option when using Java).

Optimizes CSS by inlining CSS files referenced by @import and removing comments.