how to minify js produced by sbt-concat

1.8k views Asked by At

I was doing my experiments using sbt-concat to combine css/js bundles. I then tried to expand my pipeline in order to do minify for my css and js. It is fairly easy to use sbt-css-compress which does minification of all the CSS produced by sbt-concat. I tried sbt-closure for js, but it does not seem to work. Just to be clear, I tried sbt-closure and it is confirmed that it does compilation of all js below to /assets. However I am trying to figure out if it works with sbt-concat.

For example: if I have /assets/js/f1.js and /assets/js/f2.js and I would like to combine them and minify into 1 bundle file.

2

There are 2 answers

0
dwickern On

You can use sbt-uglify to combine and minify.

Add to plugins.sbt:

addSbtPlugin("com.typesafe.sbt" % "sbt-uglify" % "1.0.3")

Add to build.sbt:

pipelineStages := Seq(uglify)

UglifyKeys.uglifyOps := { js =>
  Seq((js.sortBy(_._2), "main.min.js"))
}

This will combine your javascript sources alphabetically by their file path.

Use pipelineStages in Assets if you want to concat/uglify in development. Normally it would only execute for a production build.

2
Yoel Garcia On

One way that I know is to do the concat in 'pipelineStages in Assests' and include a filter

For example in build.sbt

Concat.groups := Seq(
  "all.js" -> group(Seq(
    "js/f1.js",
    "js/f2.js"
  ))
)

Concat.parentDir := "concated"

Closure.suffix := ".min.js"

Closure.flags := Seq("--formatting=PRETTY_PRINT", "--accept_const_keyword")

pipelineStages in Assets := Seq(concat)

includeFilter in closure := "all.js"

pipelineStages := Seq(closure, digest, gzip)