uni*_*nal 43 typescript tsconfig visual-studio-code
假设我把我的代码放在下面src
并测试spec
:
+ spec
+ --- classA.spec.ts
+ src
+ --- classA.ts
+ --- classB.ts
+ --- index.ts
+ tsconfig.json
Run Code Online (Sandbox Code Playgroud)
我只想转发src
到dist
文件夹.既然index.ts
是我的包的入口点,我的tsconfig.json
样子如下:
{
"compileOptions": {
"module": "commonjs"
"outDir": "dist"
},
"files": {
"src/index.ts",
"typings/main.d.ts"
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这tsconfig.json
不包括测试文件,因此我无法解析其中的依赖项.
另一方面,如果我将测试文件包含在内,tsconfig.json
那么它们也会被转换为dist
文件夹.
我该如何解决这个问题?
uni*_*nal 36
我最终定义了多个配置文件并用于extends
简化它们.
说我有两个文件:tsconfig.json
和tsconfig.build.json
// tsconfig.json
{
...
"exclude": [...]
}
// tsconfig.build.json
{
...
"files": [ "typings/index.d.ts", "src/index.ts" ]
}
Run Code Online (Sandbox Code Playgroud)
这样,我可以很好地控制构建(使用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": [ ... ]
}
Run Code Online (Sandbox Code Playgroud)
use*_*920 21
这是管理源和测试的详细解决方案:
该解决方案基于tsconfig.json
其他答案中提到的2 个文件。
主要./tsconfig.json
(用于编译和IDE):
{
"compileOptions": {
"module": "commonjs"
"outDir": "dist"
},
"include": [
"spec/**/*. spec.ts"
],
"files": {
"src/index.ts",
}
}
Run Code Online (Sandbox Code Playgroud)
第二个./tsconfig-build.json
(用于构建):
{
"extends": "./tsconfig.json",
"exclude": [
"spec/**/*. spec.ts"
]
}
Run Code Online (Sandbox Code Playgroud)
注意:我们排除之前包含的测试文件
构建命令: tsc -p tsconfig-build.json
或者npm run build
如果脚本被添加到package.json
:
{
"compileOptions": {
"module": "commonjs"
"outDir": "dist"
},
"include": [
"spec/**/*. spec.ts"
],
"files": {
"src/index.ts",
}
}
Run Code Online (Sandbox Code Playgroud)
这在某种程度上取决于您正在使用的测试框架,但我喜欢使用ts-node来编译我的测试文件.使用mocha,您的npm test
脚本可能如下所示:
"mocha": "mocha test/ --compilers ts:ts-node/register --recursive"
Run Code Online (Sandbox Code Playgroud)
在tsconfig.json中,请确保删除该rootDir
选项.
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": false,
"removeComments": true,
"sourceMap": true,
"outDir": "lib"
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules",
"lib",
"typings/**"
]
}
Run Code Online (Sandbox Code Playgroud)
当您尝试使用rootDir
set to src
或者应用程序代码的基本文件夹运行typescript时,它将禁止在外部的目录中进行任何编译,例如tests
.使用ts-node
,您可以轻松地将所有内容保持分离,而无需单独的TypeScript配置文件.
归档时间: |
|
查看次数: |
20860 次 |
最近记录: |