编译打字稿时排除文件夹

FCi*_*Cin 12 javascript typescript tsc

我使用 Atom 编写代码。它用于tsconfig.json包含和排除文件夹。为了使用智能感知,我需要node_modules被包含在内,但是当我想编译它时,js我不想node_modules被编译。所以我需要调用tsc上层文件夹config.ts,这会导致编译整个node_modules.

我的文件夹结构如下所示:

node_modules
config.ts
spec
  |--test1.ts
  |--test2.ts
Run Code Online (Sandbox Code Playgroud)

知道如何node_modules在使用tsc命令编译时排除吗?

And*_*uca 21

使用 exclude 属性

{
    "compilerOptions": {
        ...
    }
    "exclude": [
        "node_modules"
    ]
}
Run Code Online (Sandbox Code Playgroud)

使用“include”包含的文件可以使用“exclude”属性过滤。但是,无论“排除”如何,始终包含使用“文件”属性明确包含的文件。“exclude”属性默认为在未指定时排除 node_modules、bower_components、jspm_packages 和目录。

https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

  • 每当我添加 `"exclude":["node_modules"]` 时,我的 `.ts` 文件都会出错,因为 atom typescript 在 `node_modules` 中找不到模块。 (3认同)
  • 它适用于node_modules,但是当您排除测试时,IDE 将停止支持它们的类型并将其中的大多数标记为“红色”。理想的情况是使用命令行排除构建时的测试,而不必被迫创建单独的 tsconfig.prod.json - 因此来自 https://www.typescriptlang.org/docs/handbook/compiler- 的 `--excludeFiles` options.html 可能是最好的解决方案。 (3认同)