Websharper编译器无法转换其他程序集

时间:2015-09-24 17:01:45

标签: f# websharper

我的目标是简单地输出包含我翻译的F#库的javascript文件。没什么。

我有一个空的解决方案,我添加了两个F#项目。一个是名为WSLib的库,只有一个文件:

namespace WSLib

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

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

另一个项目是一个控制台应用程序,它引用了WebSharperWebSharper.Compiler NuGet包。它有一个文件。我从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) 

我收到了一些错误:

{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].";}

如何让websharper编译器使用不同的项目,我需要做些什么?如果我在WSLib上包含WebSharper包并将ReflectedDefinition替换为JavaScript,我会收到同样的错误。

1 个答案:

答案 0 :(得分:3)

这里发生的事情是,将WSLib.dll添加到编译器引用只会使它在该程序集中查找WebSharper元数据(如果有的话);但WSLib已经需要进行WebSharper编译。为此,您需要在WebSharper中引用WSLib(正如您所做的那样)并将以下属性添加到项目文件中:

<WebSharperProject>Library</WebSharperProject>

指示WebSharper必须编译此程序集。