【问题标题】:Websharper compiler can't translate other assembliesWebshaper 编译器无法翻译其他程序集
【发布时间】:2015-09-29 20:40:56
【问题描述】:

我的目标是简单地输出一个包含我翻译的 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,则会收到相同的错误。

【问题讨论】:

    标签: f# websharper


    【解决方案1】:

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

    <WebSharperProject>Library</WebSharperProject>
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      • 1970-01-01
      相关资源
      最近更新 更多