Compiling many typescript files at once resulted in File not found error

♀尐吖头ヾ 提交于 2020-01-02 07:26:09

问题


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...


回答1:


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>



回答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>


来源:https://stackoverflow.com/questions/14397972/compiling-many-typescript-files-at-once-resulted-in-file-not-found-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!