2016-01-13 69 views
6

是否有一個打字稿模塊分辨率策略,可以讓我同時具有本地導入而不是以斜槓開始並從同一模塊中的node_modules導入?typescript模塊分辨率

當「moduleResolution」設置爲「node」時,編譯器找不到我的本地模塊,其他所有選項都沒有看到node_modules目錄中的模塊我試圖導入。

我在文件中的下列進口:

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

「angular2 /核心」位於node_modules,「核心/日誌/記錄器」是我的本地模塊。

我認爲這應該是可能的,如果你看看角碼的代碼,他們似乎都有,即。在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 

更多背景信息:
我有由兩個子項目的項目:
- 庫
- 應用
庫定義了一些實用工具和應用程式即可使用。我的構建過程首先構建'庫'子項目,然後將其複製(將js與d.ts文件一起編譯)到'app'的node_modules中的'library'子文件夾中,然後編譯'app'。

在「圖書館」,我有以下類別:

//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

我認爲這應該是可能的,如果你看看角碼的代碼,他們似乎都有,即。在http.ts模塊中:https://github.com/angular/angular/blob/master/modules/angular2/src/http/http.ts

不是微不足道的。

他們有模塊分辨率classichttps://github.com/angular/angular/blob/6b73d09ba1d8c227d1b297341de2218aea5d1276/modules/angular2/tsconfig.json#L12

進口{}可觀察到從 'rxjs /可觀測'; //這可能來自node_modules

具有特定於它們的非平凡構建管道,將其映射到node_modules。參見:https://github.com/angular/angular/blob/851647334040ec95d1ab2f376f6b6bb76ead0d9a/tools/broccoli/broccoli-typescript.ts#L271