3

We have an MSBuild script which we use to compile all our .ts files in our project. First we create a propery group containing all the .ts files;

<ItemGroup>
   <AllTypeScriptFiles Include="XXXXX\Scripts\**\*.ts;" Exclude="XXXX\Scripts\**\*.d.ts;" /> 
</ItemGroup>

Then we dump this file list to an input file and run tsc.exe;

<WriteLinesToFile
    File="typescriptcompiler.input"
    Lines="@(AllTypeScriptFiles)"
    Overwrite="true"
    Encoding="Unicode"/>    

<Exec Command="&quot;$(MSBuildProgramFiles32)\Microsoft SDKs\TypeScript\$(TypeScriptVersion)\tsc&quot; --target ES5 @typescriptcompiler.input"
      CustomErrorRegularExpression="\.ts\([0-9]+,[0-9]+\):(.*)"
      IgnoreExitCode="true" >
</Exec>

Now, the output states that some files can not be found;

Error reading file "XXXXX.ts": File not found

This happens to some files, but if I run tsc.exe giving the exact same path as the error message I get no errors and the file is compiled.

If I rather compile each file in sequence instead:

<Exec Command="&quot;$(MSBuildProgramFiles32)\Microsoft SDKs\TypeScript\$(TypeScriptVersion)\tsc&quot; --target ES5 &quot;%(AllTypeScriptFiles.Identity)&quot;"
      CustomErrorRegularExpression="\.ts\([0-9]+,[0-9]+\):(.*)"
      IgnoreExitCode="true" >
</Exec>

All files are compiled without problems, except it takes 5 minutes instead of 10 seconds...

Julius
  • 946
  • 1
  • 10
  • 26

2 Answers2

3

Typescript version 0.8.3 solves this problem! No more erros.

I am now able to compile all the files with one go:

<ItemGroup>
   <AllTypeScriptFiles Include="XXXXX\Scripts\**\*.ts;" Exclude="XXXX\Scripts\**\*.d.ts;" /> 
</ItemGroup>

<WriteLinesToFile
    File="typescriptcompiler.input"
    Lines="@(AllTypeScriptFiles)"
    Overwrite="true"
    Encoding="Unicode"/>    

<Exec Command="&quot;$(MSBuildProgramFiles32)\Microsoft SDKs\TypeScript\$(TypeScriptVersion)\tsc&quot; --target ES5 @typescriptcompiler.input"
      CustomErrorRegularExpression="\.ts\([0-9]+,[0-9]+\):(.*)">
</Exec>
Julius
  • 946
  • 1
  • 10
  • 26
2

The easiest way to do this is to pick your top level file (for example app.ts) and set an output file on the compiler...

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

TypeScript will walk all the dependencies and compile it all into final.js.

Note - I recommended this way because you aren't using a --module flag. I would give a different answer for commonjs or amd programs.

I have just changed my TypeScript workflow to use this based on ideas from Mark Rendle.

Alternatively, you can use the following to compile all .ts files...

  <ItemGroup>
    <TypeScriptCompile Include="$(ProjectDir)\**\*.ts" />
  </ItemGroup>
  <Target Name="BeforeBuild">
    <Exec Command="&quot;$(PROGRAMFILES)\Microsoft SDKs\TypeScript\0.8.1.1\tsc&quot; --target ES5 @(TypeScriptCompile ->'&quot;%(fullpath)&quot;', ' ')" />
  </Target>
Fenton
  • 241,084
  • 71
  • 387
  • 401
  • I will take a look at this, but it was not the answer I was hoping for. In our file list there are basically many "root" files, we have one for each part of our app and each unit test file is basically its own root. – Julius Jan 18 '13 at 14:07
  • I've added an alternative that may help. – Fenton Jan 18 '13 at 14:10
  • Yeah, this is the code I get the "File not found" errors in, or is your code different from mine? – Julius Jan 18 '13 at 14:17
  • It may be some other difference in our projects, but I have updated a project to use this exact code and it runs fine. – Fenton Jan 18 '13 at 14:26
  • Yes it does normally. But it looks there are issues with many files, we have about 300 ts files. I came across [this](http://typescript.codeplex.com/workitem/597), but I was not able to make it work with node.js either. – Julius Jan 18 '13 at 14:37
  • Maybe raise a ticket over at http://typescript.codeplex.com/ and see if it is a known issue. – Fenton Jan 18 '13 at 20:24
  • It works better with the new [version of the compiler 0.8.2](http://www.microsoft.com/en-us/download/details.aspx?id=34790), but I do not get all the errors as I do when compiling the files individually. – Julius Jan 22 '13 at 11:05
  • Not sure what you mean? I get the same errors using web extensions when editing the files in VS2012. – Julius Jan 22 '13 at 13:51