BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Transcompiling F# to Javascript with Fable

Transcompiling F# to Javascript with Fable

This item in japanese

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

Adoption
Style

BT