打字稿模块解析

时间:2016-01-13 12:02:30

标签: typescript node-modules tsconfig

是否有一个打字稿模块解析策略,允许我从斜杠开始本地导入并从同一模块中的node_modules导入?

当“moduleResolution”设置为“node”时,编译器找不到我的本地模块,所有其他选项都没有看到我正在尝试导入的node_modules目录中的模块。

我的文件中有以下导入:

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

“angular2 / core”位于node_modules中,“core / logging / Logger”是我的本地模块。

我认为这应该是可能的,如果你看看角度的代码,他们似乎都有,即。在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

更多背景信息:
我有一个由两个子项目组成的项目:
  - 图书馆
  - app
库定义了应用程序随后使用的一些实用程我的构建过程首先构建'library'子项目,然后将它(已编译的js和d.ts文件)复制到'app的node_modules中的'library'子文件夹,然后编译'app'。

在'library'中我有以下类:

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

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

在'app'中:

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有任何非公共字段(它们从两个不同的地方导入,因此对于打字稿,这些是两种不同的类型),这不会编译。这就是为什么我在尝试两个导入我的模块时没有斜线的原因。但也许还有其他解决方案吗?

1 个答案:

答案 0 :(得分:2)

  

我认为这应该是可能的,如果你看看角度的代码,他们似乎都有,即。在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

相关问题