3

Context

Sometimes I would like just build/recompile all TypeScript files in a standard VS 2017 .NET framework C# project (.csproj) without building the dlls, etc. I would be happy even a console command line solution.

What I've done so far:

I can use MSBuild to compile everything: c:\>msbuild.exe my.csproj. I even figured out that the installed nuget package Microsoft.TypeScript.MSBuild.2.7.2 is included in my .csproj and has a CompileTypeScript target defined, but unfortunately the c:\>msbuild.exe my.csproj -t:CompileTypeScript returns with 0 Errors, 0 Warnings and does nothing. (supposedly looks for the ts files in in wrong place and found none) Note: referring a not existing target gives an informative error message, so the target is definitely found.

Question

Regardless the MSBuild idea what I was figured out, I would like to compile all TypeScript files where are in the .csproj project and use the same TypeScript compiler settings what are defined there. How can I accomplish this task?

2 Answers 2

5

The following command worked for me:

msbuild YourProject.csproj -t:CompileTypeScript -p:BuildingProject=true

The "BuildingProject" condition exists on "CompileTypeScript" task in the Microsoft.TypeScript.targets:

<Target Name="CompileTypeScript" Condition="'$(BuildingProject)' != 'false'" DependsOnTargets="$(CompileTypeScriptDependsOn)" Inputs="@(TypeScriptCompile)" Outputs="@(GeneratedJavascript)">

Essentially, I had to trick the target into thinking that that the project was indeed building so that it would pass the condition and execute.

I should note that I'm working with TS 1.6 so it's possible that this doesn't apply to to 2.7.2.

Sign up to request clarification or add additional context in comments.

Comments

3

Create a new MsBuild target that use EXEC task and tell it to execute tsc (TypeScript compiler). tsc will need you to pass tsconfig.json file so it will know where to find and where to put the output files.

So you will have the following:

<Target Name="TypeScriptCompiler">   
    <Exec Command="tsc [path to your tsconfig.json file]"/>  
</Target> 

Note that tsc compiler should be in your PATH environment variables.

2 Comments

Many thanks for the answer. Unfortunately there is no tsconfig.json, instead when I use VS 2017 Project Properties / TypeScript Build tab, all my settings there persisted in the .csproj file. I suppose there is a mechanism, which passes those settings to the MSBuild typescript task.
You can use tsc without giving to it a tsconfig.json file. That file is here to help the compiler to find where the ts files are so it don't search in all directories.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.