Skip to content

Commit a6ff937

Browse files
committed
Have sbt-dotty correctly configure Scala.js projects.
This also implies changes in the build for JS projects, since they will now get the configuration for JS from `DottyPlugin`.
1 parent 0222b4e commit a6ff937

File tree

6 files changed

+69
-12
lines changed

6 files changed

+69
-12
lines changed

project/Build.scala

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,6 @@ object MyScalaJSPlugin extends AutoPlugin {
3636
override def projectSettings: Seq[Setting[_]] = Def.settings(
3737
commonBootstrappedSettings,
3838

39-
/* Remove the Scala.js compiler plugin for scalac, and enable the
40-
* Scala.js back-end of dotty instead.
41-
*/
42-
libraryDependencies := {
43-
val deps = libraryDependencies.value
44-
deps.filterNot(_.name.startsWith("scalajs-compiler")).map(_.withDottyCompat(scalaVersion.value))
45-
},
46-
scalacOptions += "-scalajs",
47-
4839
// Replace the JVM JUnit dependency by the Scala.js one
4940
libraryDependencies ~= {
5041
_.filter(!_.name.startsWith("junit-interface"))
@@ -781,6 +772,8 @@ object Build {
781772
asDottyLibrary(Bootstrapped).
782773
enablePlugins(MyScalaJSPlugin).
783774
settings(
775+
libraryDependencies +=
776+
("org.scala-js" %% "scalajs-library" % scalaJSVersion).withDottyCompat(scalaVersion.value),
784777
unmanagedSourceDirectories in Compile :=
785778
(unmanagedSourceDirectories in (`dotty-library-bootstrapped`, Compile)).value,
786779

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package test
2+
3+
import scala.scalajs.js
4+
import org.scalajs.dom
5+
6+
object Main {
7+
def main(args: Array[String]): Unit = {
8+
println("Hello Scala.js")
9+
dom.console.log("using a Scala.js dependency")
10+
}
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
enablePlugins(ScalaJSPlugin)
2+
3+
scalaVersion := sys.props("plugin.scalaVersion")
4+
5+
// Test withDottyCompat for %%% dependencies
6+
libraryDependencies += ("org.scala-js" %%% "scalajs-dom" % "1.1.0").withDottyCompat(scalaVersion.value)
7+
8+
scalaJSUseMainModuleInitializer := true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % sys.props("plugin.version"))
2+
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.1.1")

sbt-dotty/sbt-test/scalajs/basic/test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
> run

sbt-dotty/src/dotty/tools/sbtplugin/DottyPlugin.scala

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import scala.util.Properties.isJavaAtLeast
1616
object DottyPlugin extends AutoPlugin {
1717
object autoImport {
1818
val isDotty = settingKey[Boolean]("Is this project compiled with Dotty?")
19+
val isDottyJS = settingKey[Boolean]("Is this project compiled with Dotty and Scala.js?")
1920

2021
// NOTE:
2122
// - this is a def to support `scalaVersion := dottyLatestNightlyBuild`
@@ -169,6 +170,12 @@ object DottyPlugin extends AutoPlugin {
169170
override def projectSettings: Seq[Setting[_]] = {
170171
Seq(
171172
isDotty := scalaVersion.value.startsWith("0.") || scalaVersion.value.startsWith("3."),
173+
isDottyJS := {
174+
isDotty.value && (crossVersion.value match {
175+
case binary: librarymanagement.Binary => binary.prefix.contains("sjs1_")
176+
case _ => false
177+
})
178+
},
172179

173180
scalaOrganization := {
174181
if (isDotty.value)
@@ -317,12 +324,47 @@ object DottyPlugin extends AutoPlugin {
317324

318325
// Because managedScalaInstance is false, sbt won't add the standard library to our dependencies for us
319326
libraryDependencies ++= {
320-
if (isDotty.value && autoScalaLibrary.value)
321-
Seq(scalaOrganization.value %% "dotty-library" % scalaVersion.value)
322-
else
327+
if (isDotty.value && autoScalaLibrary.value) {
328+
val name =
329+
if (isDottyJS.value) "dotty-library_sjs1"
330+
else "dotty-library"
331+
Seq(scalaOrganization.value %% name % scalaVersion.value)
332+
} else
323333
Seq()
324334
},
325335

336+
// Patch up some more options if this is Dotty with Scala.js
337+
scalacOptions ++= {
338+
if (isDottyJS.value) Seq("-scalajs")
339+
else Nil
340+
},
341+
libraryDependencies := {
342+
val prev = libraryDependencies.value
343+
if (!isDottyJS.value) {
344+
prev
345+
} else {
346+
prev
347+
/* Remove the dependencies we don't want:
348+
* * We don't want scalajs-library, because we need the one that comes
349+
* as a dependency of dotty-library_sjs1
350+
* * We don't want scalajs-compiler, because that's a compiler plugin,
351+
* which is replaced by the `-scalajs` flag in dotc.
352+
*/
353+
.filterNot { moduleID =>
354+
moduleID.organization == "org.scala-js" && (
355+
moduleID.name == "scalajs-library" || moduleID.name == "scalajs-compiler"
356+
)
357+
}
358+
// Apply withDottyCompat to the dependency on scalajs-test-bridge
359+
.map { moduleID =>
360+
if (moduleID.organization == "org.scala-js" && moduleID.name == "scalajs-test-bridge")
361+
moduleID.withDottyCompat(scalaVersion.value)
362+
else
363+
moduleID
364+
}
365+
}
366+
},
367+
326368
// Turns off the warning:
327369
// [warn] Binary version (0.9.0-RC1) for dependency ...;0.9.0-RC1
328370
// [warn] in ... differs from Scala binary version in project (0.9).

0 commit comments

Comments
 (0)