简体   繁体   中英

Websharper compiler can't translate other assemblies

my goal is to simply output a javascript file containing my translated F# library. Nothing more.

I have an empty solution to which I added two F# projects. One is a library called WSLib with a single file:

namespace WSLib

[<ReflectedDefinition>]
type Class1() = 
    member this.X = "F#"

[<ReflectedDefinition>]
module Foo =
  let bar = 34

The other project is a console app and references the WebSharper and WebSharper.Compiler NuGet packages. It has a single file. I copied the first half of the code from http://www.fssnip.net/snippet/rP .

module Program

open Microsoft.FSharp.Quotations
open WebSharper
type AR = IntelliFactory.Core.AssemblyResolution.AssemblyResolver

module FE = WebSharper.Compiler.FrontEnd

let compile (expr: Expr) : string option =
    let loader = FE.Loader.Create (AR.Create()) (eprintfn "%O")
    let options =
        { FE.Options.Default with
            References =
                List.map loader.LoadFile [
                    // These contain the JavaScript implementation for most of the standard library
                    "WebSharper.Main.dll"
                    "WebSharper.Collections.dll"
                    "WebSharper.Control.dll"
                    "WSLib.dll"
                    // Add any other assemblies used in the quotation...
                ] }
    let compiler = FE.Prepare options (sprintf "%A" >> System.Diagnostics.Debug.WriteLine)
    compiler.Compile expr
    |> Option.map (fun e -> e.ReadableJavaScript)

[<JavaScript>]
let main() =
  let a = WSLib.Class1().X
  let b = WSLib.Foo.bar
  (a,b)

let code = 
  match (compile <@ main() @>) with
  |None -> failwith "parse failed"
  |Some x -> x

open System.IO

let filePath = Path.Combine(System.Environment.CurrentDirectory, "index.js") 
File.WriteAllText(filePath, code) 

I get a couple of errors:

{Location = {ReadableLocation = "main";
             SourceLocation = null;};
 Priority = Error;
 Text = "Failed to translate property access: X [WSLib.Class1].";}
{Location = {ReadableLocation = "main";
             SourceLocation = null;};
 Priority = Error;
 Text = "Failed to translate property access: bar [WSLib.Foo].";}

What do I need to do to get the websharper compiler working with different projects? I get the same error if I include the WebSharper package on WSLib and replace ReflectedDefinition with JavaScript .

What happens here is that adding WSLib.dll to the compiler references will only make it look for WebSharper metadata in that assembly, if there is any; but WSLib needs to be WebSharper-compiled already. For this to happen, you need to reference WebSharper in WSLib (as you did) and add the following property to the project file:

<WebSharperProject>Library</WebSharperProject>

to instruct WebSharper that it does have to compile this assembly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM