如何获取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)

查看:186
本文介绍了如何获取Visual Studio 2017以在类库项目的构建中编译TypeScript(新的.net核心样式.csproj)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前正在运行VS 2017 15.3.5和Typescript 2.5.2 SDK.我如何获取TypeScript以在构建(常规MSBuild)上进行编译. csproj是新的".net核心样式",项目类型是类库.

Currently running VS 2017 15.3.5 and Typescript 2.5.2 SDK. How do I get TypeScript to compile on build (regular MSBuild). csproj is the new ".net core style" and project type is class library.

推荐答案

在这里看到两个好的答案之前,这是我尝试的方法:

Before seeing the two good answers here this is what I tried:

首先尝试添加.csproj的某些部分,这些部分在谷歌搜索和查看旧" csproj文件时找到了(在转换为新的VS2017/.net核心样式之前)

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)

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

当我关闭解决方案并重新打开时,系统提示我有关该项目的TypeScript版本的问题,该问题设置为2.3,但是有2.5版本可用,如果我想升级该项目.我回答是.现在我得到了TypeScript Build 项目属性中的选项卡,可以正确识别tsconfig.json文件.

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. I answered yes. Now I got the TypeScript Build Tab in project properties which correctly recognized the tsconfig.json file.

我希望在build上编译现在可以,但是没有.

I was hoping that compile on build would noW work, but it didn't.

我还添加了:

    <TypeScriptToolsVersion>2.5</TypeScriptToolsVersion>

.csproj文件中的第一个PropertyGroup.这也没做任何事情.

to the first PropertyGroup in .csproj file. This didn't really do anything either.

然后尝试添加:

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

带有TypeScriptCompile元素的所有文件都可以编译.没有运气.

with TypeScriptCompile elements for all the files to compile. No luck.

由于我已经在项目中设置了Gulp(实际上是从Gulp使用MSBuild来编译打字稿),所以我决定从在Gulp中调用的MSBuild转换为在NFP模块中使用NPM模块"typescript"和"gulp-typescript"代替Gulp任务:

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.task("Build", function () {
return gulp.src('./' + packageName + '.csproj')
    .pipe(msbuild(
        {
            stderr: true,
            stdout: true,
            toolsVersion: 15.0
        }));
});

已添加:

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(""));
});

然后,我将"CompileTypeScript" Gulp任务绑定到Visual Studio中的构建之前".效果很好,尽管我想知道是否也可以使用新的csproj格式.

Then I bound the "CompileTypeScript" Gulp task to Before Build in Visual Studio. Works fine, although I would like to know if this is possible to get to work with the new csproj format as well.

感谢@Balah和@Joshit的好的建议,他们可能也能正常工作.

Thanks for good suggestions @Balah and @Joshit, they probably work just fine as well.

编辑2018年2月8日:修复了一些拼写错误并更正了相关的gulp任务名称(与任务名称相同,这当然不起作用)

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)

有关更多信息,请参见这篇文章

For more info, see this article

这篇关于如何获取Visual Studio 2017以在类库项目的构建中编译TypeScript(新的.net核心样式.csproj)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆