【问题标题】:How does tsc know where are the ts files to compile?tsc 怎么知道要编译的 ts 文件在哪里?
【发布时间】:2016-12-15 23:36:45
【问题描述】:

我正在尝试从官方文档中学习 angularjs v2。

https://angular.io/docs/ts/latest/tutorial/toh-pt1.html

https://github.com/angular/quickstart

按照上面链接中的教程,我注意到在运行npm start之后,该软件能够检测到ts脚本的变化并立即编译为js。这太棒了!

我试图找出使这成为可能的代码在哪里。

来自package.json

"scripts": {
    "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",

我猜tsc 负责编译。令我困惑的是 tsc 怎么知道要编译的 ts 文件在哪里?在哪里可以配置tsc 的设置?是否有优化编译过程的设置?

【问题讨论】:

    标签: angularjs angular typescript tsc


    【解决方案1】:

    当你不带任何参数调用 tsc 时。它只是假设您要从当前目录向下编译所有打字稿文件。它使用调用命令的目录来开始搜索。 (这没什么特别的,任何可执行文件都可以找到它是从哪里执行的)然后它只是递归地 /**/*.ts 搜索它本身和所有子目录中的任何文件。

    您可以创建一个 tsconfig.json 来指定选项。

    显然,如果您在配置中指定“文件”数组,您将优化您的编译过程,因为您不需要搜索所有可用的文件夹。但是,除非您拥有庞大的目录结构,否则差异将是名义上的。

    【讨论】:

      【解决方案2】:

      TypeScript 使用的地方通常有一个tsconfig.json

      内容具有为此目的所必需的配置:

      {
        "compilerOptions": {
          "target": "es5",
          "module": "commonjs",
          "moduleResolution": "node",
          "sourceMap": true,
          "emitDecoratorMetadata": true,
          "experimentalDecorators": true,
          "lib": ["es2015", "dom"],
          "noImplicitAny": true,
          "suppressImplicitAnyIndexErrors": true,
          "typeRoots": [
            "../../node_modules/@types/"
          ]
        },
        "compileOnSave": true,
        "exclude": [
          "node_modules/*",
          "**/*-aot.ts"
        ]
      }
      

      现在您可以像这样使用files 选项:

      "files": [
          "core.ts",
          "sys.ts",
          "types.ts",
          "scanner.ts"
          ]
      

      或者您可以使用include 来包含路径:

      "include": [
          "src/**/*"
      ],
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多