打字稿模块分辨率 [英] typescript module resolution

查看:61
本文介绍了打字稿模块分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种打字稿模块解析策略可以让我在同一模块中同时使用本地导入以斜杠开头和从 node_modules 导入?

Is there a typescript module resolution strategy that would allow me to have both local imports not starting with a slash and imports from node_modules in the same module?

将moduleResolution"设置为node"时,编译器无法找到我的本地模块,使用所有其他选项时,它看不到我尝试导入的 node_modules 目录中的模块.

With "moduleResolution" set to "node" the compiler cannot find my local modules, with all other options it does not see the modules in node_modules directory I'm trying to import.

我的文件中有以下导入:

I have the following imports in my file:

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

"angular2/core" 位于 node_modules 中,"core/logging/Logger" 是我的本地模块.

"angular2/core" is located in node_modules, "core/logging/Logger" is my local module.

我认为这应该是可能的,如果您查看 angular 的代码,它们似乎两者都有,即.在 http.ts 模块中:https://github.com/angular/angular/blob/master/modules/angular2/src/http/http.ts

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

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

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

More background info:
I have a project consisting of two subprojects:
- library
- app
Library defines some utilities which app then uses. My build process first builds the 'library' subproject, then copies it (compiled js along with d.ts files) to 'library' subfolder in 'app's node_modules, then compiles the 'app'.

在图书馆"中,我有以下课程:

In 'library' I have the following classes:

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

This won't compile as soon as JSONParser has any non public fields (they are imported from two different places so for typescript these are two different types). That is why I'm trying two import my modules without the slash at the start. But maybe there are other solutions to that?

推荐答案

我认为这应该是可能的,如果您查看 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

They have module resolution classic https://github.com/angular/angular/blob/6b73d09ba1d8c227d1b297341de2218aea5d1276/modules/angular2/tsconfig.json#L12

从'rxjs/Observable'导入{Observable};//这可能来自node_modules

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

The have a non trivial build pipeline specific to them that maps this to node_modules. See : https://github.com/angular/angular/blob/851647334040ec95d1ab2f376f6b6bb76ead0d9a/tools/broccoli/broccoli-typescript.ts#L271

这篇关于打字稿模块分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆