我正在开发一个Play 2.4应用程序,并希望SBT在编译期间运行webpack来生成静态资产.
我试过https://github.com/stejskal/sbt-webpack,但它似乎对我不起作用.
我设法通过定义一个自定义sbt任务来使它工作,该任务在打包应用程序时使用了依赖关系dist
和stage
调用的任务.
任务的代码是直截了当的:
lazy val webpack = taskKey[Unit]("Run webpack when packaging the application")
def runWebpack(file: File) = {
Process("webpack", file) !
}
webpack := {
if(runWebpack(baseDirectory.value) != 0) throw new Exception("Something goes wrong when running webpack.")
}
dist <<= dist dependsOn webpack
stage <<= stage dependsOn webpack
在开发模式下,我使用play action hookswebpack watch
在代码更改时运行:
PlayKeys.playRunHooks <+= baseDirectory.map(Webpack.apply)
与Webpack
定义如下:
import java.net.InetSocketAddress
import play.sbt.PlayRunHook
import sbt._
object Webpack {
def apply(base: File): PlayRunHook = {
object WebpackHook extends PlayRunHook {
var process: Option[Process] = None
override def beforeStarted() = {
process = Option(
Process("webpack", base).run()
)
}
override def afterStarted(addr: InetSocketAddress) = {
process = Option(
Process("webpack --watch", base).run()
)
}
override def afterStopped() = {
process.foreach(_.destroy())
process = None
}
}
WebpackHook
}
}
它就像一个魅力.您可以在我的github帐户中找到使用此技术的示例游戏项目:https://github.com/nouhoum/play-react-webpack/blob/master/webpack.sbt
我希望这有帮助 ;-)