使用spec / test文件夹设置tsconfig

时间:2016-02-18 00:28:03

标签: typescript visual-studio-code tsconfig

假设我将代码放在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文件夹。

如何解决这个问题?

5 个答案:

答案 0 :(得分:29)

我最终定义了多个配置文件并使用extends来简化它们。

说我有两个文件:tsconfig.jsontsconfig.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": [ ... ]
 }

答案 1 :(得分:6)

这在某种程度上取决于您正在使用的测试框架,但我喜欢使用ts-node来编译我的测试文件。使用mocha,您的npm test脚本可能如下所示:

"mocha": "mocha test/ --compilers ts:ts-node/register --recursive"

在tsconfig.json中,请务必删除rootDir选项。

{
"compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "noImplicitAny": false,
    "removeComments": true,
    "sourceMap": true,
    "outDir": "lib"
},
"include": [
    "src/**/*.ts"
],
"exclude": [
    "node_modules",
    "lib",
    "typings/**"
]
}

当您尝试运行设置为rootDir的{​​{1}}或任何应用程序代码的基本文件夹的typescript时,它将禁止在外部的目录中进行任何编译,例如{{ 1}}。使用src,您可以轻松地将所有内容分开,而无需单独的TypeScript配置文件。

答案 2 :(得分:4)

我认为你不应该使用&#39;文件&#39;配置中的选项。相反,您可以排除不需要的文件并将其包含在内:

{ 
    "compilerOptions": { 
        "module": "commonjs", 
        "outDir": "dist"
    },
    "exclude": [
        "node_modules",
        "dist",
        "typings/browser.d.ts",
        "typings/browser/**"
    ]
} 

这样可以保留原始结构。没有混合测试和app js文件的文件夹:

--dist
----spec
-------....
----src
-------....

答案 3 :(得分:2)

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

  • 编译包括源代码和测试文件夹/文件
  • 内部版本仅包含源
  • IDE(VSCode,...)

配置

该解决方案基于2个tsconfig.ts文件,如其他答案所述。

主要./tsconfig.ts (用于编译和IDE):

{
  "compileOptions": {
    "module": "commonjs"
    "outDir": "dist"
  },
  "include": [
    "spec/**/*. spec.ts"
  ],
  "files": {
    "src/index.ts",
  }
}

第二个./tsconfig-build.ts (用于构建):

{
  "extends": "./tsconfig.json",
  "exclude": [
    "spec/**/*. spec.ts"
  ]
}

注意:我们排除以前包含的测试文件

构建

构建命令:tsc -p tsconfig-build.json

或者npm run build(如果在package.json中添加了脚本:

{
  "scripts": {
    "build": "tsc -p tsconfig-build.json",
}

答案 4 :(得分:0)

只需添加一个包含要编译并包含在构建中的源文件的目录。接下来,在 tsconfig.json 中指定排除目录。对于您的用例,没有必要有多个 tsconfig 文件。

{
  "include": [ "src/**/*" ],
  "exclude": [ "./spec" ]
}