scala.js

How to provide a stub implementation of JDK classes (like java.awt) in a Scala.js project?

本秂侑毒 提交于 2021-02-10 06:11:58
问题 Here is my attempt to provide a dummy implementation of a part of java.awt related to Graphics2D : package java package object awt { object RenderingHints { type Key = Int val KEY_TEXT_ANTIALIASING = 0 val VALUE_TEXT_ANTIALIAS_ON = 0 } object Color { val GREEN = 0 } type Color = Int object image { object BufferedImage { val TYPE_INT_RGB = 0 } class BufferedImage(w: Int, h: Int, tpe: Int) { def createGraphics: Graphics2D = new Graphics2D } } class Graphics2D { def setColor(c: Color): Unit = ()

Facing ReferenceError while running tests with ScalaJS Bundler

青春壹個敷衍的年華 提交于 2021-01-28 08:47:36
问题 I am facing this issue when upgrading from sbt-scalajs 0.6.x to 1.2.0 and the issue is:- With sbt-scalajs v0.6.26 (and sbt-scalajs-bundler v0.14.0 ), I have enabled the jsdom support for tests: requireJsDomEnv in Test := true And test suites are running fine. But with sbt-scalajs v1.2.0 (and sbt-scalajs-bundler v0.18.0 ), I have enabled the jsdom support for tests too: requireJsDomEnv in Test := true But this is giving me the following error: [info] Writing and bundling the test loader

scala-js “@JSGlobalScope” error when migrating to scala-js 1.1.1

核能气质少年 提交于 2020-08-26 07:18:08
问题 I had a fallowing snippet of code in scala-js(0.6.33) object Main2 extends App { val js = for { jsTest <- JSTest.js1.toOption } yield jsTest println(JSTest.js1) } import scala.scalajs.js import scala.scalajs.js.annotation.JSGlobalScope @js.native @JSGlobalScope object JSTest extends js.Object { def js1: js.UndefOr[JS2] = js.native } @js.native trait JS1 extends js.Object { def js1: js.UndefOr[JS2] = js.native } @js.native trait JS2 extends js.Object { def js2: js.UndefOr[Int] = js.native }

Embedding resources in scala.js

霸气de小男生 提交于 2020-02-25 06:33:45
问题 I have a localization resource file I need access from scala.js. It needs to be local to the script execution environment (i.e., not loaded asynchronously from a server, as recommended at How to read a resource file in Scala.js?). Is there any mechanism for embedding the contents of a small resource file directly into the generated javascript compiled from a scala.js file? Basically, I want something like: object MyResource { @EmbeddedResource(URL("/my/package/localized.txt")) val

How to use scalajs-bundler with client only app

こ雲淡風輕ζ 提交于 2020-01-15 11:13:34
问题 In another question I was advised to use ScalaJS bundler to import NPM dependencies. I would like to use some Javascript NPM packages in a simple client-only web application. There is an example called static which shows this. My changes to the example: Add into build.sbt: npmDependencies in Compile += "esprima" -> "3.1.3" Add into Main.scala: import Esprima._ import JsonToString._ val code = "answer = 42" val tokens = tokenize(code) val tokensStr = tokens.json Change in Main.scala : "This is

upickle read from scalaJS - upickle.Invalid$Data: String (data: 1)

点点圈 提交于 2020-01-07 02:41:18
问题 From ScalaJS. import upickle.default._ import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue case class Post(userId: Long, id: Long, title: String, body: String) @JSExport def posts() : Future[Seq[Post]] = { val txt = """[{ "userId": 1, "id": 1, "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem

How to access 'this' element from event handler passed into scalatags?

耗尽温柔 提交于 2020-01-04 11:10:54
问题 I'm trying to access the text of the current ( this ) element from within an event handler created with scalatags. Here is what I tried: val onChange = {(e: HTMLElement) => number() = e.textContent.toInt }: js.ThisFunction input(`type`:="number", onchange := onChange).render When I debug the above code, nothing is being passed into the onChange function. Specifically, if I put this into the function body: js.Dynamic.global.alert(JSON.stringify(e)) , it prints {} . Also, I get an error that e

How do you Cross Compile to Scala.JS with Gradle

二次信任 提交于 2020-01-04 11:04:42
问题 When adding Scala.JS dependencies in SBT you use %%% . E.g. libraryDependencies += "be.doeraene" %%% "scalajs-jquery" % "0.9.0" How is this done in the Gradle Scala plugin? 回答1: %%% = the scala version + scalajs version. So something like: val scalaJsDomV = "0.9.1" libraryDependencies ++= Seq( "org.scala-js" %%% "scalajs-dom" % scalaJsDomV ) would be compile "org.scala-js:scalajs-dom_sjs0.6_2.12:0.9.1" Or if you used the ext for multiple used versions it would be something like: ext {

how can one distinguish JS Opaque Objects?

风流意气都作罢 提交于 2020-01-04 08:21:26
问题 The WebCrypto API introduces the notion of non exportable private keys, which can be exported to IndexDB but not not LocalStorage or over the web. This is nicely explained in Charles Engleke's blog "Saving Cryptographic Keys in the Browser". But how do these objects actually work? Is there a way to tell from JS if an object is opaque or not? I am having trouble finding any information on this. 回答1: There isn't a magical "opaque flag" anywhere. "Opaque" here just means there is data held in

Working with opaque types (Char and Long)

守給你的承諾、 提交于 2020-01-02 04:11:14
问题 I'm trying to export a Scala implementation of an algorithm for use in JavaScript. I'm using @JSExport . The algorithm works with Scala Char and Long values which are marked as opaque in the interoperability guide. I'd like to know (a) what this means; and (b) what the recommendation is for dealing with this. I presume it means I should avoid Char and Long and work with String plus a run-time check on length (or perhaps use a shapeless Sized collection) and Int instead. But other ideas