注意:如果您想查看完整的项目,此问题的公共 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
Run Code Online (Sandbox Code Playgroud)
我的目标是仅将主要源代码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"
}
}
Run Code Online (Sandbox Code Playgroud)
该节的一个片段package.json scripts如下(我拒绝 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...
}
Run Code Online (Sandbox Code Playgroud)
我可以毫无问题地编译主要源代码,并且它们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.
Run Code Online (Sandbox Code Playgroud)
让我感到困惑的是'rootDir' is expected to contain all source files. 我正在尝试根据lib/main. 我不希望所有源都在一个目录下。
为了实现单独的主编译和测试编译的目标,tsconfig.json选项和cli 选项的正确组合是什么?tsc
rootDir预计是所有源/输入文件的根。TS 编译器的处理过程如下:
collect all input files ("files" / "include" / "exclude" / imports)
--> for each included input file
--> chop off the "rootDir" from the input
--> prepend the "outDir"
Run Code Online (Sandbox Code Playgroud)
文件src/test从 导入src/main,所以目前rootDir只能设置为src或以上。
项目参考可以解决您描述的所有问题:
rootDir将分别设置为src/main或src/testsrc文件夹不显示为lib输出的一部分src/main无法导入src/test(反之亦然)| tsconfig-base.json // other config options go here
| tsconfig.json // solution config containing references
|
+---lib // output folder
| +---main
| | mod.d.ts
| | mod.js
| | tsconfig.tsbuildinfo
| |
| \---test
| mod-test.js
\---src
+---main // referenced sub-project main
| mod.ts
| tsconfig.json
|
\---test // referenced sub-project test
mod-test.ts
tsconfig.json
Run Code Online (Sandbox Code Playgroud)
./tsconfig-base.json:
{
"compilerOptions": {
// just some sample values, set your own as needed
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"strict": true,
"esModuleInterop": true,
// other compiler options
}
}
Run Code Online (Sandbox Code Playgroud)
./tsconfig.json:
{
"files": [],
"references": [
{
"path": "./src/main"
},
{
"path": "./src/test"
}
]
}
Run Code Online (Sandbox Code Playgroud)
./src/main/mod.ts:
export const foo = "foo";
Run Code Online (Sandbox Code Playgroud)
./src/main/tsconfig.json:
{
"extends": "../../tsconfig-base.json",
"compilerOptions": {
"outDir": "../../lib/main",
"composite": true // flag to signal referenced project
}
}
Run Code Online (Sandbox Code Playgroud)
./src/test/mod-test.ts:
import { foo } from "../main/mod";
// use your test framework here
export function testMod() {
if (foo !== "foo") throw new Error("foo expected.");
}
testMod()
Run Code Online (Sandbox Code Playgroud)
./src/test/tsconfig.json:
{
"extends": "../../tsconfig-base.json",
"compilerOptions": {
"outDir": "../../lib/test"
},
"references": [
{
"path": "../main" // test project depends on main
}
]
}
Run Code Online (Sandbox Code Playgroud)您可以使用以下脚本
构建和清理项目:
"scripts": {
"build": "tsc -b -v",
"clean": "tsc -b --clean"
},
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6402 次 |
| 最近记录: |