BT

Transcompiling F# to Javascript with Fable

| by Pierre-Luc Maheu on Aug 22, 2016. Estimated reading time: 1 minute |

Fable is a F# to Javascript transpiler. It can compile an F# script or a whole F# project to Javascript. Fable is executed through a command line interface, giving the option to either compile an .fsx script or a whole project. It is distributed as an npm package.

Fable supports most of the F# core library and a bit of .NET BCL. All basic types are supported fully or with some specific exceptions. One notable exception is async. The async computation expression works as expected, minus RunSynchronously which is not available.

Fable can be extended through user created plugins. Fable plugins are usually used to replace calls to external libraries during the compilation process. Creating a plugin is done by implementing the IReplacePlugin interface and its single method TryReplace. The following shows the skeleton of a plugin to replace System.Random:

type RandomPlugin() =
   interface IReplacePlugin with
   member x.TryReplace com (info: Fable.ApplyInfo) =
     match info.ownerFullName with
     | "System.Random" ->
        match info.methodName with
        | ".ctor" -> failwith "TODO"
        | "Next" -> failwith "TODO"
        | _ -> None
     | _ -> None

Plugins can be used to handle more complex scenarios, such as translating test frameworks. NUnit is currently supported through a plugin translating tests to Mocha.

Mobile apps can also be created with Fable through the use of React Native. React Native makes it possible to compile React applications to Android and iOS apps. A Fable plugin provides the bindings to React Native. It must be noted that the mobile app support through React Native is an experimental feature, subject to frequent changes.

Fable is an open source project available on GitHub. Documentation and code samples can be found on Fable's website. For an example of usage in a full-fledged project, Ionide, a plugin for F# editing in VsCode and Atom, is powered by Fable since its 2.0 release.

Rate this Article

Relevance
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

This is very interesting! by Shirley Perkins

I think you might like to know about AngularJS vs. ReactJS 2016 survey results: blog.testproject.io/2016/09/01/front-end-develo...

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

1 Discuss
BT