6

スラッシュで始まらないローカル インポートと、同じモジュール内の node_modules からのインポートの両方を可能にする typescript モジュール解決戦略はありますか?

「moduleResolution」を「node」に設定すると、コンパイラはローカル モジュールを見つけることができません。他のすべてのオプションを使用すると、インポートしようとしている node_modules ディレクトリにモジュールが表示されません。

ファイルに次のインポートがあります。

import {Injectable} from "angular2/core";
import {Logger} from "core/logging/Logger";

「angular2/core」は node_modules にあり、「core/logging/Logger」は私のローカル モジュールです。

angularのコードを見ると、両方があるように見えます。http.ts モジュール: https://github.com/angular/angular/blob/master/modules/angular2/src/http/http.ts

import {Injectable} from 'angular2/core'; //this is local
import {Observable} from 'rxjs/Observable'; //this probably comes from node_modules

詳細な背景情報:
私は 2 つのサブプロジェクトで構成されるプロジェクトを持っています:
- ライブラリ
- アプリ
ライブラリは、アプリが使用するいくつかのユーティリティを定義します。私のビルド プロセスは、最初に「ライブラリ」サブプロジェクトをビルドし、それを (d.ts ファイルと一緒にコンパイルされた js) を「アプリの node_modules の「ライブラリ」サブフォルダにコピーしてから、「アプリ」をコンパイルします。

「ライブラリ」には、次のクラスがあります。

//file project/library/jsonparser/JSONParser.ts
class JSONParser {...} 

//file project/library/services/ConfigurationService.ts
import {JSONParser} from "../jsonparser/JSONParser"
class ConfigurationService {
  constructor(parser: JSONParser){ ... }
}

「アプリ」で:

import {JSONParser} from "library/jsonparser/JSONParser" //this comes from node_modules in 'app'
import {ConfigurationService} from "library/services/ConfigurationService" //from node_modules
class AppConfigurationService extends ConfigurationService {
  constructor(parser: JSONParser) {
    super(parser);
  }
}

これは、JSONParser に非パブリック フィールドがあるとすぐにはコンパイルされません (これらは 2 つの異なる場所からインポートされるため、typescript の場合、これらは 2 つの異なる型になります)。そのため、最初にスラッシュを付けずにモジュールを 2 つインポートしようとしています。しかし、おそらくそれに対する他の解決策はありますか?

4

1 に答える 1

2

angularのコードを見ると、両方があるように見えます。http.ts モジュール: https://github.com/angular/angular/blob/master/modules/angular2/src/http/http.ts

些細なことではありません。

モジュールの解像度がありclassic ます https://github.com/angular/angular/blob/6b73d09ba1d8c227d1b297341de2218aea5d1276/modules/angular2/tsconfig.json#L12

'rxjs/Observable' から {Observable} をインポートします。//これはおそらく node_modules から来ています

には、これを にマップする、それらに固有の重要なビルド パイプラインがありますnode_modules。参照: https://github.com/angular/angular/blob/851647334040ec95d1ab2f376f6b6bb76ead0d9a/tools/broccoli/broccoli-typescript.ts#L271

于 2016-01-13T22:34:43.853 に答える