简体   繁体   English

如何防止visual studio 2017构建javascript?

[英]How to prevent visual studio 2017 from build javascript?

I upgraded today to VS2017, and I saw that every time I change something in my my web app project - the build build all my javascript again (I'm using webpack for client).我今天升级到 VS2017,每次我在我的 Web 应用程序项目中更改某些内容时,我都会看到这一点 - 构建再次构建我的所有 javascript(我使用 webpack 作为客户端)。 It is cool, but it take a lot of time, so I'll be happy to configure it to stop building the javascript (and I'll build it myself just when it changed).这很酷,但需要很多时间,所以我很乐意配置它以停止构建 javascript(我会在它发生变化时自己构建它)。

Simple Answer简单的答案

In your csproj file, add the following line to the existing PropertyGroup block:在您的 csproj 文件中,将以下行添加到现有的 PropertyGroup 块中:

<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.如果将.ts.tsx文件添加到您的项目导致您的项目文件被修改,您可能需要应用以下修复。 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:tsconfig.json文件添加到您的项目根目录,并确保设置了以下设置:

"compileOnSave": false,

Finally, Restart Visual Studio最后,重启 Visual Studio


Details详情

Nuget creates a generated targets file called [ProjectName].csproj.nuget.g.targets in the obj directory of your project. Nuget 在[ProjectName].csproj.nuget.g.targetsobj目录中创建一个名为[ProjectName].csproj.nuget.g.targets的生成目标文件。 This targets file is importing Microsoft.NET.Sdk.Web.ProjectSystem.targets which in turn imports Microsoft.TypeScript.targets .此目标文件正在导入Microsoft.NET.Sdk.Web.ProjectSystem.targets ,而后者又导入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:Microsoft.TypeScript.targets文件中,下面这行有一条注释,让我们知道如果这个属性设置为 true,那么 TypeScript 编译任务将什么都不做:

<!-- 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.我正在使用 webpack 的 ts-loader 来编译和打包我的 TypeScript 文件。 So I no longer needed the automatic compilation that Visual Studio performed on each build.所以我不再需要 Visual Studio 在每次构建时执行的自动编译。 In Visual Studio 2017, I just commented out the following line from tsconfig.json to stop the automatic compilation:在 Visual Studio 2017 中,我刚刚从 tsconfig.json 中注释掉了以下行以停止自动编译:

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

Setting TypeScriptCompileBlocked to true was not enough for me.TypeScriptCompileBlocked设置为true对我来说还不够。 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:什么工作是去项目属性 - 有一个 TypeScript Build 选项卡,您可以在其中配置 TS 编译,包括 Compile On Save 选项:

在此处输入图片说明

It results in the following getting added to the csproj file:它导致以下内容被添加到csproj文件中:

<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 had the same problem - Webpack was rebuilding my Typescript files every time Visual Studio rebuilt the project, despite having我遇到了同样的问题 - 每次 Visual Studio 重建项目时,Webpack 都会重建我的 Typescript 文件,尽管有

<TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>

in my proj file, and在我的项目文件中,和

"compileOnSave": false,
  "buildOnSave": false

in my tsconfig.json file.在我的 tsconfig.json 文件中。

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.原来这是因为我安装了 NPM Task Runner VS 扩展( https://marketplace.visualstudio.com/items?itemName=MadsKristensen.NPMTaskRunner ),并且在 Task Runner Explorer 中有一个绑定到“Before Build”的构建任务' 事件。 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注意:我不需要 Webpack 在 VS 构建上重建,因为我让它监视我的文件并在我进行更改时重建,通过此扩展名: https ://marketplace.visualstudio.com/items?itemName=MadsKristensen.WebPackTaskRunner

Errors complaining about .ts files: I got build error when I tried to rebuild a VS2017 Web project that includes some typescript files.抱怨 .ts 文件的错误:当我尝试重建包含一些打字稿文件的 VS2017 Web 项目时出现构建错误。 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错误看起来像这些Error TS1005 (TS) '?' expected. C:\\...\\...project1 (tsconfig or jsconfig project) C:\\\\...\\...project1 \\node_modules\\@storybook\\addon-knobs\\dist\\type-defs.d.ts 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:以下是步骤:

  1. Unload the projects and edited Project file卸载项目和编辑的项目文件

  2. Search "TypeScript" (exact case and exact word option) and found two Imports that matched:搜索“TypeScript”(精确大小写和精确单词选项)并找到两个匹配的导入:

    <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')" />

  1. Then I went to the Project that did not have a problem compiling .ts files and look, it's different in the Condition:然后我去了编译 .ts 文件没有问题的项目,看看,它的条件不同:

    <Import Project="$(MSBuildExtensionsPath32)\\Microsoft\\VisualStudio\\v$(VisualStudioVersion)\\TypeScript\\Microsoft.TypeScript.targets" Condition="false" />

  2. So I just replaced the Import with the one in 3) and reloaded the project and as expected, compiled successfully.所以我只是用 3) 中的一个替换了 Import 并重新加载了项目并按预期成功编译。

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).在完美的世界中, Condition="Exists ('$(MSBuildExtensionsPath32)\\Microsoft\\VisualStudio\\v$(VisualStudioVersion)\\TypeScript\\Microsoft.TypeScript.targets')" 可能与Condition="false"完全相同,但有不是,即使在我的机器中也不存在这样的文件夹(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就我而言(Visual Studio 2017 15.9.25),我必须将以下属性添加到 .csproj

<PropertyGroup>
   <TypeScriptCompileOnSaveEnabled>false</TypeScriptCompileOnSaveEnabled>
</PropertyGroup>

More details:更多详情:

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 .我阅读了这篇文章https://www.typescriptlang.org/docs/handbook/compiler-options-in-msbuild.html并意识到 VS 将基于文件Microsoft.TypeScript.targets构建 TS 。

And in this file, I see that it also bases on this property to control the Compile-on-Save TypeScriptCompileOnSaveEnabled .在这个文件中,我看到它也基于这个属性来控制 Compile-on-Save TypeScriptCompileOnSaveEnabled So setting this property to false in .csproj will stop the compile typescript when saving.因此,在 .csproj 中将此属性设置为false将在保存时停止编译打字稿。 (Please note that before doing this I already tried setting "compileOnSave": false in tsconfig.json file but it did not help) (请注意,在执行此操作之前,我已经尝试在 tsconfig.json 文件中设置"compileOnSave": false ,但没有帮助)

Done the following setting along with TypeScriptCompileBlocked to true.完成以下设置以及 TypeScriptCompileBlocked 为 true。

Set the below condition to false in .csproj file of the application.在应用程序的 .csproj 文件中将以下条件设置为 false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM