打字稿模块分辨率

typescript module resolution

是否有 typescript 模块解析策略允许我在同一个模块中既有本地导入 而不是 以斜线开头,又有来自 node_modules 的导入?

将 "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

更多背景信息:
我有一个由两个子项目组成的项目:
- 图书馆
- 应用
库定义了一些应用程序随后使用的实用程序。我的构建过程首先构建 'library' 子项目,然后将其(编译的 js 和 d.ts 文件)复制到 'app' 中的 'library' 子文件夹 node_modules,然后编译'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 有任何非 public 字段(它们是从两个不同的地方导入的,所以对于打字稿来说,它们是两种不同的类型),这将不会编译。这就是为什么我尝试两次导入我的模块而没有开始的斜线。但也许还有其他解决方案?

I think that should be possible, if you look at angular's code they seem to have both, ie. in the http.ts module: 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

import {Observable} from 'rxjs/Observable'; //this probably comes from node_modules

他们有一个特定于他们的重要构建管道,将其映射到 node_modules。参见:https://github.com/angular/angular/blob/851647334040ec95d1ab2f376f6b6bb76ead0d9a/tools/broccoli/broccoli-typescript.ts#L271