2016-02-18 6 views
15

は、私がspecsrcやテストの下で私のコードを入れて言ってやるがいい。スペック/ testフォルダとのTSconfigを設定

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

私がしたいだけtranspile srcdistフォルダに移動します。私はそれらに依存関係を解決できませんでしたので、

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

しかし、このtsconfig.jsonは、テストファイルが含まれていません:index.tsので、このように私のtsconfig.json見て、私のパッケージのエントリポイントです。

一方、テストファイルをtsconfig.jsonに含めると、distフォルダにも移動されます。

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

答えて

13

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

このテストやアプリのjsファイルを混合せずに「DIST」フォルダに元の構造を保持します複数の設定ファイルを使用し、簡略化するためにextendsを使用してください。

は、私は2つのファイルがあるとしましょう: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)のハンドル。

更新:私のプロジェクトが成長するにつれて、より多くの設定ファイルがあることになりました。私は活字体で使用できるようになりました「拡張」機能を使用する:これは、あなたが使っているものは何でもテストフレームワークに多少依存しますが、私は私のテストファイルをコンパイルするts-nodeを使用したい

// 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": [ ... ] 
} 
2

私はあなたの設定で 'ファイル'オプションを使用すべきではないと思います。代わりに、あなたは、不要なファイルを除外し、持っている、それをこのようにすることができます:私は定義してしまった

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

。モカを使用して、あなたのnpm testスクリプトは次のようになります。あなたのtsconfig.jsonで

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

を、rootDirオプションを削除してください。

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

、あなたのアプリケーションコードのためsrcまたは任意のベースフォルダにrootDirセットでtypescriptですを実行しようと、それは、外に座っているディレクトリに、このようなtestsを任意のコンパイルを禁止します。 ts-nodeを使用すると、個別のTypeScript設定ファイルを持たなくても、すべてを分けておくことができます。

+1

複数の設定ファイルに分かれていないと、パッケージに特別な(テスト)ファイルを配布するという欠点があります。また、あなたの2つの 'include 'は冗長です。 'src/**/*。ts'だけが必要です。 – unional

+0

btw、' typings/*** 'のように' *** 'とは何ですか?聞いたことがない。 – unional

+0

'**'にする必要があります。 – barndog

関連する問題