使用spec/test文件夹设置tsconfig

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)

我只想转发srcdist文件夹.既然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.jsontsconfig.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)

  • 现在可能值得使用 Typescript 3 [项目参考](https://www.typescriptlang.org/docs/handbook/project-references.html) 功能,如 /sf/ask/3614225051/ 中所述/如何使用-typescript-3-0中的项目引用 (2认同)

use*_*920 21

这是管理源和测试的详细解决方案:

  • 编译包括源和测试文件夹/文件
  • 构建仅包括源
  • IDE(VSCode,...)

配置

该解决方案基于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)


bar*_*dog 7

这在某种程度上取决于您正在使用的测试框架,但我喜欢使用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)

当您尝试使用rootDirset to src或者应用程序代码的基本文件夹运行typescript时,它将禁止在外部的目录中进行任何编译,例如tests.使用ts-node,您可以轻松地将所有内容保持分离,而无需单独的TypeScript配置文件.

  • 不分成多个配置文件的缺点是在程序包中分发了额外的(测试)文件。另外,您的两个“ include”是多余的。您只需要`src / ** / *。ts` (2认同)

归档时间:

查看次数:

20860 次

最近记录:

5 年,10 月 前