0

I'm trying to write a new library that would work in Scala.js. I have written some of the implementations of the classes and methods in Javascript. How do I set it up so that a user can code in Scala.js (Scala)?

I've looked at some Scala.js libraries on GitHub, but these do not show the Javascript code; they all appear to be .scala files.

So how does one actually create a new library for Scala.js?

Edit: The main code for the library must be written in Javascript since it takes advantage of the Javascript audio api.

3
  • There are relevant docs: calling JavaScript from Scala.js or exporting scala.js API to javascript (nor clear which one from your description) Commented Jun 24, 2014 at 14:58
  • Indeed. Additionally, to answer the "The main code for the library must be written in Javascript since it takes advantage of the Javascript audio api.": No it doesn't. Scala.js is precisely very good at letting you call arbitrary JavaScript APIs directly from your .scala code. See the first link given by @om-nom-nom for this use case. Commented Jun 24, 2014 at 15:25
  • Both of @om-nom-nom links seem to be the same (exporting), the first link should be calling JavaScript from Scala.js. Commented Oct 17, 2014 at 16:18

1 Answer 1

1

Somewhat agreeing with the comment from @sjrd, it should be possible to write your JavaScript-targeted API directly within Scala.js (See Calling JavaScript from Scala.js ).

Alternatively, there may also be the possibility for 'importing' or converting your existing JavaScript code into Scala.js '.scala' files in a strongly-typed-Scala manner, so:

  1. Convert your JavaScript file to TypeScript, start by changing the file extension from '.js' to '.ts'.
  2. Process your TypeScript file(s) created in #1 into valid Scala[JS] files using @sjrd's (!) TypeScript-to-ScalaJS importer @ Github.
  3. Finally, develop your code against Scala.js, using the '.scala' files generated in #2

As TypeScript is a strongly-typed superset of JavaScript, changing the extension alone might be enough - otherwise, after running and failing step #2, you might need to refine you TypeScript'd library, this post, 'How to compile plain *.js (JavaScript) files with the TypeScript Compiler', should help with that.

The 'DefinitelyTyped' code repository @ Github contains a collection of JavaScript libraries updated to TypeScript, the webaudio API is one of them (so this could be converted and used within Scala.JS using some of the process outlined above).

I haven't [yet] personally tested this myself, in anger, I'd be interested in whether you get any mileage out of this tool set/process.

Here are some extra TypeScript resources:

http://en.wikipedia.org/wiki/TypeScript
http://www.sitepen.com/blog/2013/12/31/definitive-guide-to-typescript

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.