In your csproj file, add the following line to the existing PropertyGroup block:
<PropertyGroup> <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked> </PropertyGroup>
Make sure you are including your typescript files as content
<ItemGroup> <Content Include="**\*.ts" Exclude="$(GlobalExclude)" /> </ItemGroup>
tsconfig.json file to your project root and make sure the following setting is set:
Finally, Restart Visual Studio
Nuget creates a generated targets file called
[ProjectName].csproj.nuget.g.targets in the
obj directory of your project. This targets file is importing
Microsoft.NET.Sdk.Web.ProjectSystem.targets which in turn imports
Microsoft.TypeScript.targets file, the following line has a comment that lets us know that if this property is set to true, then the TypeScript compilation task will do nothing:
<!-- Makes the TypeScript compilation task a no-op --> <TypeScriptCompileBlocked Condition="'$(TypeScriptCompileBlocked)' == ''">false</TypeScriptCompileBlocked>
I'm using webpack's ts-loader to compile and bundle my TypeScript files. So I no longer needed the automatic compilation that Visual Studio performed on each build. In Visual Studio 2017, I just commented out the following line from tsconfig.json to stop the automatic compilation:
I had the same problem - Webpack was rebuilding my Typescript files every time Visual Studio rebuilt the project, despite having
in my proj file, and
"compileOnSave": false, "buildOnSave": false
in my tsconfig.json file.
Turns out it was because I had the NPM Task Runner VS extension installed (https://marketplace.visualstudio.com/items?itemName=MadsKristensen.NPMTaskRunner), and in Task Runner Explorer this had a build task bound to the 'Before Build' event. I didn't need this extension so I just uninstalled it.
Note: I didn't need Webpack to rebuild on VS build because I had it watching my files and rebuilding anyway as I made changes, via this extension: https://marketplace.visualstudio.com/items?itemName=MadsKristensen.WebPackTaskRunner
true was not enough for me. What worked was going to the project properties - there is a TypeScript Build tab where you can configure TS compilation, including the Compile On Save option:
It results in the following getting added to the
<TypeScriptTarget>ES5</TypeScriptTarget> <TypeScriptJSXEmit>None</TypeScriptJSXEmit> <TypeScriptModuleKind>ES6</TypeScriptModuleKind> <TypeScriptCompileOnSaveEnabled>False</TypeScriptCompileOnSaveEnabled> <TypeScriptNoImplicitAny>False</TypeScriptNoImplicitAny> <TypeScriptRemoveComments>False</TypeScriptRemoveComments> <TypeScriptOutFile /> <TypeScriptOutDir /> <TypeScriptGeneratesDeclarations>False</TypeScriptGeneratesDeclarations> <TypeScriptNoEmitOnError>True</TypeScriptNoEmitOnError> <TypeScriptSourceMap>True</TypeScriptSourceMap> <TypeScriptMapRoot /> <TypeScriptSourceRoot />
Done the following setting along with TypeScriptCompileBlocked to true.
Set the below condition to false in .csproj file of the application.
Errors complaining about .ts files:
I got build error when I tried to rebuild a VS2017 Web project that includes some typescript files. Error looked like these
Error TS1005 (TS) '?' expected. C:\...\...project1 (tsconfig or jsconfig project) C:\\...\...project1 \node_modules\@storybook\addon-knobs\dist\type-defs.d.ts
No answers that really helped..so far: After spending hours searching up and down the Stack or else, I realized it could be as simple as to compare the errored project file to another one that also has typescript but no compile error.
It is simple..but it worked:
Here are the steps:
Search "TypeScript" (exact case and exact word option) and found two Imports that matched:
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.Default.props" Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.Default.props')" />
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets" Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets')" />
Then I went to the Project that did not have a problem compiling .ts files and look, it's different in the Condition:
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets" Condition="false" />
So I just replaced the Import with the one in 3) and reloaded the project and as expected, compiled successfully.
In the perfect world,Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets')" might just be exactly same as Condition="false" but there were not, even in my machine there is no such folder existing (C:\Program Files (x86)\MSBuild\15.0).
Sometimes, common sense helps more than deep knowledge, and this was that time.
In my case (Visual Studio 2017 15.9.25), I have to add the following property to .csproj
<PropertyGroup> <TypeScriptCompileOnSaveEnabled>false</TypeScriptCompileOnSaveEnabled> </PropertyGroup>
I read this article https://www.typescriptlang.org/docs/handbook/compiler-options-in-msbuild.html and realize that VS will build TS based on the file Microsoft.TypeScript.targets.
And in this file, I see that it also bases on this property to control the Compile-on-Save
TypeScriptCompileOnSaveEnabled. So setting this property to false in .csproj will stop the compile typescript when saving. (Please note that before doing this I already tried setting
"compileOnSave": false in tsconfig.json file but it did not help)
©2020 All rights reserved.