typescript TypeScriptCompile 构建操作似乎没有做任何事情

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12687956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:10:38  来源:igfitidea点击:

TypeScriptCompile build action doesn't seem to do anything

typescript

提问by Wvdd007

When I add a new TypeScript file to my solution, the system nicely gives me a (prefilled) .ts file with the .js file just below. This configuration makes me think that the conversion from TypeScript to JavaScript is automatic. This assumption is strengthened by the fact that the .ts file has a Build Action 'TypeScriptCompile'. However, regardless of what I do (save a changed ts, compile the project ...), nothing really happens.

当我向我的解决方案添加一个新的 TypeScript 文件时,系统很好地为我提供了一个(预填充的).ts 文件,.js 文件就在下面。这个配置让我觉得从 TypeScript 到 JavaScript 的转换是自动的。.ts 文件具有构建操作“TypeScriptCompile”这一事实加强了这一假设。但是,无论我做什么(保存更改的 ts,编译项目...),都没有发生任何事情。

I know I can add prebuild steps to the project but I would really like the TypeScriptCompile build action to automatically kick in so that I just need to save the file for the .js file to be regenerated. What do I need to do to make 'TypeScriptCompile' from working as expected/desired ?

我知道我可以向项目添加预构建步骤,但我真的希望 TypeScriptCompile 构建操作自动启动,这样我只需要保存文件即可重新生成 .js 文件。我需要做什么才能使“TypeScriptCompile”按预期/期望工作?

回答by Per Brage

I had the same problem when trying to use TypeScript in an MVC4 project, but resolved the problem!

我在 MVC4 项目中尝试使用 TypeScript 时遇到了同样的问题,但问题解决了!

Put the following lines into your project file using your favorite text editor. There is a commented out BeforeBuildsection usually already there at the end of the proj file, which you can replace with these lines. Reload the project and you are good to go. The .js files will now be generated during compilation.

使用您喜欢的文本编辑器将以下几行放入您的项目文件中。BeforeBuild在 proj 文件的末尾通常已经有一个注释掉的部分,您可以用这些行替换它。重新加载项目,您就可以开始了。现在将在编译期间生成 .js 文件。

<Target Name="BeforeBuild">
  <Exec Command="&quot;$(PROGRAMFILES)\Microsoft SDKs\TypeScript
<Target Name="BeforeBuild">
  <Exec Command="&quot;tsc&quot; @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />
</Target>
.8.0.0\tsc&quot; @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" /> </Target>

Have a good look at the exact path though, it may differ (e.g. if you have a different version of the Typescript SDK). Similarly, if tsc.exeis already in your PATHenvironment variable, you can use a version-agnostic command:

仔细看看确切的路径,它可能会有所不同(例如,如果您有不同版本的 Typescript SDK)。同样,如果tsc.exe已经在您的PATH环境变量中,您可以使用与版本无关的命令:

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.Default.props" Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.Default.props')" />

Finally, as noted in the comments, you might want to add -c to preserve comments to the command line. This allows tools like cassette & asp.net's resource bundling to retrieve the references and render things in the correct order.

最后,如注释中所述,您可能希望添加 -c 以保留对命令行的注释。这允许像盒式磁带和 asp.net 的资源捆绑工具这样的工具来检索引用并以正确的顺序呈现事物。

回答by Jeremy Danyow

A few people have had this issue, including myself. The fix is to manually install the typescript visual studio extension.

有几个人遇到过这个问题,包括我自己。修复方法是手动安装 typescript Visual Studio 扩展

The installer is here:

安装程序在这里:

"C:\Program Files (x86)\Microsoft SDKs\TypeScript\0.8.0.0\TypeScriptLanguageService.vsix"

“C:\Program Files (x86)\Microsoft SDKs\TypeScript\0.8.0.0\TypeScriptLanguageService.vsix”

or here on x86 machines:

或在 x86 机器上:

"C:\Program Files\Microsoft SDKs\TypeScript\0.8.0.0\TypeScriptLanguageService.vsix"

“C:\Program Files\Microsoft SDKs\TypeScript\0.8.0.0\TypeScriptLanguageService.vsix”

回答by Arrow

If you still have problems compiling typescript files, you might want to check out an app I just released. You can compile the files with just 1 click of a button.

如果您在编译打字稿文件时仍然遇到问题,您可能需要查看我刚刚发布的应用程序。您只需单击一下按钮即可编译文件。

回答by Jozef Benikovsky

If you use Visual Studio 2013 with latest TypeScript 1.5, you may need to install TypeScript tools for Visual Studio. I also recommed to install Web Essentialswhich will invoke .ts script file compilation on save (besides many more other useful features).

如果您使用带有最新 TypeScript 1.5 的 Visual Studio 2013,您可能需要为 Visual Studio安装TypeScript 工具。我还建议安装Web Essentials,它会在保存时调用 .ts 脚本文件编译(除了许多其他有用的功能)。

Then follow the steps Visual Studio makes for new web projects in .csproj file.

然后按照 Visual Studio 为 .csproj 文件中的新 Web 项目创建的步骤进行操作。

1) Import MSBuild TypeScript properties by adding following element under root Projectelement:

1) 通过在根Project元素下添加以下元素来导入 MSBuild TypeScript 属性:

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets" Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets')" />

2) Import MSBuild TypeScript build target

2) 导入 MSBuild TypeScript 构建目标

<TypeScriptToolsVersion>1.5</TypeScriptToolsVersion> 

3) Configure target TypeScript version under Project/PropertyGroupelement

3)在Project/PropertyGroupelement下配置目标TypeScript版本

##代码##

回答by Wvdd007

Ok, I have reinstalled everything, including the vsix file. Apparently when I "run" my project now, the .ts file is automatically converted into a .js file (without custom build actions). That's ok for now. It would have been better if these files follow other code generators in that they compile when the source file gets saved. I would write my own SingleFileGenerator implementation for it but I do not yet know how to call the .js from c#.

好的,我已经重新安装了所有内容,包括 vsix 文件。显然,当我现在“运行”我的项目时,.ts 文件会自动转换为 .js 文件(没有自定义构建操作)。暂时没问题。如果这些文件遵循其他代码生成器会更好,因为它们会在源文件保存时进行编译。我会为它编写自己的 SingleFileGenerator 实现,但我还不知道如何从 c# 调用 .js。