summaryrefslogtreecommitdiff
path: root/scalajslib/src/main/scala/mill/scalajslib/ScalaJSModule.scala
blob: 03c4e216a37bca9ed80e0cf70feeb697f7638178 (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
package mill
package scalajslib

import java.io.File

import ammonite.ops.Path
import mill.eval.Result.Success
import mill.scalajslib.Lib._
import mill.scalalib.Lib.resolveDependencies
import mill.scalalib.{Dep, Module, PublishModule, TestModule}
import mill.util.Loose

trait ScalaJSModule extends scalalib.Module { outer =>

  def scalaJSVersion: T[String]

  private val ReleaseVersion = raw"""(\d+)\.(\d+)\.(\d+)""".r
  private val MinorSnapshotVersion = raw"""(\d+)\.(\d+)\.([1-9]\d*)-SNAPSHOT""".r

  def scalaJSBinaryVersion = T{
    scalaJSVersion() match {
      case ReleaseVersion(major, minor, _) => s"$major.$minor"
      case MinorSnapshotVersion(major, minor, _) => s"$major.$minor"
      case _ => scalaJSVersion()
    }
  }

  def scalaJSBridgeVersion = T{ scalaJSVersion().split('.').dropRight(1).mkString(".") }

  def scalaJSLinkerClasspath: T[Loose.OSet[PathRef]] = T{
    val jsBridgeKey = "MILL_SCALAJS_BRIDGE_" + scalaJSBridgeVersion().replace('.', '_')
    val jsBridgePath = sys.props(jsBridgeKey)
    if (jsBridgePath != null) {
      Success(
        Loose.OSet.from(
          jsBridgePath.split(File.pathSeparator).map(f => PathRef(Path(f), quick = true))
        )
      )
    }
    else {
      val dep = scalaJSLinkerIvyDep(scalaJSBridgeVersion())
      resolveDependencies(
        repositories,
        scalaVersion(),
        scalaBinaryVersion(),
        Seq(dep)
      )
    }
  }

  def fastOpt = T{
    val linker = scalaJSLinkerBridge(scalaJSLinkerClasspath().map(_.path))
    link(mainClass(), Seq(compile().classes.path), compileDepClasspath().map(_.path), linker, FastOpt)
  }

  def fullOpt = T{
    val linker = scalaJSLinkerBridge(scalaJSLinkerClasspath().map(_.path))
    link(mainClass(), Seq(compile().classes.path), compileDepClasspath().map(_.path), linker, FullOpt)
  }

  override def scalacPluginIvyDeps = T{ Loose.OSet(Dep.Point("org.scala-js", "scalajs-compiler", scalaJSVersion())) }

  override def ivyDeps = T{ Loose.OSet(Dep("org.scala-js", "scalajs-library", scalaJSVersion())) }

  // publish artifact with name "mill_sjs0.6.4_2.12" instead of "mill_sjs0.6_2.12"
  def crossFullScalaJSVersion: T[Boolean] = false
  def artifactScalaJSVersion: T[String] = T {
    if (crossFullScalaJSVersion()) scalaJSVersion()
    else scalaJSBinaryVersion()
  }

  override def artifactId: T[String] = T { s"${artifactName()}_sjs${artifactScalaJSVersion()}_${artifactScalaVersion()}" }

}

trait TestScalaJSModule extends ScalaJSModule with TestModule