简体   繁体   English

TypeScript导入路径别名

[英]TypeScript Import Path Alias

I am currently working on a TypeScript application which is comprised of multiple Node modules written in TypeScript, that are installed into the node_modules directory. 我目前正在开发一个TypeScript应用程序,它由多个用TypeScript编写的Node模块组成,安装在node_modules目录中。

Before continuing, I would like to note that I am using TypeScript 2.0, I am not adverse to using the 2.1 dev release if need be as well. 在继续之前,我想指出我使用的是TypeScript 2.0,如果需要的话,我也不会对使用2.1 dev版本感到不利。 I just want to get this working. 我只是想让这个工作。

The structure of each Node module is something along the lines of: 每个Node模块的结构都是这样的:

dist/
    es2015/
        index.js
        utils/
            Calculator.js
            Calculator.d.ts
            Date.js
            Date.d.ts
            Flatpack.js
            Flatpack.d.ts
src/
    index.ts
    utils/
        Calculator.ts
        Date.ts
        Flatpack.ts

The dist folder is the generated source, with this path being configured using outDir inside of my tsconfig.json file. dist文件夹是生成的源,使用我的tsconfig.json文件中的outDir配置此路径。 I have also configured the main property in my package.json to be dist/es2015/index.js . 我还将package.jsonmain属性配置为dist/es2015/index.js

Important things to note: 需要注意的重要事项:

  • In my project I am using moduleResolution type of node 在我的项目中,我使用的是moduleResolution类型的node
  • I am using TypeScript 2.0 我正在使用TypeScript 2.0
  • I am not asking how to import a file from within the package. 我不是问如何从包中导入文件。 I am installing the package via Npm and then importing it via packagename/ from within an application that is using this module. 我通过Npm安装软件包,然后通过packagename/从使用此模块的应用程序中导入它。

Now comes my question/issue. 现在是我的问题/问题。 Whilst importing files from this module, I would like to be able to do this: 从这个模块导入文件时,我希望能够这样做:

import {Sin, Cos, Tan} from "common-utils/utils/Calculator";

However, the file cannot resolve to the dist/es2015/utils directory. 但是,该文件无法解析到dist/es2015/utils目录。 Ideally I would like my imports to resolve from this specific dist folder and not from the root, which is what appears to be happening. 理想情况下,我希望我的导入能够从这个特定的dist文件夹解析,而不是从根目录解析,这似乎正在发生。

The above import needs to be written as the following to get it to work: 上面的导入需要写成以下内容才能使其工作:

import {Sin, Cos, Tan} from "common-utils/dist/es2015/utils/Calculator";

However, writing dist/es2015 each time is not ideal and it just makes some of the imports look really long. 然而,每次写dist/es2015并不理想,只是让一些导入看起来很长。 Is there a way I can configure my module to resolve to the dist/es2015 directory? 有没有办法可以配置我的模块解析到dist/es2015目录? I don't want to have to put in overrides inside of my project, ideally each module would specify where files are resolved from. 我不想在我的项目中放入覆盖,理想情况下,每个模块都会指定文件的解析位置。

If you are still unsure what I am asking (and I apologise if this is confusing) in Jspm when you create a plugin/module to be used with Jspm, you can specify inside of the package.json for the module something like the following: 如果你在Jspm中创建一个与Jspm一起使用的插件/模块时仍然不确定我在问什么(我道歉,如果这令人困惑),你可以在package.json为模块指定如下内容:

  "jspm": {
    "registry": "npm",
    "jspmPackage": true,
    "main": "my-module",
    "format": "amd",
    "directories": {
      "dist": "dist/amd"
    },

I am looking for the equivalent of the above in TypeScript (if it exists). 我正在寻找TypeScript中的上述内容(如果它存在)。 A mapping directive, so when the end user runs npm install my-cool-package and then in their app tries to import something, all imports by default resolve to the commonjs directory (the above example for Jspm uses amd, but same premise). 一个映射指令,所以当最终用户运行npm install my-cool-package然后在他们的应用程序中尝试导入一些东西时,默认情况下所有导入都会解析为commonjs目录(上面的Jspm示例使用amd,但前提是相同)。

Is this possible or am I misunderstanding something here? 这可能还是我在这里误解了什么? I know some new path mapping features were added into the latest release, but documentation on using them is almost nonexistent. 我知道在最新版本中添加了一些新的路径映射功能,但使用它们的文档几乎不存在。

So after reading your comment, I realized I misunderstood your question! 所以在看完你的评论后,我意识到我误解了你的问题! If you want to control the paths from an imported package's perspective, just use set the main property of your package.json to a file that properly represents the object graph of your module. 如果要从导入包的角度控制路径,只需将package.jsonmain属性设置为正确表示模块对象图的文件。

{
  "main": "common-utils/dist/es2015/index.js"
}

If you're attempting to control the import paths from a project's perspective, what you're looking for is TypeScript 2's new path mapping feature for module resolution. 如果您试图从项目的角度控制导入路径,那么您正在寻找的是TypeScript 2的模块分辨率的新路径映射功能。 You can enable path mapping by configuring your tsconfig.json as follows: 您可以通过配置tsconfig.json来启用路径映射,如下所示:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "angular2/*": ["../path/to/angular2/*"],
      "local/*": ["../path/to/local/modules/*"]
    }
  }
}

Then, in your TypeScript files, you can import the modules like this: 然后,在TypeScript文件中,您可以导入如下模块:

import { bootstrap } from 'angular2/bootstrap';
import { module } from 'local/module';

For more details about the Path Mapping feature in TypeScript 2 see this Github issue . 有关TypeScript 2中的路径映射功能的更多详细信息,请参阅此Github问题

In your case, I think the following configuration should work: 在您的情况下,我认为以下配置应该工作:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "common-utils/utils/*": ["./node_modules/common-utils/dist/es2015/utils/*"]
    }
  }
}

If you are using paths, you will need to change back absolute paths to relative paths for it to work after compiling typescript into plain javascript using tsc . 如果您正在使用路径,则需要将绝对路径更改为相对路径,以便在使用tsctsc编译为纯JavaScript后将其工作。

Most popular solution for this has been tsconfig-paths so far. 到目前为止,最流行的解决方案是tsconfig-paths

I've tried it, but it did not work for me for my complicated setup. 我试过了,但是对于我复杂的设置它并不适用。 Also, it resolves paths in run-time, meaning overhead in terms of your package size and resolve performance. 此外,它在运行时解析路径,这意味着在包大小和解决性能方面的开销。

So, I wrote a solution myself, tscpaths . 所以,我自己写了一个解决方案, tscpaths

I'd say it's better overall because it replaces paths at compile-time. 我说它总体上更好,因为它在编译时替换了路径。 It means there is no runtime dependency or any performance overhead. 这意味着没有运行时依赖性或任何性能开销。 It's pretty simple to use. 它使用起来非常简单。 You just need to add a line to your build scripts in package.json . 您只需要在package.json为构建脚本添加一行。

The project is pretty young, so there could be some issues if your setup is very complicated. 该项目非常年轻,如果您的设置非常复杂,可能会出现一些问题。 It works flawlessly for my setup, though my setup is fairly complex. 虽然我的设置相当复杂,但它对我的设置完美无缺。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM