Skip to content

Commit

Permalink
Bloop: Support JavaScript module kind
Browse files Browse the repository at this point in the history
  • Loading branch information
tindzk committed May 1, 2020
1 parent f9a1f5a commit 8098ab7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ jsdom = false # Import the jsdom library in the generated.
# yarn/npm. Useful for test cases that rely
# on DOM operations.
emitSourceMaps = true # Emit source maps
moduleKind = "default" # Options: default, commonjs
output = "myModule.js" # Path to generated file
# Default: <module name>.js
```
Expand Down
14 changes: 13 additions & 1 deletion src/main/scala/seed/config/util/TomlUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.nio.file.{Path, Paths}
import org.apache.commons.io.FileUtils
import seed.{Log, LogLevel}
import seed.cli.util.Ansi
import seed.model.Build.{PlatformModule, VersionTag}
import seed.model.Build.{ModuleKindJs, PlatformModule, VersionTag}
import seed.model.{Licence, Platform, TomlBuild}
import toml.{Codec, Value}

Expand Down Expand Up @@ -70,6 +70,18 @@ object TomlUtils {
Left((List(), s"Version tag expected, $value provided"))
}

implicit val moduleKindJs: Codec[ModuleKindJs] = Codec {
case (Value.Str(id), _, _) =>
id match {
case "default" => Right(ModuleKindJs.Default)
case "commonjs" => Right(ModuleKindJs.CommonJs)
case _ => Left((List(), "Invalid module kind provided"))
}

case (value, _, _) =>
Left((List(), s"Module kind expected, $value provided"))
}

implicit val licenceCodec: Codec[Licence] = Codec {
case (Value.Str(id), _, _) =>
Licence.All.find(_.id == id) match {
Expand Down
8 changes: 6 additions & 2 deletions src/main/scala/seed/generation/Bloop.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import seed.config.BuildConfig.{
}
import seed.artefact.{ArtefactResolution, Coursier}
import seed.cli.util.Ansi
import seed.model.Build.Module
import seed.model.Build.{Module, ModuleKindJs}
import seed.model.Platform.{JVM, JavaScript, Native}
import seed.model.Resolution
import seed.Log
Expand Down Expand Up @@ -156,6 +156,7 @@ object Bloop {
module.js.foreach { js =>
val jsdom = js.jsdom
val emitSourceMaps = js.emitSourceMaps
val moduleKind = js.moduleKind
val mainClass = js.mainClass

val bloopName = if (!test) name else name + "-test"
Expand Down Expand Up @@ -221,7 +222,10 @@ object Bloop {
Config.JsConfig(
version = js.scalaJsVersion.get,
mode = Config.LinkerMode.Debug,
kind = Config.ModuleKindJS.NoModule,
kind = moduleKind match {
case ModuleKindJs.Default => Config.ModuleKindJS.NoModule
case ModuleKindJs.CommonJs => Config.ModuleKindJS.CommonJSModule
},
emitSourceMaps = emitSourceMaps,
jsdom = Some(jsdom),
output = jsOutputPath,
Expand Down
7 changes: 7 additions & 0 deletions src/main/scala/seed/model/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ object Build {
)
}

sealed trait ModuleKindJs
object ModuleKindJs {
case object Default extends ModuleKindJs
case object CommonJs extends ModuleKindJs
}

// TODO Instead of using this `case class` directly, create a polymorphic
// version for different platform types
case class Module(
Expand All @@ -122,6 +128,7 @@ object Build {
// --- JavaScript
jsdom: Boolean = false,
emitSourceMaps: Boolean = true,
moduleKind: ModuleKindJs = ModuleKindJs.Default,
// --- Native
gc: Option[String] = None,
targetTriple: Option[String] = None,
Expand Down
3 changes: 2 additions & 1 deletion test/test-scalajs-10/build.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ sources = ["src"]

[module.example.test.js]
sources = ["test"]
scalaDeps = [["org.scalameta", "munit", "0.7.3"]]
scalaDeps = [["org.scalameta", "munit", "0.7.3"]]
moduleKind = "commonjs"

0 comments on commit 8098ab7

Please sign in to comment.