Maxime Mangel

Fable REPL 5, a full rewrite

On this page
  1. A new editor, checked by the F# compiler
  2. Libraries are back
  3. Examples that keep compiling
  4. Settings, themes and languages
  5. Fable.Ripple compared with Elmish
  6. Changes in fable-standalone
  7. A first check that took three minutes
  8. New editor APIs

Fable compiles F# to JavaScript and other languages, and the Fable REPL lets you try it in your browser, with nothing to install.

I had wanted to rework the REPL for a long time. The previous version used an outdated build system, and it could no longer load external libraries, which limited the examples we could show. Monaco, the editor from Visual Studio Code that it used, was also a pain to deal with.

This was also a good opportunity to use Fable.Ripple, my reactive UI library for Fable (the introduction post), on a medium-sized project, and to compare it with my experience building the previous REPL with Elmish, together with Alfonso García-Caro, eight years ago.

The Fable REPL with the Fable.Ripple todo example: examples on the left, the F# code in the middle, and the running page on the right

A new editor, checked by the F# compiler

The editor is now CodeMirror 6, a code editor for web pages, in place of Monaco.

CodeMirror took more setup than Monaco. It knows nothing about F#, so I wrote a tokenizer to colour the code, and built completion, tooltips and diagnostics from its extensions.

Once in place, everything just worked. With Monaco, several editors on one page, or controlling their size, was a nightmare. CodeMirror follows your CSS and doesn't fight it.

As you type, the F# compiler checks your code in a web worker, a background script that does not block the page. From its results, the editor gives you:

  • completion,
  • tooltips on hover,
  • go to definition,
  • rename, for symbols declared in your file,
  • errors and warnings shown in the code, and listed in a Problems panel you can filter, with their number such as FS0039.

The editor colours code in two passes. As you type, the tokenizer colours the code. After each check, the compiler adds semantic highlighting, which is more precise.

Errors and warnings appear on the line itself: a dot in the margin, the code underlined, and the message at the end of the line. You can turn this off in Settings.

Two syntax errors shown inline, each with a red dot in the margin and its message at the end of the line

Libraries are back

Early versions of the REPL could load libraries. That was lost with Fable 3, along with the Elmish and Fulma examples (#121). Even before, support was incomplete: public inline functions did not work in the REPL's libraries, so each library needed custom code.

Now the REPL loads libraries as they are published on NuGet, the .NET package registry, inline functions included. Besides the general examples, it comes with two library sets, groups of packages you pick in the Libraries panel:

  • Ripple: Fable.Ripple, with Thoth.Json for JSON.
  • React: React through Feliz, with Elmish and Thoth.Json.

Both also include Fable.Promise and Glutinum.Web, bindings to the browser APIs. Each set is compiled when the REPL is built, so you cannot add a package from the REPL itself, and the REPL loads one set at a time. Adding a set takes two project files: the README of the repository shows how.

Most libraries work. The exceptions:

  • Compiler plugins cannot run in the browser. With Feliz, declare components with React.memo instead of [<ReactComponent>].
  • Sutil cannot be compiled into a set yet. Its dependencies Feliz.Engine and Feliz.Engine.Event both declare Feliz.Util, and they collide in one assembly. I opened davedawkins/Sutil#105 with a fix.

Examples that keep compiling

The examples are F# files in the repository, and the build type-checks each one against its library set. An update that breaks an example fails the build, so it never reaches the site.

Some use no library, such as a brick breaker and a platformer. Others use Elmish, Feliz or Fable.Ripple, such as Conway's Game of Life.

Settings, themes and languages

Settings are now buttons in a side panel. In the previous REPL they were drop-down lists in a pop-up menu, and in Firefox the menu closed before you could pick an option (#158, #159).

Fable.Ripple made them easy to write. Each setting is one line, such as Setting.bool "typed-arrays" true: a signal saved in the browser and restored on your next visit. The compiler options, the editor's font size and indentation read it, and update as soon as you change it.

The REPL has a light theme, a dark theme, and a system theme that follows your operating system (#71).

The interface is in English, French, Ukrainian, Russian and Kazakh. It follows your browser's language, or the one you chose in the previous REPL.

Fable.Ripple compared with Elmish

The previous REPL kept its state in one Elmish program: a file of 1,118 lines, with a single message type of 36 cases and one update function for all of them.

Elmish is great at making you reason about your system. The price is boilerplate, which is also its strength: everything is explicit.

And, as many Elmish users found out, when a child component needs to talk back to its parent, you have to get creative. I wrote about it in my Elmish tips.

Fable.Ripple makes it easy to write small modules, each responsible for its own state, that other modules can use. The REPL has one for the compiler, one for the draft, one for the settings, and so on. Each holds a few Vars and the functions that change them. You still control what others can see or do, by choosing what is public and what is private.

Side effects are short too. Saving the draft, recompiling when a setting changes, and reloading the preview are each one Signal.autorun: a function that runs again whenever the values it reads change.

The REPL also served to improve Fable.Ripple. The rough edges I hit are fixed in Fable.Ripple 1.0.0-beta.9 (fable-hub/Fable.Ripple#35):

  • Var.Notify re-runs what reads a value without changing it, for example to reload the preview with the same code.
  • attr.onMount runs once the element is in the page, to focus or measure it.
  • attr.aria accepts signals and booleans.
  • Event handlers no longer subscribe to the values they read.

Changes in fable-standalone

The REPL runs fable-standalone, the Fable compiler built for the browser. Building the new REPL led to a few changes in it, released in Fable 5.22.0.

A first check that took three minutes

With the Ripple set loaded, checking a single line took about three minutes the first time.

The cause was Encoding.GetString in Fable's JavaScript library. To read a few bytes, it copied the whole array first. The compiler reads thousands of small strings from multi-megabyte .dll files, so that added up fast.

Now it copies only the bytes it needs, and the first check takes about 1 s instead of 174 s (Fable#5069). Any Fable app that calls Encoding.GetString on a plain JavaScript array gets the fix too.

New editor APIs

The editor features above needed more from the compiler. fable-standalone now gives:

If you use fable-standalone in your own tool, version 4.0.0-beta.1 changes its API.

Try the new REPL at fable.io/repl.

The code is on GitHub, and issues and pull requests are welcome.

More posts

Working on something with these libraries?

I maintain them, so I've probably seen your problem before. A retainer is one way to get help, or you can simply write to me.

Write to me