说我把我的代码放在下面src
并在下面测试spec
:
+ spec
+ --- classA.spec.ts
+ src
+ --- classA.ts
+ --- classB.ts
+ --- index.ts
+ tsconfig.json
我只想转移src
到该dist
文件夹。既然index.ts
是我包装的入口,我的tsconfig.json
样子是这样的:
{
"compileOptions": {
"module": "commonjs"
"outDir": "dist"
},
"files": {
"src/index.ts",
"typings/main.d.ts"
}
}
但是,这tsconfig.json
不包括测试文件,因此我无法解析其中的依赖项。
另一方面,如果我将测试文件包括在内,tsconfig.json
它们也将被转换为dist
文件夹。
我该如何解决这个问题?
我最终定义了多个配置文件,并使用extends
它们来简化它们。
说我有两个文件:tsconfig.json
和tsconfig.build.json
// tsconfig.json
{
...
"exclude": [...]
}
// tsconfig.build.json
{
...
"files": [ "typings/index.d.ts", "src/index.ts" ]
}
这样,我可以很好地控制构建(使用tsc -p tsconfig.build.json
)和ts language service
(IDE)处理的内容。
更新:现在随着项目的增长,我最终拥有更多的配置文件。我使用TypeScript现在提供的“扩展”功能:
// tsconfig.base.json
{
// your common settings. Mostly "compilerOptions".
// Do not include "files" and "include" here,
// let individual config handles that.
// You can use "exclude" here, but with "include",
// It's pretty much not necessary.
}
// tsconfig.json
{
// This is used by `ts language service` and testing.
// Includes source and test files.
"extends": "./tsconfig.base.json",
"atom": { ... },
"compilerOptions": {
// I set outDir to place all test build in one place,
// and avoid accidentally running `tsc` littering test build to my `src` folder.
"outDir": "out/spec"
}
"include": [ ... ]
}
// tsconfig.commonjs.json or tsconfig.systemjs.json or tsconfig.global.json etc
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
// for some build this does not apply
"declaration": true/false,
"outDir": "dist/<cjs, sys, global, etc>",
"sourceRoot": "..."
},
// Only point to typings and the start of your source, e.g. `src/index.ts`
"files": [ ... ],
"include": [ ... ]
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句