Kevin Barasa
Kevin Barasa

Reputation: 71

Set up invocation of browserify by sbt in build.sbt so that dependencies are bundled on compilation

The scenario, I'm building with Java Play Framework for play-java. I have a dependency that babel compiles to a nasty output line of "var sth = require("sth")". Apparently browserify is the only hope of getting this nodejs style inclusion to work in the browser. But I don't want to manually call browserify from the CLI on every new compilation. So I found this snippet https://www.toptal.com/scala/using-scala-js-with-npm-and-browserify that is closer to home, but his case was for Scalajs. So how can I replicate his solution in my scenario?

Upvotes: 1

Views: 172

Answers (1)

Jaie Wilson
Jaie Wilson

Reputation: 147

You can execute browserify from sbt by creating a task that executes during the Asset source generation phase.

See this seed project as an example, take a look at the build.sbt for how to create the task: https://github.com/maximebourreau/play-reactjs-es6-seed

Note they deprecated the <+= for appending to the list so you can change the sourceGenerators line to:

sourceGenerators in Assets += browserifyTask.taskValue

Note this is just executing the browserify binary in the node_modules directory so you have to npm install browserify and running the bablify task but you could change what tasks it executes.

It has also hardcoded the file to browerify app/assets/javascripts/main.jsx. You could change this to be a settingKey as well.

The source generators will run anytime you change a file that is in the sourceDirectory in Assets.

Hope this helps.

Upvotes: 3

Related Questions