【问题标题】:Running Closure Compiler to get warning messages运行 Closure Compiler 以获取警告消息
【发布时间】:2015-06-07 16:52:42
【问题描述】:

我正在使用在本地机器上运行的 Closure Compiler 来生成客户端文件。我还使用http://closure-compiler.appspot.com/home 的在线关闭工具来检查我的工作:我粘贴我的 JavaScript 代码,按编译,如果有警告,编译器会向我显示带有实际行和行号的警告。

我想知道是否可以使用本地版本的 Closure Compiler 获得相同的输出。这是我用来编译文件并获取已编译 JavaScript 的代码,但这次,我想要警告:

System.IO.File.WriteAllText(FileNameInput, TheJS);

string JavaArgument = "-jar ClosureCompiler/compiler.jar --js ClosureCompiler/initial" + FileNameSuffix + ".js --js_output_file ClosureCompiler/compiled" + FileNameSuffix + ".js --compilation_level ADVANCED_OPTIMIZATIONS --externs ExternalFiles/JqueryExtern.js";

System.Diagnostics.Process clientProcess = new System.Diagnostics.Process();

clientProcess.StartInfo.FileName = "java.exe";
clientProcess.StartInfo.Arguments = JavaArgument;
clientProcess.StartInfo.CreateNoWindow = true;
clientProcess.StartInfo.UseShellExecute = false;
clientProcess.StartInfo.WorkingDirectory = HttpRuntime.AppDomainAppPath;

clientProcess.Start();
clientProcess.WaitForExit();

string TheOutputScript = System.IO.File.ReadAllText(FileNameOutput);

我需要改变什么?

【问题讨论】:

    标签: c# asp.net google-closure-compiler


    【解决方案1】:

    编译器将警告和错误写入标准错误,不会显示在输出文件中。

    选项 1 - 将标准错误重定向到文件

    2> errorfile.txt 添加到执行命令的末尾以将标准错误重定向到文件。然后,您需要读取该文件。

    选项 2 - 读取进程的标准错误属性

    这应该很简单:

    clientProcess.StandardError
    

    【讨论】:

    • 感谢您的回答:我修改了我的 javascript 以生成编译错误,并且当我添加 var t = clientProcess.StandardError;在 WaitForExit() 行之后,我得到一个异常“StandardError 没有被重定向。”
    • 查看使用标准错误属性的注意事项:msdn.microsoft.com/en-us/library/…
    • 刚刚意识到这只会得到错误;我们如何也得到警告?
    【解决方案2】:

    Chad 的回答很棒,因为它把我引向了代码。对于那些需要实际代码的人,这就是我所拥有的:

    clientProcess.StartInfo.FileName = "java.exe";
    clientProcess.StartInfo.Arguments = JavaArgument;
    clientProcess.StartInfo.CreateNoWindow = true;
    clientProcess.StartInfo.UseShellExecute = false;
    clientProcess.StartInfo.WorkingDirectory = HttpRuntime.AppDomainAppPath;
    clientProcess.StartInfo.RedirectStandardError = true; //add this line
    
    clientProcess.Start();
    string CompilerErrors = clientProcess.StandardError.ReadToEnd(); //add this line
    clientProcess.WaitForExit();
    
    return System.IO.File.ReadAllText(FileNameOutput); //add breakpoint here
    

    现在您所要做的就是在末尾添加一个断点并观察变量 CompilerErrors。

    【讨论】:

      猜你喜欢
      • 2011-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 2010-09-18
      • 1970-01-01
      • 2018-10-21
      • 2016-08-21
      相关资源
      最近更新 更多