scala-js 如何与 sbt-web 集成? [英] How can scala-js integrate with sbt-web?

查看:46
本文介绍了scala-js 如何与 sbt-web 集成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 scala-jssbt-web 以这样一种方式,它可以被编译以生成添加到资产管道的 javascript 资产(例如 gzip、digest).我知道 lihaoyi 的工作台项目,但我认为这不会影响资产管道.这两个项目如何集成为一个sbt-web插件?

I would like to use scala-js with sbt-web in such a way that it can be compiled to produce javascript assets that are added to the asset pipeline (e.g. gzip, digest). I am aware of lihaoyi's workbench project but I do not believe this affects the asset pipeline. How can these two projects be integrated as an sbt-web plugin?

推荐答案

Scala-js 从 Scala 文件生成 js 文件.sbt-web 文档将此称为 源文件任务.

Scala-js produces js files from Scala files. The sbt-web documentation calls this a Source file task.

结果看起来像这样:

val compileWithScalaJs = taskKey[Seq[File]]("Compiles with Scala js")

compileWithScalaJs := {
  // call the correct compilation function / task on the Scala.js project
  // copy the resulting javascript files to webTarget.value / "scalajs-plugin"
  // return a list of those files
}

sourceGenerators in Assets <+= compileWithScalaJs

<小时>

编辑

创建插件需要做更多的工作.Scala.js 还不是 AutoPlugin,所以你可能会遇到一些依赖问题.

To create a plugin involves a bit more work. Scala.js is not yet an AutoPlugin, so you might have some problems with dependencies.

第一部分是将 Scala.js 库添加为插件的依赖项.你可以通过使用这样的代码来做到这一点:

The first part is that you add the Scala.js library as a dependency to your plugin. You can do that by using code like this:

libraryDependencies += Defaults.sbtPluginExtra(
  "org.scala-lang.modules.scalajs" % "scalajs-sbt-plugin" % "0.5.5", 
  (sbtBinaryVersion in update).value, 
  (scalaBinaryVersion in update).value
)

你的插件看起来像这样:

Your plugin would look something like this:

object MyScalaJsPlugin extends AutoPlugin {
  /* 
    Other settings like autoImport and requires (for the sbt web dependency), 
    see the link above for more information
  */

  def projectSettings = scalaJSSettings ++ Seq(
    // here you add the sourceGenerators in Assets implementation
    // these settings are scoped to the project, which allows you access 
    // to directories in the project as well
  )
}

然后在使用此插件的项目中,您可以执行以下操作:

Then in a project that uses this plugin you can do:

lazy val root = project.in( file(".") ).enablePlugins(MyScalaJsPlugin)

这篇关于scala-js 如何与 sbt-web 集成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆