aboutsummaryrefslogtreecommitdiff
path: root/plugins/scalajs/ScalaJs.scala
blob: 6a19d0568a01b18b3be3a328833510b4bbdd9a3b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import java.io.File

import cbt._


trait ScalaJsSbtDependencyDsl extends SbtDependencyDsl { self: ScalaJsBuild =>

  //Note: We make same assumption about scala version.
  //In order to be able to choose different scala version, one has to use %.
  implicit class ScalaJsDependencyBuilder(groupId: String){
    def %%%(artifactId: String) = new DependencyBuilder2(
      groupId, artifactId + artifactIdSuffix, Some(scalaMajorVersion))
  }
}

trait ScalaJsBuild extends BaseBuild with ScalaJsSbtDependencyDsl with ScalaJsInformation { outer =>

  def sharedFolder = projectDirectory ++ "/shared"
  def jvmFolder = projectDirectory ++ "/jvm"
  def jsFolder = projectDirectory ++ "/js"

  private lazy val jvmBuild = new BasicBuild(outer.context){
    override def sources = Seq(sharedFolder ++ "/src/main/scala", jvmFolder ++ "/src/main/scala")
    override def target = jvmFolder ++ "/target"
    override def dependencies = outer.dependencies ++ jvmDependencies
  }
  private lazy val jsBuild = new BasicBuild(outer.context){
    override def sources = Seq(sharedFolder ++ "/src/main/scala", jsFolder ++ "/src/main/scala")
    override def target = jsFolder ++ "/target"
    override def dependencies = outer.dependencies :+ scalaJsLibDep
    override def scalacOptions = super.scalacOptions ++
      Seq(s"-Xplugin:${scalaJsCompilerDep.jar.getAbsolutePath}", "-Xplugin-require:scalajs")
  }

  def jvmDependencies = Seq.empty[Dependency]
  //TODO: implement
  def jsDependencies = Seq.empty[Dependency]
  def jvmCompile: Option[File] = jvmBuild.compile
  def jsCompile: Option[File] = jsBuild.compile
  override def compile = {
    jvmCompile
    jsCompile
  }

  trait JsOutputMode {
    def option: String
    def fileSuffix: String
  }
  case object FastOptJS extends JsOutputMode{
    override val option = "--fastOpt"
    override val fileSuffix = "fastopt"
  }
  case object FullOptJS extends JsOutputMode{
    override val option = "--fullOpt"
    override val fileSuffix = "fullopt"
  }

  private def output(mode: JsOutputMode) = s"${jsBuild.target.getAbsolutePath}/$projectName-${mode.fileSuffix}.js"

  //TODO: should process all options that Scalajsld recognizes?
  private def link(mode: JsOutputMode, outputPath: String) = {
    lib.runMain(
      "org.scalajs.cli.Scalajsld",
      Seq(
        mode.option,
        "--sourceMap",
        "--stdlib", s"${scalaJsLibDep.jar.getAbsolutePath}",
        "--output", outputPath
        jsBuild.target.getAbsolutePath) ++
        jsBuild.dependencies.collect{case d: BoundMavenDependency => d.jar.getAbsolutePath},
      scalaJsCliDep.classLoader(jsBuild.context.classLoaderCache))
  }
  def fastOptJS = link(FastOptJS, fastOptOutput)
  def fullOptJS = link(FullOptJS, fullOptOutput)
  def fastOptOutput: String = output(FastOptJS)
  def fullOptOutput: String = output(FullOptJS)
}