自定义各种Typescript输入文件的特定输出文件 [英] Customizing the specific output files for various Typescript input files

查看:83
本文介绍了自定义各种Typescript输入文件的特定输出文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用TypeScript的Web项目,该项目对编译后的输出文件有一些相当复杂的要求.因此,举例来说,我需要将一个目录中的所有* .ts文件编译成一个单独的.js文件,而将另一个目录中的所有* .ts文件编译成一个不同的.js文件. (比这还复杂,但是您明白了.)

I've got a web project using TypeScript that has some reasonably complex requirements for the compiled output files. So for instance, I need all the *.ts files in one directory to compile down to one single .js file, and all the *.ts files in another directory to compile down to a different .js file. (It's more complex than that, but you get the idea.)

我已经能够使用tsc.exe命令行,输入文件和诸如此类的东西使它正常工作,但是我希望能够使用MSBuild .targets文件-除其他外,还可以使用tsc命令行中的.exe在连续集成服务器上似乎不太受支持,因为它可以位于谁知道的位置,而且肯定不会出现在路径中.

I've been able to get this working using the tsc.exe command-line, using input files and what-not, but I'd like to be able to use MSBuild .targets files - among other things, using tsc.exe from the command-line seems to be pretty poorly supported on continuous integration servers, where it can be located who-knows-where, and certainly isn't likely to be in the path.

根据此答案此处,看来我应该能够使用自定义构建目标来执行此操作.因此,我创建了一个自定义版本的Microsoft.TypeScript.targets,除了默认的"CompileTypeScript"目标外,我还创建了第二个目标"PayboardApiV10",因此相关部分如下所示:

According to this answer here, it seems like I should be able to do this using custom build targets. So I've created a custom version of Microsoft.TypeScript.targets, and in addition to the default "CompileTypeScript" target, I've created a second one, "PayboardApiV10", so that the relevant part looks like so:

  <Target Name="CompileTypeScript" Condition="'$(BuildingProject)' != 'false'">
    <Message Text="Compiling TypeScript files normally" Importance="high"/>
    <VsTsc
      ToolPath="$(TscToolPath)"
      ToolExe="$(TscToolExe)"
      Configurations="$(TypeScriptBuildConfigurations)"
      FullPathsToFiles="@(TypeScriptCompile)"
      YieldDuringToolExecution="$(TscYieldDuringToolExecution)"
      OutFile="$(TypeScriptOutFile)"
      OutDir="$(TypeScriptOutDir)"
      >
      <Output TaskParameter="GeneratedJavascript" ItemName="GeneratedJavascript" />
    </VsTsc>
  </Target>

  <Target Name="PayboardApiV10" Condition="'$(BuildingProject)' != 'false'">
    <Message Text="Compiling TypeScript files for Payboard API v1.0" Importance="high"  />
    <VsTsc
      ToolPath="$(TscToolPath)"
      ToolExe="$(TscToolExe)"
      Configurations="$(TypeScriptBuildConfigurations)"
      FullPathsToFiles="@(TypeScriptCompile)"
      YieldDuringToolExecution="$(TscYieldDuringToolExecution)"
      OutFile="Payboard.js"
      OutDir="$(ProjectDir)api\v1.0\"
      >
      <Output TaskParameter="GeneratedJavascript" ItemName="GeneratedJavascript" />
    </VsTsc>
  </Target>

然后,我在项目配置中为要由"PayboardApiV10"构建目标获取的特定文件指定了"CustomTool",如下所示:

And then I've specified a "CustomTool" in my project configuration for the specific files that I'd like to get picked up by the "PayboardApiV10" build target, like so:

我应该注意,我不知道我是否正确地做到了这一点.我似乎找不到任何文档,而我唯一能找到的例子就是从

I should note that I have no idea if I'm doing this bit correctly. I can't seem to find any documentation on it, and the only examples I've been able to find are from that previous answer. And more to the point, when I run my builds, all the TS files in my project get caught up in the first build target, including the ones for which I've specified "MSBuild:PayboardApiV10" for the custom tool. The "PayboardApiV10" tool never seems to get run, i.e., I never see the message "Compiling TypeScript files for Payboard API v1.0".

有两个问题:

(1)是否有更好的方法来做我想做的事情?

(1) Is there a better way to do what I'm trying to do?

(2)如果通常这是正确的方法,那么关于我在做什么的任何想法?

(2) If this is generally the right way to do it, any ideas as to what I'm doing wrong?

推荐答案

我最终在TypeScript论坛上问了同样的问题( https://typescript.codeplex.com/discussions/455781 ).我从那里的对话中得出的结论:

I ended up asking this same question over on the TypeScript forums (https://typescript.codeplex.com/discussions/455781). The conclusions I drew from the conversation there:

  • 现在没有很好的方法.

  • There isn't a great way to do it now.

现在最好的黑客方式可能就是我这样做的方式,即使用批处理文件,生成后事件以及将编译后的文件检入源代码.其他人则推荐使用Grunt或ASP.NET MVC捆绑机制(在我的方案中后者不起作用).我过去也曾使用过r.js缩小器.这些将起作用;但要重申一下,这些都不是非常好的解决方案.

The best hacky way to do it now is probably the way that I was doing it, namely, with batch files, post-build events, and checking the compiled files into source. Other folks recommended Grunt, or the ASP.NET MVC bundling mechanism (the latter won't work in my scenario); I've also used the r.js minifier in the past. And these will work; but to reiterate, none of these are really very good solutions.

将来最好的方法是创建TypeScript文件的库项目",以便将给定项目中的所有TS文件一起编译.然后您可以从主Web项目中引用库TS项目,并且将自动将编译后的文件合并到主~/Scripts文件夹中.但这需要TS团队的支持-乔恩·特纳(Jon Turner)基本上表示会来的,尽管他没有透露具体时间. (另请参见 https://typescript.codeplex.com/workitem/571 .)

The best way to do it in the future will be to create "library projects" of TypeScript files, so that all TS files in a given project get compiled together; and then you can reference the library TS projects from your main Web project, and will automatically get the compiled files merged into your main ~/Scripts folder. But that will require support from the TS team - which Jon Turner basically indicated would be coming, though he didn't say when. (See also https://typescript.codeplex.com/workitem/571.)

这篇关于自定义各种Typescript输入文件的特定输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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