我有以下目录结构:
.
??? tsconfig.json ("module": "CommonJS")
??? foo/
??? node-file.ts
??? bar/
??? browser-file.ts
??? tsconfig.json ("module": "esnext")
Run Code Online (Sandbox Code Playgroud)
根tsconfig.json已module设置为CommonJS因为我希望我的大部分文件为 Node.js 编译。不过里面bar,我要的文件编译成JavaScript模块,所以我设置module到esnext。
现在,当我tsc从根运行时,我希望node-file.ts编译为CommonJS模块并browser-file.ts编译为 JavaScript 模块。但这不是我得到的。似乎tsc完全无视foo/bar/tsconfig.json,只是捡了根tsconfig.json。
(我也在tsc --watch开发时使用,所以我试图避免必须运行两个不同的tsc进程来编译两个不同的目标。对我来说,运行tsc带有嵌套tsconfig.json文件的单个程序应该会给我想要的结果。)
有谁知道我做错了什么?
TypeScript 只使用一个tsconfig.json,并且不会自动tsconfig.json为那里的文件使用子目录。但是,您可以为此使用项目引用。
创建这样的目录结构:
.
??? tsconfig.json
??? tsconfig.settings.json (optional)
??? foo/
??? node-file.ts
??? tsconfig.json ("module": "commonjs")
??? bar/
??? browser-file.ts
??? tsconfig.json ("module": "esnext")
Run Code Online (Sandbox Code Playgroud)
tsconfig.json
{
"files": [],
"references": [
{"path": "./foo"},
{"path": "./foo/bar"}
]
}
Run Code Online (Sandbox Code Playgroud)
这是根tsconfig.json。当您tsc --build在根目录中运行(见下文)时,TypeScript 将构建引用的项目./foo/tsconfig.json和./foo/bar/tsconfig.json.
这"files": []是为了阻止意外的tscs 而不--build尝试编译根目录中的所有内容,这会出错,但会.js在可能不正确的位置创建多个文件。
tsconfig.settings.json (可选的)
{
"compilerOptions": {
"strict": true,
"noImplicitReturns": true
}
}
Run Code Online (Sandbox Code Playgroud)
你可以把配置通用foo和foo/bar和扩展这个配置extends,以减少重复。请注意,此处的所有相对路径都将相对于tsconfig.settings.json扩展时进行解析,因此类似的内容"outDir": "dist"可能无法按预期工作。
foo/tsconfig.json
{
"extends": "../tsconfig.settings.json",
"exclude": ["bar/**/*.ts"],
"compilerOptions": {
"module": "commonjs"
}
}
Run Code Online (Sandbox Code Playgroud)
这是 CommonJS 文件的配置。它还扩展了通用配置并排除了foo/bar.
foo/bar/tsconfig.json
{
"extends": "../../tsconfig.settings.json",
"compilerOptions": {
"module": "esnext"
}
}
Run Code Online (Sandbox Code Playgroud)
这与foo的配置非常相似。
要编译foo并foo/bar在同一时间,从根目录使用的构建模式:
tsc --build # or tsc -b
# Watch mode:
tsc --build --watch # or tsc -b -w
Run Code Online (Sandbox Code Playgroud)
从手册:
期待已久的功能是针对 TypeScript 项目的智能增量构建。在 3.0 中,您可以将
--build标志与tsc. 这实际上是一个新的入口点,tsc它的行为更像是一个构建协调器,而不是一个简单的编译器。运行
tsc --build(tsc -b简称)将执行以下操作:
- 查找所有引用的项目
- 检测它们是否是最新的
- 以正确的顺序构建过时的项目
您可以提供
tsc -b多个配置文件路径(例如tsc -b src test)。就像tsc -p,如果它被命名为 ,则指定配置文件名本身是不必要的tsconfig.json。
您还可以编译单个项目:
tsc -b foo # or cd foo && tsc
tsc -b foo/bar # or cd foo/bar && tsc
Run Code Online (Sandbox Code Playgroud)
请注意,这是一些仅限构建的标志,您不能使用命令行参数覆盖编译器选项。
| 归档时间: |
|
| 查看次数: |
1135 次 |
| 最近记录: |