当前位置:  开发笔记 > 编程语言 > 正文

如何从SBT运行webpack

如何解决《如何从SBT运行webpack》经验,为你挑选了1个好方法。

我正在开发一个Play 2.4应用程序,并希望SBT在编译期间运行webpack来生成静态资产.

我试过https://github.com/stejskal/sbt-webpack,但它似乎对我不起作用.



1> 小智..:

我设法通过定义一个自定义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 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

我希望这有帮助 ;-)

推荐阅读
php
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有