问题描述
假设我将我的代码放在 src
下并在 spec
下进行测试:
Say I put my code under src
and tests under spec
:
+ spec
+ --- classA.spec.ts
+ src
+ --- classA.ts
+ --- classB.ts
+ --- index.ts
+ tsconfig.json
我只想将 src
转译到 dist
文件夹.由于 index.ts
是我的包的入口点,我的 tsconfig.json
看起来像这样:
I want to only transpile src
to the dist
folder. Since index.ts
is the entry point of my package, my tsconfig.json
look like this:
{
"compileOptions": {
"module": "commonjs"
"outDir": "dist"
},
"files": {
"src/index.ts",
"typings/main.d.ts"
}
}
但是,这个 tsconfig.json
不包含测试文件,所以我无法解析它们中的依赖项.
However, this tsconfig.json
does not include the test files so I could not resolve dependencies in them.
另一方面,如果我将测试文件包含在 tsconfig.json
中,那么它们也会被转换为 dist
文件夹.
On the other hand, if I include the test files into tsconfig.json
then they are also transpiled to dist
folder.
我该如何解决这个问题?
How do I solve this problem?
推荐答案
我最终定义了多个配置文件并使用 extends
来简化它们.
I ended up defining multiple config files and use extends
to simplify them.
假设我有两个文件:tsconfig.json
和 tsconfig.build.json
Say I have two files: tsconfig.json
and tsconfig.build.json
// tsconfig.json
{
...
"exclude": [...]
}
// tsconfig.build.json
{
...
"files": [ "typings/index.d.ts", "src/index.ts" ]
}
这样,我可以很好地控制要构建的内容(使用 tsc -p tsconfig.build.json
)以及 ts 语言服务
(IDE) 处理的内容.
This way, I can have fine control on what to build (using tsc -p tsconfig.build.json
) and what the ts language service
(IDE) handles.
更新:现在随着我的项目的增长,我最终拥有更多的配置文件.我使用了现在在 TypeScript 中可用的扩展"功能:
UPDATE: now as my projects grow, I ended up having more config files. I use the "extend" feature that is now available in 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": [ ... ]
}
这篇关于使用 spec/test 文件夹设置 tsconfig的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!