tsc 如何知道要编译的 ts 文件在哪里?

How does tsc know where are the ts files to compile?

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

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

https://github.com/angular/quickstart

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

我想弄清楚使这成为可能的代码在哪里。

来自 package.json,

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

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

通常有一个 tsconfig.json 某处被 TypeScript 使用。

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

{
  "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/**/*"
],

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

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

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