104

コードを下に置き、srcテストを下に置くとしspecます。

+ spec
+ --- classA.spec.ts
+ src
+ --- classA.ts
+ --- classB.ts
+ --- index.ts
+ tsconfig.json

srcフォルダにのみトランスパイルしたいdistindex.ts私のパッケージのエントリポイントであるため、私の外観tsconfig.jsonは次のようになります。

{
  "compileOptions": {
    "module": "commonjs"
    "outDir": "dist"
  },
  "files": {
    "src/index.ts",
    "typings/main.d.ts"
  }
}

ただし、これtsconfig.jsonにはテスト ファイルが含まれていないため、依存関係を解決できませんでした。

一方、テストファイルを含めると、tsconfig.jsonそれらもdistフォルダーにトランスパイルされます。

この問題を解決するにはどうすればよいですか?

4

6 に答える 6

80

複数の構成ファイルを定義し、extendsそれらを単純化するために使用することになりました。

次の 2 つのファイルがある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": [ ... ]
 }
于 2016-05-31T20:43:41.810 に答える
15

これは、使用しているテスト フレームワークに多少依存しますが、私は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設定してtypescriptを実行しようとすると、 . を使用すると、個別の TypeScript 構成ファイルを用意しなくても、すべてを簡単に分離できます。srcteststs-node

于 2016-12-11T09:40:08.843 に答える
2

設定で「ファイル」オプションを使用しないでください。代わりに、不要なファイルを除外して、次のようにすることができます。

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

これにより、テストとアプリの js ファイルを混在させることなく、元の構造が「dist」フォルダーに保持されます。

--dist
----spec
-------....
----src
-------....
于 2016-02-18T07:48:05.077 に答える
1

コンパイルしてビルドに含めるソース ファイルのインクルード ディレクトリを追加するだけです。次に、tsconfig.json で除外ディレクトリを指定します。あなたのユースケースでは、複数の tsconfig ファイルを持つ必要はありません。

{
  "include": [ "src/**/*" ],
  "exclude": [ "./spec" ]
}
于 2021-04-02T16:50:25.090 に答える