简体   繁体   English

如何让Visual Studio 2017在类库项目中构建TypeScript(新的.net核心样式.csproj)

[英]How to get Visual Studio 2017 to compile TypeScript on build in class library project (new .net core style .csproj)

Currently running VS 2017 15.3.5 and Typescript 2.5.2 SDK. 目前正在运行VS 2017 15.3.5和Typescript 2.5.2 SDK。 How do I get TypeScript to compile on build (regular MSBuild). 如何在构建(常规MSBuild)上编译TypeScript。 csproj is the new ".net core style" and project type is class library. csproj是新的“.net核心样式”,项目类型是类库。

Since you want to use MSBuild, you can edit the .csproj file as follows (I'm using TypeScript 2.5): 由于您要使用MSBuild,您可以按如下方式编辑.csproj文件(我正在使用TypeScript 2.5):

<Target Name="PostBuild" AfterTargets="PostBuildEvent">
  <Exec Command="&quot;C:\Program Files (x86)\Microsoft SDKs\TypeScript\2.5\tsc.exe&quot; -p tsconfig.json" />
</Target>

It's not ideal, because that is a hardcoded path, so I suggest adding C:\\Program Files (x86)\\Microsoft SDKs\\TypeScript\\2.5\\ to your Path environment variable. 这不是理想的,因为这是一个硬编码路径,因此我建议将C:\\Program Files (x86)\\Microsoft SDKs\\TypeScript\\2.5\\到Path环境变量中。 then you can just use a cleaner command: Command="tsc.exe -p tsconfig.json" . 那么你可以使用一个更干净的命令: Command="tsc.exe -p tsconfig.json"

Entering the command into the post-build (or pre-build if that's what you're looking for) in the properties window of the project produces the same result. 在项目的属性窗口中将命令输入到构建后(或预构建,如果这是您要查找的内容)会产生相同的结果。

post build tsc

Before seeing the two good answers here this is what I tried: 在看到这里的两个好答案之前,这就是我尝试过的:

First tried adding some parts of .csproj that I found referenced when googling and looking at the "old" csproj file (before converting to the new VS2017/.net core style) 首先尝试添加.csproj的一些部分,我发现在google搜索并查看“旧”csproj文件时(在转换为新的VS2017 / .net核心样式之前)

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

When I closed the solution and re-opened I was presented with a question about TypeScript version for the project was set to 2.3, but 2.5 was available and if I wanted to upgrade the project. 当我关闭解决方案并重新打开时,我看到有关该项目的TypeScript版本的问题被设置为2.3,但2.5可用,如果我想升级项目。 I answered yes. 我回答是的。 Now I got the TypeScript Build Tab in project properties which correctly recognized the tsconfig.json file. 现在我在项目属性中获得了TypeScript Build选项卡,它正确识别了tsconfig.json文件。

I was hoping that compile on build would noW work, but it didn't. 我希望编译版本可以正常工作,但事实并非如此。

I also added: 我还补充说:

    <TypeScriptToolsVersion>2.5</TypeScriptToolsVersion>

to the first PropertyGroup in .csproj file. 到.csproj文件中的第一个PropertyGroup。 This didn't really do anything either. 这也没有做任何事情。

Then tried adding: 然后尝试添加:

<ItemGroup>
  <TypeScriptCompile Include="src\sourcefile.ts" />
</ItemGroup>

with TypeScriptCompile elements for all the files to compile. 使用TypeScriptCompile元素编译所有文件。 No luck. 没运气。

Since I already had Gulp set up in the project (and MSBuild was actually used from Gulp to compile the typescript) I decided to switch from MSBuild called from Gulp to using NPM modules "typescript" and "gulp-typescript" in a Gulp task instead: 由于我已经在项目中设置了Gulp(并且实际上是从Gulp使用MSBuild来编译打字稿)我决定从Gulp调用的MSBuild切换到在Gulp任务中使用NPM模块“typescript”和“gulp-typescript” :

REMOVED: 删除:

gulp.task("Build", function () {
return gulp.src('./' + packageName + '.csproj')
    .pipe(msbuild(
        {
            stderr: true,
            stdout: true,
            toolsVersion: 15.0
        }));
});

ADDED: 添加:

var tsProject;
gulp.task("CompileTypeScript", ['NameOfAnotherTaskThatThisTaskDependsOn'], function () {
  var ts = require("gulp-typescript");

  if (!tsProject) {
      tsProject = ts.createProject("tsconfig.json");
  }

  var reporter = ts.reporter.fullReporter();
  var tsResult = tsProject.src()
    .pipe(tsProject(reporter));

  return tsResult.js
    .pipe(gulp.dest(""));
});

Then I bound the "CompileTypeScript" Gulp task to Before Build in Visual Studio. 然后我将“CompileTypeScript”Gulp任务绑定到Visual Studio中的Before Build之前。 Works fine, although I would like to know if this is possible to get to work with the new csproj format as well. 工作正常,虽然我想知道是否可以使用新的csproj格式。

Thanks for good suggestions @Balah and @Joshit, they probably work just fine as well. 感谢@Balah和@Joshit的好建议,他们也可能工作得很好。

EDIT 8.february 2018: Fixed some typos and corrected name of dependent gulp task (was the same name as the task, which doesnt work of course) 编辑8. 2018年2月:修正了一些拼写错误并更正了依赖gulp任务的名称(与任务名称相同,当然不起作用)

For more info, see this article 有关详细信息,请参阅此文章

here is a description for more build tools including msbuild: 这里是更多构建工具的描述,包括msbuild:

The simpliest way: 最简单的方法:

  1. Right-Click -> Manage NuGet Packages 右键单击 - >管理NuGet包
  2. Search for Microsoft.TypeScript.MSBuild 搜索Microsoft.TypeScript.MSBuild
  3. Hit Install 点击安装
  4. When install is complete, rebuild! 安装完成后,重建!

To be able to build set the tsconfig.json as content in the project. 为了能够构建,将tsconfig.json设置为项目中的内容。

If you´ve installed WebEssentials plugin, you´ll find several options under Tools -> Options. 如果您已安装WebEssentials插件,则可以在“工具” - >“选项”下找到多个选项。 I think the default setting is Compile on save. 我认为默认设置是保存时编译。 But you can set this option to false and set Compile on build to true. 但是您可以将此选项设置为false并将build上的Compile设置为true。

VS选项

Hope thats what you looking for. 希望这就是你要找的东西。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编译typescript \\ ts文件作为Visual Studio 2017中构建过程的一部分 - compile typescript\ts files as part of build process in Visual Studio 2017 在TeamCity中,如何安装正确的TypeScript库以匹配我的Visual Studio 2017项目? - In TeamCity, how do I install the right TypeScript library to match my Visual Studio 2017 project? Typescript 的热重载在 Visual Studio 2022 中的 Razor class 库中编译 - Hot reload for Typescript compile in Razor class library in Visual Studio 2022 如何使用Visual Studio构建打字稿项目参考 - How to build typescript project references with visual studio 如何在.csproj C#项目中编译(“构建”)所有TypeScript文件? - How can I compile ('build') all TypeScript files in a .csproj C# project? .net核心控制台应用程序之前的Visual Studio 2015中如何编译打字稿? - How to compile typescript in Visual Studio 2015 in pre- .net core console application? Visual Studio 2017点网核心的Angular 4应用程序。 如何从打字稿文件访问appsetting.json配置密钥 - angular 4 application with Visual studio 2017 dot net core. how to access appsetting.json configuration key from typescript file 如何在 Visual Studio 2019 ASP.NET 核心项目中对 TypeScript 进行单元测试? - How to do unit-testing of TypeScript in a Visual Studio 2019 ASP.NET Core project? Visual Studio 2017 Asp.Net Core 2.1 TypeScript Vue设置 - 非SPA - Visual Studio 2017 Asp.Net Core 2.1 TypeScript Vue Setup - Not SPA 不带JS生成的Visual Studio 2017 ASP.NET核心打字稿编译 - Visual Studio 2017 ASP.NET core typescript compilation without js generation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM