BT

InfoQ Homepage News Transcompiling F# to Javascript with Fable

Transcompiling F# to Javascript with Fable

This item in japanese

Bookmarks

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

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.

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

Community comments

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

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

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.