Skip to content

Instantly share code, notes, and snippets.

@Tarmil
Last active November 28, 2016 14:02
Show Gist options
  • Save Tarmil/98cb7a0fb78e22aa4fde to your computer and use it in GitHub Desktop.
Save Tarmil/98cb7a0fb78e22aa4fde to your computer and use it in GitHub Desktop.
Compile a quotation with WebSharper.Compiler
// NuGet reference: WebSharper.Compiler
open WebSharper
type AR = IntelliFactory.Core.AssemblyResolution.AssemblyResolver
module FE = WebSharper.Compiler.FrontEnd
let compile (expr: Microsoft.FSharp.Quotations.Expr) : string option =
let loader = FE.Loader.Create (AR.Create()) (eprintfn "%O")
let options =
{ FE.Options.Default with
References =
[
// Needed for the example belowe because it contains the proxy for (+)
"WebSharper.Main.dll"
// Add any other assemblies you need...
]
|> List.map loader.LoadFile }
let compiler = FE.Prepare options (eprintfn "%O")
compiler.Compile expr
|> Option.map (fun e -> e.ReadableJavaScript)
// Example call
printfn "%s" (defaultArg (compile <@ 1 + 1 @>) "Compilation failed.")
// The output is actually a full WebSharper source file that needs the WebSharper runtime
// and defines a global field WebSharper.EntryPoint.Example.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment