【问题标题】:Why does Visual studio 2019 adds <TypeScriptCompile Remove /> in my project?为什么 Visual Studio 2019 在我的项目中添加 <TypeScriptCompile Remove />?
【发布时间】:2020-02-19 09:53:09
【问题描述】:

我有一个在 .net 4.7.2 上运行的 asp.net core 2.2 项目,其中包括 Microsoft.TypeScript.MSBuild nuget 包。

每次我向项目中添加新的打字稿(.ts 或 .tsx)文件时,Visual Studio 都会自动添加:

<ItemGroup><TypeScriptCompile Remove="myFile.ts" /></ItemGroup>

进入我的项目 .csproj 文件。

这是我的 tsconfig 文件:

{
  "compilerOptions": {
    // Target latest version of ECMAScript.
    "target": "esnext",

    // Search under node_modules for non-relative imports.
    "moduleResolution": "node",

    // Process & infer types from .js files.
    "allowJs": false,
    "jsx": "react",

    // Don't emit; allow Babel to transform files.
    "noEmit": true,

    // Enable strictest settings like strictNullChecks & noImplicitAny.
    "strict": true,

    // Disallow features that require cross-file information for emit.
    "isolatedModules": true,

    // Import non-ES modules as default imports.
    "esModuleInterop": true,

    "skipLibCheck": true,

    "baseUrl": ".",
    "paths": {
        /* my paths */
    }
  },
  "include": [
        /* my typescript folders */
  ],
  "exclude": [ "node_modules" ]
}

请注意,我只是使用包对我的文件进行类型检查,实际编译是使用 Babel 完成的。

您能帮我理解为什么 Visual Studio 在我的项目中添加这些 TypeScriptCompile Remove 吗?我做错了吗?

【问题讨论】:

  • 你的项目结构是什么?我假设您将 ts 文件放在项目根目录或 tsconfig 文件夹之外的某个位置,并且您没有使用 spa 模板? SPA 模板有一些类似于&lt;SpaRoot&gt;ClientApp\&lt;/SpaRoot&gt;&lt;Content Remove="$(SpaRoot)**" /&gt; 的内容,然后使用目标来编译它/运行 angular-cli 或 npm 脚本或 webpack 来编译它或启动一个 npm 进程来执行热模块所需的东西替换(调试时)或发布时构建静态文件
  • 您好@Tseng,我的 .ts 文件位于 tsconfig 文件的“include”文件夹中。关于 SPA 模板,我认为这是不同的,它们使用 .netapp 和 spa 服务,而我使用 .net472 和 TypeScript.MSBuild 来构建打字稿。
  • 对于 Visual Studio 和 .NET Core,无论是否添加文件,都不会真正评估 tsconfig,这仅适用于 TypeScript 编译器。默认项目模板从 SPA 文件夹中排除内容,这就是为什么在那里创建的文件不会添加到 csproj 的原因,这就是我询问您的项目结构的原因
  • 我在一个 SPA 模板上做了一个测试:只要我添加了 Typescript.MSBuild nuget 包,Visual Studio 就开始添加删除,即使在 SpaRoot 中:

标签: typescript visual-studio asp.net-core


【解决方案1】:

添加了“删除”条目,这样您就不会在项目文件夹结构中看到嵌套的 filename.js 和 filename.js.map 文件。该指令明确告诉 Visual Studio 不要编译此文件,而是允许您使用其他一些编译器(您通过 NPM 下载的 TSC 甚至可能是 Angular 编译器)。基本上它告诉 VisualStudio 让开。

【讨论】:

  • 即使没有该配置,我的 VS2019 也从未生成任何 .js 或 .map 文件。我正在使用捆绑器(webpack)。从您的回答来看,由于当我将该项目的框架切换到 netcore 时这种行为停止了,所以它可能是一种遗留行为。感谢您的宝贵时间!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 2020-04-19
  • 1970-01-01
  • 2022-01-09
  • 2020-07-01
  • 2020-06-21
相关资源
最近更新 更多