Skip to content

Commit 3f2b12f

Browse files
authored
chore: extract DottyJSPlugin from Build (#23597)
2 parents 00d19df + 3dfceda commit 3f2b12f

File tree

2 files changed

+68
-53
lines changed

2 files changed

+68
-53
lines changed

project/Build.scala

Lines changed: 10 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import sbt.PublishBinPlugin.autoImport._
2424
import dotty.tools.sbtplugin.RepublishPlugin
2525
import dotty.tools.sbtplugin.RepublishPlugin.autoImport._
2626
import dotty.tools.sbtplugin.ScalaLibraryPlugin
27+
import dotty.tools.sbtplugin.DottyJSPlugin
28+
import dotty.tools.sbtplugin.DottyJSPlugin.autoImport._
2729

2830
import sbt.plugins.SbtPlugin
2931
import sbt.ScriptedPlugin.autoImport._
@@ -39,58 +41,6 @@ import sbttastymima.TastyMiMaPlugin.autoImport._
3941
import scala.util.Properties.isJavaAtLeast
4042

4143
import org.portablescala.sbtplatformdeps.PlatformDepsPlugin.autoImport._
42-
import org.scalajs.linker.interface.{ModuleInitializer, StandardConfig}
43-
44-
object DottyJSPlugin extends AutoPlugin {
45-
import Build._
46-
47-
object autoImport {
48-
val switchToESModules: StandardConfig => StandardConfig =
49-
config => config.withModuleKind(ModuleKind.ESModule)
50-
}
51-
52-
val writePackageJSON = taskKey[Unit](
53-
"Write package.json to configure module type for Node.js")
54-
55-
override def requires: Plugins = ScalaJSPlugin
56-
57-
override def projectSettings: Seq[Setting[_]] = Def.settings(
58-
commonBootstrappedSettings,
59-
60-
/* #11709 Remove the dependency on scala3-library that ScalaJSPlugin adds.
61-
* Instead, in this build, we use `.dependsOn` relationships to depend on
62-
* the appropriate, locally-defined, scala3-library-bootstrappedJS.
63-
*/
64-
libraryDependencies ~= {
65-
_.filter(!_.name.startsWith("scala3-library_sjs1"))
66-
},
67-
68-
// Replace the JVM JUnit dependency by the Scala.js one
69-
libraryDependencies ~= {
70-
_.filter(!_.name.startsWith("junit-interface"))
71-
},
72-
libraryDependencies +=
73-
("org.scala-js" %% "scalajs-junit-test-runtime" % scalaJSVersion % "test").cross(CrossVersion.for3Use2_13),
74-
75-
// Typecheck the Scala.js IR found on the classpath
76-
scalaJSLinkerConfig ~= (_.withCheckIR(true)),
77-
78-
Compile / jsEnvInput := (Compile / jsEnvInput).dependsOn(writePackageJSON).value,
79-
Test / jsEnvInput := (Test / jsEnvInput).dependsOn(writePackageJSON).value,
80-
81-
writePackageJSON := {
82-
val packageType = scalaJSLinkerConfig.value.moduleKind match {
83-
case ModuleKind.NoModule => "commonjs"
84-
case ModuleKind.CommonJSModule => "commonjs"
85-
case ModuleKind.ESModule => "module"
86-
}
87-
88-
val path = target.value / "package.json"
89-
90-
IO.write(path, s"""{"type": "$packageType"}\n""")
91-
},
92-
)
93-
}
9444

9545
object Build {
9646
import ScaladocConfigs._
@@ -1608,6 +1558,7 @@ object Build {
16081558
asDottyLibrary(Bootstrapped).
16091559
enablePlugins(DottyJSPlugin).
16101560
settings(
1561+
commonBootstrappedSettings,
16111562
libraryDependencies +=
16121563
("org.scala-js" %% "scalajs-library" % scalaJSVersion).cross(CrossVersion.for3Use2_13),
16131564
// NOTE: Until 3.8.0, we pin the source files to be used by the scala3 library
@@ -2055,6 +2006,7 @@ object Build {
20552006
enablePlugins(DottyJSPlugin).
20562007
dependsOn(`scala3-library-bootstrappedJS`).
20572008
settings(
2009+
commonBootstrappedSettings,
20582010
// Required to run Scala.js tests.
20592011
Test / fork := false,
20602012

@@ -2072,6 +2024,7 @@ object Build {
20722024
enablePlugins(DottyJSPlugin).
20732025
dependsOn(`scala3-library-bootstrappedJS`).
20742026
settings(
2027+
commonBootstrappedSettings,
20752028
bspEnabled := false,
20762029
scalacOptions --= Seq("-Werror", "-deprecation"),
20772030

@@ -2315,12 +2268,15 @@ object Build {
23152268
lazy val `scaladoc-js-common` = project.in(file("scaladoc-js/common")).
23162269
enablePlugins(DottyJSPlugin).
23172270
dependsOn(`scala3-library-bootstrappedJS`).
2318-
settings(libraryDependencies += ("org.scala-js" %%% "scalajs-dom" % "2.8.0"))
2271+
settings(
2272+
commonBootstrappedSettings,
2273+
libraryDependencies += ("org.scala-js" %%% "scalajs-dom" % "2.8.0"))
23192274

23202275
lazy val `scaladoc-js-main` = project.in(file("scaladoc-js/main")).
23212276
enablePlugins(DottyJSPlugin).
23222277
dependsOn(`scaladoc-js-common`).
23232278
settings(
2279+
commonBootstrappedSettings,
23242280
scalaJSUseMainModuleInitializer := true,
23252281
Test / fork := false
23262282
)
@@ -2329,6 +2285,7 @@ object Build {
23292285
enablePlugins(DottyJSPlugin).
23302286
dependsOn(`scaladoc-js-common`).
23312287
settings(
2288+
commonBootstrappedSettings,
23322289
Test / fork := false,
23332290
scalaJSUseMainModuleInitializer := true,
23342291
libraryDependencies += ("org.scala-js" %%% "scalajs-dom" % "2.8.0")

project/DottyJSPlugin.scala

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package dotty.tools.sbtplugin
2+
3+
import sbt.*
4+
import sbt.Keys.*
5+
6+
import org.scalajs.sbtplugin.ScalaJSPlugin
7+
import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport._
8+
9+
import org.scalajs.linker.interface.StandardConfig
10+
11+
object DottyJSPlugin extends AutoPlugin {
12+
13+
object autoImport {
14+
val switchToESModules: StandardConfig => StandardConfig =
15+
config => config.withModuleKind(ModuleKind.ESModule)
16+
}
17+
18+
val writePackageJSON = taskKey[Unit](
19+
"Write package.json to configure module type for Node.js")
20+
21+
override def requires: Plugins = ScalaJSPlugin
22+
23+
override def projectSettings: Seq[Setting[_]] = Def.settings(
24+
25+
/* #11709 Remove the dependency on scala3-library that ScalaJSPlugin adds.
26+
* Instead, in this build, we use `.dependsOn` relationships to depend on
27+
* the appropriate, locally-defined, scala3-library-bootstrappedJS.
28+
*/
29+
libraryDependencies ~= {
30+
_.filter(!_.name.startsWith("scala3-library_sjs1"))
31+
},
32+
33+
// Replace the JVM JUnit dependency by the Scala.js one
34+
libraryDependencies ~= {
35+
_.filter(!_.name.startsWith("junit-interface"))
36+
},
37+
libraryDependencies +=
38+
("org.scala-js" %% "scalajs-junit-test-runtime" % scalaJSVersion % "test").cross(CrossVersion.for3Use2_13),
39+
40+
// Typecheck the Scala.js IR found on the classpath
41+
scalaJSLinkerConfig ~= (_.withCheckIR(true)),
42+
43+
Compile / jsEnvInput := (Compile / jsEnvInput).dependsOn(writePackageJSON).value,
44+
Test / jsEnvInput := (Test / jsEnvInput).dependsOn(writePackageJSON).value,
45+
46+
writePackageJSON := {
47+
val packageType = scalaJSLinkerConfig.value.moduleKind match {
48+
case ModuleKind.NoModule => "commonjs"
49+
case ModuleKind.CommonJSModule => "commonjs"
50+
case ModuleKind.ESModule => "module"
51+
}
52+
53+
val path = target.value / "package.json"
54+
55+
IO.write(path, s"""{"type": "$packageType"}\n""")
56+
},
57+
)
58+
}

0 commit comments

Comments
 (0)