How to prevent visual studio 2017 from build javascript?

Simple Answer

In your csproj file, add the following line to the existing PropertyGroup block:

<PropertyGroup>
     <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
</PropertyGroup>

If adding .ts or .tsx files to your project causes your project file to be modified, you may need to apply the following fix. See the bug report for more details.

 <ItemGroup>
      <None Remove="**/*.ts;**/*.tsx" />
      <Content Remove="**/*.ts;**/*.tsx" />
      <TypeScriptCompile Include="**/*.ts;**/*.tsx" />
 </ItemGroup>

Add a tsconfig.json file to your project root and make sure the following setting is set:

"compileOnSave": false,

Finally, Restart Visual Studio


Details

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.

In the 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 had the same problem - Webpack was rebuilding my Typescript files every time Visual Studio rebuilt the project, despite having

<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>

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


Setting TypeScriptCompileBlocked to 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:

enter image description here

It results in the following getting added to the csproj file:

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

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:

"outDir": "./wwwroot/build/",