javascript - Visual Studio 2017 无法更改 project.csproj 的 <TypescriptModuleKind> 以进行 typescript 转译

标签 javascript visual-studio typescript module tsc

这来 self 在 https://stackoverflow.com/a/48436147/1019307 的回答.

我需要将转译器 VS 使用的模块类型更改为 AMD .默认为 commonjs即使当 <TypescriptModuleKind>project.csproj 中更改来自 commonjs成为AMD .

查看问题:

  1. 添加 typescript 插件 - Typescript 工具、Typescript 构建和 Rich Newman 的 Typescript HTML 应用程序模板。
  2. 使用模板创建新项目
  3. 观察 project.csproj包含 <TypescriptModuleKind>commonjs</TypescriptModuleKind>就这样吧
  4. 启动服务器。它启动正常,可以看到应用程序正在运行(开发人员控制台中没有错误消息)
  5. 打开app.js文件并观察它以:

    "use strict";      
    var Greeter = /** @class */ (function () {
        function Greeter(element) {
    
  6. 打开 app.ts并以 export 为前缀让它成为export class Greeter {

  7. 刷新页面并观察错误exports is undefined在开发者控制台中
  8. 打开app.js文件并观察它现在说以下内容:

    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Greeter = /** @class */ (function () {
    
  9. 创建 tsconfig.json通过运行 tsc --init

  10. 观察它包含 "module": "commonjs"
  11. 运行 tsc转译 ts 文件
  12. 观察 app.js文件仍然如上(并且会导致 exports is not defined 错误)
  13. 更改 tsconfig.json文件,使其包含 "module": "amd"
  14. 运行 tsc转译 ts 文件
  15. 打开app.js文件并观察它现在说以下内容:

      define(["require", "exports"], function (require, exports) {
          "use strict";
          Object.defineProperty(exports, "__esModule", { value: true });
          var Greeter = /** @class */ (function () {
    
  16. 关注https://stackoverflow.com/a/48436147/1019307设置使用 requirejs :-)

  17. 刷新并观察它在浏览器中运行,没有显示任何错误
  18. 移动tsconfig.json通过将例如重命名为xxxxtsconfigxx.xxjson来避开
  19. 停止服务器;修改 app.ts归档使其变脏;重启服务器
  20. 观察 exports is not defined错误返回
  21. 如果您愿意,可以将文件重命名为 tsconfig.json并重复最后一步,观察错误仍然存​​在。这确认 VS 不使用本地 tsconfig.json用于配置。
  22. 修改project.csproj包含 <TypescriptModuleKind>amd</TypescriptModuleKind>
  23. 停止服务器;修改 app.ts归档使其变脏;重启服务器
  24. 观察 exports is not defined错误返回

从而证明 VS 不遵守 tsconfig.json文件,似乎忽略了它的 TypescriptModuleKind配置值。

What are the TypeScript project build configuration options?好像说VS是怎么用tsc的。我使用 --module 运行切换并正常工作(例如 tsc --module commonjs [app.ts]tsc --module amd [app.ts] )。与 [app.ts]如果 tsconfig.json 是必需的文件不存在。因此,VS 似乎忽略了 TypescriptModuleKind。以应有的方式设置或不应用它。

那么这里有什么交易。有什么方法可以让它使用 tsconfig.json归档或兑现其TypescriptModuleKind设置?

最佳答案

我找到了答案。

在项目的属性中,有一个 Typescript Build(侧面)选项卡,还有一个标记为 Module System 的下拉菜单,我将其设置为“AMD”。这奏效了!太棒了。

Visual Studio's Project Properties and Typescript build and modules

@Chris Halcrow 在评论中建议,如果您从项目文件中取出 Typescript 配置,则 VS 可能会使用 tsconfig.json 中的配置。我没有尝试过,但如果您需要比 VS 提供的选项更多的选项,那绝对值得一试。


我刚找到 https://developercommunity.visualstudio.com/content/problem/24584/cant-compile-the-ts-files-on-vs2017-node-modules-n.html ,其中包含一些有关 VS 如何处理 typescript 的有用信息。

我还意识到 @ChrisHalcrow 说的是正确的,他说“如果项目文件夹中有 tsconfig.json,那么 VS TS Config 将被忽略”。我没有强制 VS 识别 tsconfig.json 文件。一旦我再次添加它,它就会被看到。我忘记了 VS 需要“添加”东西(源文件、配置文件等)。我习惯于 Eclipe 和 IDEA 看到变化并询问或只是做。

更棒!

Visual Studio's Project Properties and Typescript build and modules disabled

关于javascript - Visual Studio 2017 无法更改 project.csproj 的 <TypescriptModuleKind> 以进行 typescript 转译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48494104/

相关文章:

reactjs - Typescript 和 React 使用空类型数组设置初始状态

javascript - Angular2 - 没有 LoggedInGuard 的提供者

javascript - 为什么此代码没有更新“添加到购物车”按钮的行为?

javascript - 将 JavaScript\Ionic\Angular 1 应用迁移到 Typescript\Ionic 2\Angular 2 应用

visual-studio - 为什么Visual Studio 2008会告诉我.9-.8999999999999995 = 0.00000000000000055511151231257827?

c - for 循环上的多个 pragma 指令(C 和 VS 2013)

javascript - 从 json feed 中删除时间

javascript - HTML 偏移宽度

git - 如何为某些项目禁用 git 而其他项目不在 Visual Studio Code 中禁用 git?

typescript - Angular 2 : how to redirect if API returns an error?