2016-01-02 116 views

回答

28

我設法通過定義一個自定義sbt任務來工作,該任務在打包應用程序時調用diststage任務。

任務的代碼是直截了當:

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 hooks運行webpack 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

我希望這有助於;-)

1

Nouhoum的回答是工作的罰款,直到我更新到SBT 1.0.x的

短短數需要更新:

import scala.sys.process.Process 

lazy val webpack = taskKey[Unit]("Run webpack when packaging the application") 

def runWebpack(file: File) = { 
    Process("npm run build", file) ! 
} 

webpack := { 
    if(runWebpack(baseDirectory.value) != 0) throw new Exception("Something went wrong when running webpack.") 
} 

dist := (dist dependsOn webpack).value 

stage := (stage dependsOn webpack).value