scalaks-bootstrap: Uncaught ReferenceError: exports is not defined at scalajsenv.js:29 - scala.js

I try to make a simple scalajs + scalajs-bootstrap application, but there is few documentation on how to set up the skeleton. The scalajs-bootstrap project has an example, but the build.sbt file is very big and it contains both the source code of the scalajs-bootstrap and the example itself.
I tried creating a new project with only the bootstrap4 example, but when I open the index.html page on the browswer, it complains with
Uncaught ReferenceError: exports is not defined at scalajsenv.js:29
This is my current set-up:
project/plugins.sbt
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.20")
addSbtPlugin("com.github.karasiq" % "sbt-scalajs-bundler" % "1.2.1")
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.13.0")
build.sbt
enablePlugins(ScalaJSBundlerPlugin)
scalaVersion := "2.12.6"
libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "0.9.7"
libraryDependencies += "com.github.karasiq" %%% "scalajs-bootstrap-v4" % "2.3.5"
npmDependencies in Compile += "jquery" -> "3.2.1"
npmDependencies in Compile += "bootstrap" -> "4.1.1"
mainClass in Compile := Some("com.karasiq.bootstrap4.test.frontend.BootstrapTestApp")
scalaJSUseMainModuleInitializer := true
scalaJSModuleKind := ModuleKind.CommonJSModule
version in webpack := "4.28.4"
emitSourceMaps := false
This is the source code of scalajs-bootstrap4 libray and example:
https://github.com/Karasiq/scalajs-bootstrap
This is my complete minimal example, which fails with the exports is not defined error:
https://github.com/dportabella/scalajs-bootstrap4-examples
How to modify plugins.sbt and/or build.sbt to make this project work?
Solution
project/plugins.sbt
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.28")
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.13.0")
build.sbt
enablePlugins(ScalaJSBundlerPlugin)
scalaVersion := "2.12.8"
libraryDependencies += "org.scala-js" %%% "scalajs-dom" % "0.9.7"
libraryDependencies += "com.github.karasiq" %%% "scalajs-bootstrap-v4" % "2.3.5"
npmDependencies in Compile += "jquery" -> "3.2.1"
npmDependencies in Compile += "bootstrap" -> "4.1.1"
mainClass in Compile := Some("com.karasiq.bootstrap4.test.frontend.BootstrapTestApp")
scalaJSUseMainModuleInitializer := true
scalaJSModuleKind := ModuleKind.CommonJSModule
version in webpack := "4.28.4"
emitSourceMaps := false
full example:
https://github.com/dportabella/scalajs-bootstrap4-examples

For those who are using sbt-web-scalajs like me see:
https://scalacenter.github.io/scalajs-bundler/getting-started.html#sbt-web

In my experience, you need to:
Add the following lines to your project/plugins.sbt:
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.25")
addSbtPlugin("ch.epfl.scala" % "sbt-scalajs-bundler" % "0.13.1")
Enable both ScalaJSPlugin and ScalaJSBundlerPlugin in your build.sbt
Add the following configuration to your build.sbt:
scalaJSUseMainModuleInitializer := true
scalaJSModuleKind := ModuleKind.CommonJSModule
version in webpack := "4.28.4"
emitSourceMaps := false
Finally, run fastOptJS::webpack to create the packed JS file.
Note that the packed JS file needs to be referenced in the index.html - not the compiled JS file!
I hope this helps.

Related

Cannot pass arguments using sbt to gatling simulation

Regarding to Gatling SBT execute a specific simulation topic is there any way to pass argument to simulation?
I've been trying passing command from any CLI like:
sbt -Dx=1 -Dy=2 -Dz=3 "gatling:testOnly fooSimulation"
and:
sbt "-Dx=1 -Dy=2 -Dz=3 gatling:testOnly fooSimulation"
and all similar variations, but in result it gives just a null value.
Same thing I was trying to do in sbt shell, because I use it as well, but no success at all. Maybe my specific configuration in build.sbt is the main reason why it doesn't work. Nevertheless I do not want to pass the arguments in config file, it should be dynamic.
build.sbt
name := "Gatling"
version := "0.1"
scalaVersion := "2.12.11"
enablePlugins(GatlingPlugin)
fork := true
scalacOptions := Seq(
"-encoding", "UTF-8", "-target:jvm-1.8", "-deprecation",
"-feature", "-unchecked", "-language:implicitConversions", "-language:postfixOps")
libraryDependencies += "io.gatling.highcharts" % "gatling-charts-highcharts" % "3.3.1" % Test
libraryDependencies += "io.gatling" % "gatling-test-framework" % "3.3.1" % Test
libraryDependencies += "org.json4s" % "json4s-native_2.12" % "3.6.7" % Test
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % Test
libraryDependencies += "com.microsoft.sqlserver" % "mssql-jdbc" % "7.2.2.jre8" % Test
libraryDependencies += "org.springframework.boot" % "spring-boot-starter" % "2.3.5.RELEASE" % Test
libraryDependencies += "com.typesafe" % "config" % "1.4.1" % Test
Test / javaOptions += "-DpropertiesFile=./src/test/resources/application.properties"
plugins.sbt
addSbtPlugin("io.gatling" % "gatling-sbt" % "3.2.0")
Example code:
class FooSimulation extends Simulation {
before {
println(s"x=${System.getProperty("x")}")
println(s"y=${System.getProperty("y")}")
println(s"z=${System.getProperty("z")}")
}
setUp(
scenario("Foo")
.exec( foo chain builder )
.inject( foo injection )
).protocols( foo protocol )
}
Additionally my sbt shell is running with prefix sbt:gatling, maybe this is the reason?
sbt -Dx=1 -Dy=2 -Dz=3 "gatling:testOnly fooSimulation" is correct.
But the modern sbt syntax is sbt -Dx=1 -Dy=2 -Dz=3 "Gatling/testOnly fooSimulation".
If it doesn't work, you probably have a typo somewhere, or possibly your version of sbt and Gatling are way too old and your should upgrade.

common-validator sbt dependency for scala 2.12.1

My sbt file looks as follows
organization := "scala"
name := "MyProject"
version := "1.0"
scalaVersion := "2.12.1"
libraryDependencies += "com.github.nscala-time" %% "nscala-time" % "2.20.0"
libraryDependencies += "commons-net" % "commons-net" % "3.6"
libraryDependencies += "commons-validator" % "commons-validator" % "1.6.0"
When I run sbt compile I get this
sbt.librarymanagement.ResolveException: unresolved dependency: commons-validator#commons-validator;1.6.0: not found
However when I change scala version to 2.11.7, sbt compiles fine. What am I missing? How can I make it work for 2.12.1?
According to mvnrepo (https://mvnrepository.com/artifact/commons-validator/commons-validator)
use libraryDependencies += "commons-validator" % "commons-validator" % "1.6"

How do dependencies work in Scala?

For my existing projects I am migrating from scalaVersion 2.10.5 to 2.11.7 and sbtVersion 1.13.9 also.
Current migrating project is Sales, it have Sales-common, Sales-read, Sales-write modules. It have one dependent project Core. But I don't have Core project code I have only already published artefact jars(I have myapp-core-read_2.10-2.2.33.jar, 2.10 is Scala version).
My project looks like this
Sales
common
build.sbt
read
build.sbt
write
build.sbt
build.sbt
Till now, with scala 2.10.5 everything working fine using in real time also.
After change the version I run sbt clean and sbt update. I am getting Unresolved dependency issue like this:
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: UNRESOLVED DEPENDENCIES ::
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
[warn] :: mycompany#myapp-core-read_2.11;2.2.33: not found
[warn] :: mycomapny#myapp-core-write_2.11;2.2.33: not found
[warn] ::::::::::::::::::::::::::::::::::::::::::::::
My Sales buld.sbt is this:
name := "myapp-sales"
organization in ThisBuild := "com.mycompany"
scalaVersion in ThisBuild := "2.11.7"
//crossScalaVersions in Thisq
//Build := List("2.10.5", scalaVersion.value)
//crossPaths := false
scalacOptions in Compile in ThisBuild ++= Seq("-unchecked", "-deprecation", "-encoding", "utf8", "-language:postfixOps", "-language:implicitConVersions")
lazy val common = Project("myapp-sales-common", file("common"))
lazy val read = Project("myapp-sales-read", file("read")).configs(IntegrationTest).settings(Defaults.itSettings: _*).dependsOn(common, write)
lazy val write = Project("myapp-sales-write", file("write")).configs(IntegrationTest).settings(Defaults.itSettings: _*).dependsOn(common)
conflictWarning in ThisBuild := ConflictWarning.disable
parallelExecution in Test in ThisBuild := false
parallelExecution in IntegrationTest in ThisBuild := false
javacOptions in Compile in ThisBuild ++= Seq("-source", "1.6", "-target", "1.6")
//Remove SNAPSHOT check from the release process (for now until Squants gets a release)
releaseProcess := releaseProcess.value.filterNot(_ == ReleaseTransformations.checkSnapshotDependencies)
My read module build.sbt:
libraryDependencies ++= Seq(
"com.mycompany" %% "myapp-core-read" % myappsales.CoreVersion % "compile", // disable using the Scala version in output paths and artifacts,
"com.mycompany" %% "myapp-core-write" % myappsales.CoreVersion % "compile",
"com.mycompany" %% "myapp-registration-common" % myappsales.RegistrationVersion % "compile",
"com.mycompany" %% "myapp-load-common" % myappsales.LoadVersion % "compile",
"com.mycompany" %% "myapp-core-write-test" % myappsales.CoreVersion % "it, test",
"com.mycompany" %% "myapp-core-test" % myappsales.CoreVersion % "it, test"
)
libraryDependencies ++= Seq(
"com.typesafe" % "config" % myappsales.TypeSafeConfigVersion % "compile",
"org.json4s" %% "json4s-native" % Versions.Json4s % "compile",
"io.spray" % "spray-routing" % Versions.Spray % "compile",
"com.typesafe.akka" %% "akka-actor" % Versions.Akka % "compile",
"com.typesafe.akka" %% "akka-remote" % Versions.Akka % "compile"
exclude ("io.netty", "netty")
)
//Assemby settings
test in assembly := {}
assemblyShadeRules in assembly := Seq(
ShadeRule.rename("play.api.libs.iteratee.**" -> "shade.play.api.libs.iteratee.#1")
.inLibrary("play" % "play-iteratees_2.10" % "2.1-RC2")
.inLibrary("org.reactivemongo" % "reactivemongo_2.10" % "0.8.1-SNAPSHOT"),
ShadeRule.rename("scala.concurrent.stm.**" -> "shade.scala.concurrent.stm.#1")
.inLibrary("org.scala-stm" % "scala-stm_2.10.0" % "0.6")
.inLibrary("play" % "play-iteratees_2.10" % "2.1-RC2")
)
assemblyMergeStrategy in assembly := {
case "application.conf" => MergeStrategy.concat
case path => MergeStrategy.defaultMergeStrategy(path)
}
//Make assembly a publishable artifact
artifact in (
Compile, assembly) := {
val art = (artifact in (Compile, assembly)).value
art.copy(`classifier` = Some("assembly"))
}
addArtifact(artifact in (Compile, assembly), assembly)
How can I make it work with existing myapp-core-read_2.10-2.2.33.jar jar?
Important note: Scala 2.10 and 2.11 are not binary compatible so you will have to recompile the modules on which you depend
Answer to question:
This seems to be related to how SBT resolves dependencies. When you declare a dependency like so:
"com.mycompany" %% "myapp-core-read" % myappsales.CoreVersion
The %% part in the declaration means that SBT will automatically append the scala version to the library name, so your dependency becomes
group: com.mycompany
artifactId: myapp-core-read_2.11
version: `your version`
It seems that your myapp-core-read was not compiled with scala 2.11 thus SBT can't find the correct version.
You can avoid this by using % and applying the correct suffix manually, so your dependency would become:
"com.mycompany" % "myapp-core-read_2.10" % myappsales.CoreVersion
Having said that, I think scala 2.10 and 2.11 are not binary compatible so you might have to recompile the myapp-core-read module with 2.11.

error: not found: value StartScriptPlugin StartScriptPlugin.stage in Compile := Unit

First I need to say, I'm Scala and SBT beginner so this is my first project with it. I try to deploy scala app on Heroku. I'm getting this error:
error: not found: value StartScriptPlugin StartScriptPlugin.stage in Compile := Unit
Here is my build.sbt. What is wrong with it? I know that is something with StartScriptPlugin.stage in Compile := Unit but I found some examples and they always use it this way. Thanks
name := "Survey server"
version := "1.0"
scalaVersion := "2.9.2"
resolvers ++= Seq("repo.codahale.com" at "http://repo.codahale.com", Classpaths.typesafeResolver)
addSbtPlugin("com.typesafe.startscript" % "xsbt-start-script-plugin" % "0.5.3")
StartScriptPlugin.stage in Compile := Unit
EclipseKeys.withSource := true
libraryDependencies ++= Seq(
"net.databinder" %% "unfiltered-filter" % "0.6.3",
"net.databinder" %% "unfiltered-jetty" % "0.6.3",
"net.databinder.dispatch" %% "core" % "0.9.0",
"com.codahale" % "jerkson_2.9.1" % "0.5.0",
"org.scalaquery" % "scalaquery_2.9.1" % "0.10.0-M1",
"postgresql" % "postgresql" % "9.1-901.jdbc4" )
You need to tell SBT that your project uses the StartScriptPlugin. This is done by using the addSbtPlugin command in project/plugins.sbt. This is all explained in the SBT documentation:
https://github.com/harrah/xsbt/wiki/Getting-Started-Using-Plugins
The documentation for the plugin that you are trying to use even tells you exactly what you need to add to project/plugins.sbt. For SBT 0.11, this would be:
resolvers += Classpaths.typesafeResolver
addSbtPlugin( "com.typesafe.startscript" % "xsbt-start-script-plugin" % "0.5.2" )
See https://github.com/typesafehub/xsbt-start-script-plugin

sbt value not found addSbtPlugin when installing onejar plugin

I'm trying to install the one-jar sbt plugin but am getting the following error:
sbt/project/plugins/plugins.sbt:5: error: not found: value addSbtPlugin
addSbtPlugin("com.github.retronym" % "sbt-onejar" % "0.6")
Here is the relevant contents of my sbt/build.sbt file:
seq(com.github.retronym.SbtOneJar.oneJarSettings: _*)
name := "dsg_nlp"
version := "0.11"
scalaVersion := "2.9.1"
libraryDependencies ++= Seq( "org.scalatest" %% "scalatest" % "1.6.1" % "test" )
libraryDependencies += "commons-lang" % "commons-lang" % "2.6"
traceLevel in run := 0
fork in run := true
javaOptions in run ++= Seq("-Xmx7G", "-agentlib:hprof=cpu=samples,depth=12", "-server", "-enableassertions")
scalacOptions ++= Seq("-optimize")
mainClass in (one-jar, Compile, packageBin) := Some("Test")
And the contents of my project/plugins/plugins.sbt file:
resolvers += "retronym-releases" at "http://retronym.github.com/repo/releases"
resolvers += "retronym-snapshots" at "http://retronym.github.com/repo/snapshots"
addSbtPlugin("com.github.retronym" % "sbt-onejar" % "0.6")
I would suggest the following changes to the code above:
Make sure you have only one setting per line. So split
libraryDependencies ++= Seq( "org.scalatest" %% "scalatest" % "1.6.1" % "test" )
libraryDependencies += "commons-lang" % "commons-lang" % "2.6"
into two lines.
The last line should read
mainClass in oneJar := Some("Test")
if you want to use another mainClass for the oneJar-Plugin. If it's the same as in the compile scope. You may as well write this as
mainClass in Compile := Some("Test")
but do not specifiy both.
Your project directory structure should look like this:
Project-Root /
|-- build.sbt
|-- project/plugins.sbt
the actual names of the sbt-files don't matter they just have to end in .sbt.