scala.js

sbt play cross build project setup: uTest runner doesn't seperate client/server projects correctly

五迷三道 提交于 2019-12-06 11:41:12
I'm using an build.sbt which has cross compile settings and basically is an adapted version of " Play with scala-js example " and having some trouble getting a clean setup for my tests. Specifically, when running my server tests, my client tests also get executed (which is something that I want to avoid). I followed the instructions from Cannot get uTest to see my tests and added libraryDependencies += "com.lihaoyi" %%% "utest" % "0.3.0" My tests for some reason didn't get executed until I added testFrameworks += new TestFramework("utest.runner.Framework") to every project definition. Also not

How can I read a text file with scala.js?

佐手、 提交于 2019-12-06 06:04:57
Basically I'm trying to figure out what I need to pass to the onload() method def selectedFile(e: ReactEventI) = { val reader = new dom.FileReader() reader.readAsText(e.currentTarget.files.item(0)) reader.onload( ) } You can assign a lambda to the onload handler: reader.onload = (e: UIEvent) => { // Cast is OK, since we are calling readAsText val contents = reader.result.asInstanceOf[String] println(contents) } Idiomatic Scala, with error handling Instead of using callbacks, I prefer to work with pattern matching and map like this: def printFileContent(file: dom.File) = readTextFile(file).map

Working with opaque types (Char and Long)

核能气质少年 提交于 2019-12-05 09:37:05
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 welcome. More detail... The kind of code I'm looking at is: @JSExport("Foo") class Foo(val x: Int) {

How to invoke nodejs modules from scala.js?

佐手、 提交于 2019-12-04 11:36:41
问题 I'm trying to use scala.js + nw.js to write some application, and will use some node modules in scala.js. But I'm not sure how to do it. Say, there is module fs and I can write such code in Javascript: var fs = require('fs'); fs.writeFile("/tmp/test", "Hey there!", function(err) { if(err) { console.log(err); } else { console.log("The file was saved!"); } }); But how to do the same in scala.js from scratch? 回答1: Using js.Dynamic and js.DynamicImplits (see also a longer answer on the topic),

Scala-JS for real web project [closed]

一笑奈何 提交于 2019-12-04 07:48:34
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago . Has someone used Scala-JS in real web project but not only for plain JavaScript replacement in isolated env ? I would like to use Scala as much as possible (I wish I could). And seems Scala-JS claims to be that lib I could use (now) or in the future. That's why I'm interested

scala.js — getting complex objects from JavaScript

怎甘沉沦 提交于 2019-12-04 06:49:57
I'm trying scala.js and I must say that it's completely impressed! However, I try to introduce it into our production little-by-little, working side-by-side with existing JavaScript code. One thing I'm struggling with is passing complex structures from JS to Scala. For example, I have ready-made JS object that I've got from the other JS module: h = { "someInt": 123, "someStr": "hello", "someArray": [ {"name": "a book", "price": 123}, {"name": "a newspaper", "price": 456} ], "someMap": { "Knuth": { "name": "The Art of Computer Programming", "price": 789 }, "Gang of Four": { "name": "Design

How to convert Scala.js application to CommonJS module?

*爱你&永不变心* 提交于 2019-12-04 06:01:45
Is there any standard way to use Scala.js application as a libriary in CommonJS environment? And if not, can I patch generated js file for that purpose? Scala.js 0.6.13 and onward Put this in your build file: scalaJSModuleKind := ModuleKind.CommonJSModule Scala.js 0.6.5 to 0.6.12 Put this in your build file (explanation see below): scalaJSOutputWrapper := ("var __ScalaJSEnv = { exportsNamespace: exports };", "") Scala.js 0.5.4 to 0.6.4 You can tell Scala.js where to send the stuff it exports. To make a CommonJS module, simply prepend this: var __ScalaJSEnv = { exportsNamespace: exports }; to

Using scala.js to compile only (and not override run) in SBT

好久不见. 提交于 2019-12-04 03:35:02
问题 I'm trying to use scalajs to just compile some scala sources to javascript and not modify anything else about the sbt environment, I don't want it to override the default behavior of the "run" sbt command. Currently I've got a build.sbt that looks like: import ScalaJSKeys._ scalaJSSettings name := "foo" organization := "com.example" scalaVersion := "2.11.4" compile <<= (compile in Compile) dependsOn (fastOptJS in Compile) crossTarget in (fastOptJS in Compile) := ((classDirectory in Compile)

How to export properties of shared case classes

…衆ロ難τιáo~ 提交于 2019-12-04 03:32:45
I am trying to share a case class between server and client. I am using upickle on both ends. The objects and their data are nicely available on both ends. shared class case class Foo(var id : Long,var title: Description) However i need to export the fields of the case class on the client side. I could add the @ExportAll annotation, but that means pulling in the scalajs libraries on the server project. Is there perhaps a better way of exposing the members id and title to the javascript. tx., The right way to export things to JavaScript is to use the @JSExportAll annotation. You cannot, and

How do I port an existing Scala library to scalajs?

别说谁变了你拦得住时间么 提交于 2019-12-03 17:11:41
问题 I'm new to Scala.js. I'd like to use the Argonaut json library. https://github.com/argonaut-io/argonaut Its only dependencies appear to be Monocle and Scalaz which both have versions compiled for Scala.js. I'd be happy to work on porting Argonaut to Scala.js, but don't have a firm idea on how to begin. Does anyone have any pointers? Thanks. 回答1: Quick proof-of-concept The first thing to try is to convert the build so that the JVM projects become Scala.js projects. The basis for this is pretty