Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
    kevinwright
    @kevinwright
    Actually, strike that first point, the watchSources was from before I started using sbt-web, that bit should be good now
    Vincent Munier
    @vmunier

    sbt-web-scalajs will only work if you have separate projects because you need to set the scalaJSProjects setting and you can't self reference your only project. This won't work:

    val client = project.settings(
      scalaJSProjects := Seq(client),  ...
    ).enablePlugins(SbtWeb)

    If you only need to copy fastOptJS output to another directory, it's probably not worth using sbt-web-scalajs in your case.

    moritz bust
    @busti
    Hi,
    where can I find the compiled js files when I run/compile the server project?
    There is nothing in server/target/<etc> after compilation.
    Here is my build.sbt for reference:
    name := "test"
    
    val commonSettings = Seq(
      scalaVersion := "2.12.4",
      version := "1.0"
    )
    
    val http4s = "0.18.0-M9"
    
    lazy val server = (project in file("server"))
      .enablePlugins(SbtWeb)
      .settings(commonSettings)
      .settings(
        scalacOptions ++= Seq("-Ypartial-unification"),
        libraryDependencies ++= Seq(
          "org.http4s"                 %% "http4s-dsl"          % http4s,
          "org.http4s"                 %% "http4s-blaze-server" % http4s,
          "org.http4s"                 %% "http4s-circe"        % http4s,
    
          "ch.qos.logback"             % "logback-classic"      % "1.2.3",
          "com.typesafe.scala-logging" %% "scala-logging"       % "3.7.2"
        ),
        scalaJSProjects := Seq(client),
        pipelineStages in Assets := Seq(scalaJSPipeline),
        compile in Compile := ((compile in Compile) dependsOn scalaJSPipeline).value
        /*resourceGenerators in Compile += Def.task {
          println("Copying ScalaJS sources...")
          val files = ((crossTarget in(client, Compile)).value ** ("*.js" || "*.map")).get
          val mappings: Seq[(File,String)] = files pair Path.rebase(
            (crossTarget in(client, Compile)).value,
            ((resourceManaged in  Compile).value / "web/js/").getAbsolutePath
          )
          val map: Seq[(File, File)] = mappings.map { case (s, t) => (s, file(t))}
          IO.copy(map).toSeq
        } dependsOn (fastOptJS in(client, Compile))*/
      )
    
    lazy val client = (project in file("client"))
      .enablePlugins(ScalaJSPlugin, ScalaJSWeb)
      .settings(commonSettings)
      .settings(
        scalaJSUseMainModuleInitializer := true,
        libraryDependencies ++= Seq(
          "be.doeraene"   %%% "scalajs-jquery" % "0.9.1",
          "com.lihaoyi"   %%% "scalatags"      % "0.6.7",
          "com.lihaoyi"   %%% "scalarx"        % "0.3.2",
          "com.timushev"  %%% "scalatags-rx"   % "0.3.0"
        ),
        skip in packageJSDependencies := false,
          jsDependencies ++= Seq(
          "org.webjars" % "jquery" % "2.1.4" / "2.1.4/jquery.js",
        )
      )
    
    // loads the server project at sbt startup
    onLoad in Global := (onLoad in Global).value andThen {s: State => "project server" :: s}
    moritz bust
    @busti
    It seems like I forgot to remove my previous method of copying the scalajs files, it is the comment block underneath the sbt-web-scalajs lines
    moritz bust
    @busti
    Is sbt-resolver required for sbt-web-scalajs to work?
    I switched to akka-http, but the scalajs files did not get copied, only after I added sbt-resolver they were copied.
    Vincent Munier
    @vmunier
    Hi, sbt-resolver is not required, you can use run and see the files get copied into the server's target folder. As for your previous build, I have not tried using sbt-web-scalajs with http4s but hopefully, it should not be too different from akka-http.
    moritz bust
    @busti
    @vmunier Thanks a lot for your answer, I am sorry that I didn't reply earlier, a lot has been going on lately.
    I have now switched to akka-http and things are working out perfectly.
    There is just one minor issue, when I try to use ~run on my server project, nothing happens when the client sources change.
    I am using sbt-resolver for that at the moment, but it makes the whole webserver restart each time a source changes which is not ideal.
    Vincent Munier
    @vmunier
    @Busti Indeed, you have to use sbt-resolver (~reStart) with akka-http if you want to see your changes get automatically included. Unfortunately, it restarts the web server, which can be slow, but I don't know any way around it. The experience is a bit better with Play framework: the server does not need to be restarted when an asset file (HTML/CSS, JS, etc.) gets modified but Play has its own auto-reload feature.
    moritz bust
    @busti
    Shouldn't ~compile listen for file changes aswell?
    Vincent Munier
    @vmunier
    It should if if you add compile in Compile := ((compile in Compile) dependsOn scalaJSPipeline).value to you server's settings. Have a look at https://github.com/vmunier/akka-http-scalajs.g8 for an example (sbt new vmunier/akka-http-scalajs.g8)
    ioleo
    @ioleo
    hello, I've got a problem trying to setup ScalaJS with scalajs-bundler. My config is simmilar to https://github.com/vmunier/akka-http-scalajs.g8/blob/master/src/main/g8/build.sbt however, when I try to serve the assets I'm getting 404 Not found errors at runtime
    import cats.effect.Sync
    import java.io.File
    import org.http4s.{ HttpService, Request, Response, StaticFile }
    import org.http4s.dsl.Http4sDsl
    import org.http4s.dsl.io._
    import org.http4s.twirl._
    import app.html
    
    class SpaController[F[_]: Sync] extends Http4sDsl[F] {
    
      val service: HttpService[F] = HttpService[F] {
        case GET -> Root => Ok(
          html.index("App title")
        )
        case request @ GET -> "assets" /: rest =>
          static(rest, request)
      }
    
      private def static(path: Path, request: Request[F]): F[Response[F]] =
        StaticFile.fromResource(s"public/$path", Some(request)).getOrElseF(NotFound())
    }
    These are my build settings
      lazy val shared = commonSettings ++ Seq(
        name := "intranet-shared",
        libraryDependencies ++= Dependencies.shared.value,
      )
    
      lazy val server = commonSettings ++ Seq(
        name := "intranet-server",
        libraryDependencies ++= Dependencies.server,
        libraryDependencies ++= Dependencies.jvm,
        pipelineStages in Assets := Seq(scalaJSPipeline),
        compile in Compile := ((compile in Compile) dependsOn scalaJSPipeline).value,
        WebKeys.packagePrefix in Assets := "public/",
        managedClasspath in Runtime += (packageBin in Assets).value,
      )
    
      lazy val client = commonSettings ++ Seq(
        name := "intranet-client",
        mainClass in Compile := Some("app.Application"),
        libraryDependencies ++= Dependencies.client.value,
        npmDependencies in Compile := Dependencies.js,
        npmDevDependencies in Compile := Dependencies.devJs,
        webpackConfigFile in fastOptJS := Some(baseDirectory.value / "dev.webpack.config.js"),
        webpackConfigFile in fullOptJS := Some(baseDirectory.value / "prod.webpack.config.js"),
        scalaJSUseMainModuleInitializer := true,
        scalaJSUseMainModuleInitializer in Test := false,
        jsEnv := new org.scalajs.jsenv.jsdomnodejs.JSDOMNodeJSEnv,
        webpackBundlingMode := BundlingMode.LibraryOnly(),
      )
    And my build.sbt
    lazy val shared = crossProject(JSPlatform, JVMPlatform)
      .crossType(CrossType.Pure)
      .in(file("shared"))
      .settings(BuildSettings.shared)
      .jsConfigure(_.enablePlugins(ScalaJSWeb, ScalaJSBundlerPlugin))
    
    lazy val sharedJVM = shared.jvm.settings(name := "sharedJVM")
    lazy val sharedJS  = shared.js.settings(name := "sharedJS")
    
    lazy val server = project
      .in(file("server"))
      .settings(BuildSettings.server)
      .settings(scalaJSProjects := Seq(client))
      .enablePlugins(WebScalaJSBundlerPlugin, SbtTwirl, JavaAppPackaging)
      .aggregate(client)
      .dependsOn(sharedJVM)
    
    lazy val client: Project = project
      .in(file("client"))
      .settings(BuildSettings.client)
      .enablePlugins(ScalaJSPlugin, ScalaJSBundlerPlugin, ScalaJSWeb)
      .dependsOn(sharedJS)
    
    onLoad in Global := (onLoad in Global).value andThen {s: State => "project server" :: s}
    ioleo
    @ioleo
    The requests my front/index page is making are:
    • http://127.0.0.1:8080/assets/intranet-client-fastopt-library.js
    • http://127.0.0.1:8080/assets/intranet-client-fastopt-loader.js
    • http://127.0.0.1:8080/assets/intranet-client-fastopt.js
    I'm wondering where I've made the mistake.
    Vincent Munier
    @vmunier
    I have not tried using sbt-web-scalajs with http4s yet. You should probably try adapting akka-http-scalajs.g8 to work with http4s first, without using scalajs-bundler for now.
    ioleo
    @ioleo
    Is there any sbt command that should list my js assets? So that I can narrow down if the problem is with: files being absent or the way I serve them?
    ioleo
    @ioleo
    Ok, it seems after adding explicit copy task in sbt I can do show compile:managedResources and see my scalajs files ^__^
    import java.nio.file.{Files, StandardCopyOption}
    import com.typesafe.sbt.packager.SettingsHelper._
    
    lazy val server = project
      .in(file("server"))
      .settings(BuildSettings.server)
      .settings(
        scalaJSProjects := Seq(client),
        (Compile / resourceGenerators) += Def.task {
          val webpackFiles = (client / Compile / fullOptJS / webpack).value.map(_.data).filterNot(_.getName contains "bundle")
          val npmFiles = Seq.empty[File]
    
          (webpackFiles ++ npmFiles).map { sourceFile =>
            val targetFile = (Compile / resourceManaged).value / "public" / sourceFile.getName
            Files.createDirectories(targetFile.getParentFile().toPath())
            Files.copy(sourceFile.toPath, targetFile.toPath, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES)
            targetFile
          }
        }.taskValue,
      )
      .enablePlugins(WebScalaJSBundlerPlugin, SbtTwirl, JavaAppPackaging)
      .aggregate(client)
      .dependsOn(sharedJVM)
    its still work in progress (I guess I might need to copy some npm files too), but it's a start
    show compile:managedResources
    [info] client / Compile / managedResources
    [info]     List()
    [info] Compile / managedResources
    [info]     List(/home/user/Projects/intranet/server/target/scala-2.12/resource_managed/main/public/intranet-client-opt.js, /home/user/Projects/intranet/server/target/scala-2.12/resource_managed/main/public/intranet-client-opt-loader.js, /home/user/Projects/intranet/server/target/scala-2.12/resource_managed/main/public/intranet-client-opt-library.js, /home/user/Projects/intranet/server/target/scala-2.12/resource_managed/main/public/intranet-client-opt-library.js.map)
    ioleo
    @ioleo
    and it's working :) I've had some more hiccups with routes (didnt strip the slash), but finally my assets are served :)
    Vincent Munier
    @vmunier
    Nice, good news!
    Ali Akhtar
    @AliAkhtar__twitter
    Hey, I'm trying to follow along the instructions, but I can't get the continuous compile to work. I ran the project using both ~run and ~reStart but if I change the SharedMessages.itWorks content, and refresh the page, it doesn't show the update message unless I ctrl + c and re-run it.
    Ali Akhtar
    @AliAkhtar__twitter
    Using ~reStart causes it to get stuck on "Stopping application server (by killing the forked JVM) ..."
    Vincent Munier
    @vmunier
    Hey, I've just tried ~reStart on akka-http-scalajs and it's working for me. I am using SBT v1.1.2, what version of SBT are you using?
    Ali Akhtar
    @AliAkhtar__twitter
    Also using 1.1.2 . I think I previously ran just the sbt run and sbt ~run , could these have caused a forked process to get stuck somehow?
    i've noticed that an old version is still running on localhost:8000 and i had to increment the port to 8001 to get it to bind the port
    Does it auto reload / recompile for you if you make changes and use ~reStart ?
    Vincent Munier
    @vmunier
    When using ~reStart and changing SharedMessages.scala, I can see in the SBT output that it's calling fastoptjs (Fast optimizing ...). However, I don't see the Scala.js message being updated in Chrome when I reload the page. I do see the change when I open a private window, so it's probably a caching issue.
    Ryuhei Ishibashi
    @rysh
    Hi, I have a problem that tests doesn’t work in the client project.
    Could you please help me ?
    https://github.com/rysh/play-scalajs-unittest-example
    Vincent Munier
    @vmunier

    Hi, it looks like you need to use triple %, so that scalatest and scalacheck can be used from Scala.js. You will also need to use Scala.js 0.6.x instead of Scala.js 1.x because scalatest & scalacheck are not compatible with Scala.js 1.x yet. So, update your build.sbt to have:

    "org.scalatest"  %%% "scalatest"  % "3.0.5"  % Test,
    "org.scalacheck" %%% "scalacheck" % "1.14.0" % Test

    and update your project/plugins.sbt to use:

    addSbtPlugin("com.vmunier"  % "sbt-web-scalajs" % "1.0.8-0.6")
    addSbtPlugin("org.scala-js" % "sbt-scalajs"     % "0.6.23")

    You should then be able to run your client's tests.

    Ryuhei Ishibashi
    @rysh
    Thanks to that, I could fix it. Thank you very much!
    Vincent Munier
    @vmunier
    No worries, glad it works
    Joan Goyeau
    @joan38
    Hey how do we use that with https://github.com/scalacenter/scalajs-bundler ?
    I think I need it to run the command fastOptJS/webpack
    michael_zh
    @michael_zh_twitter
    Can not reload run with "~run"??
    Error: 8080 Address already in use
    @michael_zh_twitter it looks like you already have a process listening on port 8080
    Jason Pickens
    @steinybot
    How can I change the target directory for the scalaJSPipeline mappings? I’d like it to go to a js directory.
    The best I can come up with is to create a new stage that moves the files.
    Vincent Munier
    @vmunier
    sbt-web-scalajs does not have any settings to configure the target directory. It relies on sbt-web, which takes care of copying the pipeline output files. If using Play, I think sbt-web's target directory can be changed by setting WebKeys.public in Assets := file("<folder-path>") in the server project
    Timo Meijer
    @TimoMeijer

    I'm trying to build a ScalaJS project with Scala 3. If I run my project with scala 2.13 it works fine, but if I change the version to 3.0.0-M3 it fails with bad option: -P:scalajs:mapSourceURI:...
    The issue persists even with the bare minimum configuration:

    lazy val client = (project in file("client"))
      .settings(commonSettings)
      .settings(
        scalaVersion := "3.0.0-M3",
        scalaJSUseMainModuleInitializer := true
      )
      .enablePlugins(ScalaJSPlugin, ScalaJSWeb)

    and plugins:

    addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % "0.5.1")
    addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.3.1")
    addSbtPlugin("com.vmunier"  % "sbt-web-scalajs" % "1.1.0")

    Any clue how to resolve this issue?

    Sébastien Doeraene
    @sjrd
    In Scala 3 the correct option is -scalajs-mapSourceURI:...
    Timo Meijer
    @TimoMeijer
    Ah, so this is something that needs to be changed in the sbt-web-scalajs plugin, so it can be compatible with Scala 3
    Domantas Petrauskas
    @ptrdom
    Hi, I have ScalaTest setup where I start application and run tests with Selenium, I wonder how I could make assets build and be available in classpath when running tests?
    2 replies
    Vincent Munier
    @vmunier
    Hey everyone, sbt-web-scalajs v1.2.0 is now available! It's compatible with Scala 3 and updated to Scala.js 1.5.1 https://github.com/vmunier/sbt-web-scalajs/releases/tag/v1.2.0