2

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

David Portabella
  • 12,390
  • 27
  • 101
  • 182

2 Answers2

1

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

Joan
  • 4,079
  • 2
  • 28
  • 37
0

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.

Markus Appel
  • 3,138
  • 1
  • 17
  • 46
  • the issue `npm ERR! Error: EACCES: permission denied` was solved by deleting my ~./npm folder, and running sbt again. However, it still fails with `Uncaught ReferenceError: exports is not defined at scalajsenv.js:29` when opening the index.html file on the browser. – David Portabella Jul 25 '19 at 16:43
  • it works now; see https://stackoverflow.com/a/57203275/280393 However it fails with plugin ch.epfl.scala:sbt-scalajs-bundler from 0.15.0 (instead of 0.13.0). any idea? – David Portabella Jul 28 '19 at 15:49