【发布时间】:2019-07-29 16:27:45
【问题描述】:
注意:如果您想查看完整项目,此问题的公共 git 存储库位于 https://github.com/matthewadams/ts-test。
我正在尝试将项目中的主要源代码编译与测试源代码编译分开。这是源目录结构:
src
main
...typescript source files and possibly child directories with sources
test
unit
...unit test sources
integration
...integration test sources
我的目标是将仅主要源代码编译成lib/main,仅将测试源代码编译成lib/test。
我正在尝试将主编译和测试编译应该通用的所有编译器选项放在tsconfig.json 中,然后使用命令行参数到tsc 来提供特定于每个编译的选项。这是我目前的tsconfig.json:
{
"compilerOptions": {
"strict": true,
"alwaysStrict": true,
"diagnostics": true,
"disableSizeLimit": true,
"esModuleInterop": true,
"extendedDiagnostics": true,
"forceConsistentCasingInFileNames": true,
"inlineSourceMap": true,
"inlineSources": true,
"listEmittedFiles": true,
"listFiles": true,
"module": "commonjs",
"pretty": true,
"target": "es2015"
}
}
来自package.jsonscripts 部分的 sn-p 如下(我拒绝 Gulp、Grunt 等,理由是复杂性,故意牺牲可移植性):
"scripts": {
"transpile-main": "rm -rf lib/main && tsc --outDir lib/main --rootDir src/main -d --declarationDir lib/main",
"transpile-test": "rm -rf lib/test && tsc --outDir lib/test --rootDir src/test --typeRoots lib/main",
...other scripts here...
}
我可以毫无问题地编译主要源代码,它们正确出现在lib/main 中。但是,当我编译测试源时,出现以下错误:
$ npm run transpile-test
> @matthewadams/ts-test@0.1.0-pre.0 transpile-test /Users/matthewadams/dev/me/ts-test
> rm -rf lib/test && tsc --outDir lib/test --rootDir src/test --typeRoots src/main
error TS6059: File '/Users/matthewadams/dev/me/ts-test/src/main/Nameable.ts' is not under 'rootDir' 'src/test'. 'rootDir' is expected to contain all source files.
error TS6059: File '/Users/matthewadams/dev/me/ts-test/src/main/Person.ts' is not under 'rootDir' 'src/test'. 'rootDir' is expected to contain all source files.
error TS6059: File '/Users/matthewadams/dev/me/ts-test/src/main/PersonImpl.ts' is not under 'rootDir' 'src/test'. 'rootDir' is expected to contain all source files.
让我感到困惑的是消息'rootDir' is expected to contain all source files. 我正在尝试根据lib/main 中的内容编译测试源。我不希望所有资源都在一个目录下。
tsconfig.json 选项和tsc cli 选项的正确组合是什么,以实现我的分开主编译和测试编译的目标?
【问题讨论】:
标签: typescript tsc