๐Ÿš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more โ†’
Socket
Book a DemoInstallSign in
Socket

typescript-transform-paths

Package Overview
Dependencies
Maintainers
2
Versions
66
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typescript-transform-paths

Transforms absolute imports to relative

2.0.2
Source
npm
Version published
Weekly downloads
263K
-9.6%
Maintainers
2
Weekly downloads
ย 
Created
Source

typescript-transform-paths

npm version Build Status Appveyor Build status Conventional Commits code style: prettier All Contributors

Transforms absolute imports to relative from paths in your tsconfig.json

Install

npm:

npm i -D typescript-transform-paths

yarn:

yarn add -D typescript-transform-paths

Usage with ttypescript or ts-patch

Add it to plugins in your tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@utils/*": ["utils/*"]
    },
    "plugins": [{ "transform": "typescript-transform-paths" }]
  }
}

Transforming declaration paths

If you want to generate declaration (.d.ts) files with transformed paths you have to modify your tsconfig.json file:

  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@utils/*": ["utils/*"]
    },
    "declaration": true,
    "plugins": [
      { "transform": "typescript-transform-paths" },
      { "transform": "typescript-transform-paths", "afterDeclarations": true }
    ]
  }

See issue4 for more information.

Virtual Directory Support

TS allows defining virtual directories via the rootDirs compiler option. To enable virtual directory mapping, use the useRootDirs plugin option.

{
  "compilerOptions": {
    "rootDirs": [ "src", "generated" ],
    "baseUrl": ".",
    "paths": {
      "#root/*": [ "./src/*", "./generated/*" ]
    },
    "plugins": [
      { "transform": "typescript-transform-paths", useRootDirs: true },
    ]
  }
}

Example output

- src/
    - subdir/
      - sub-file.ts
    - file1.ts
- generated/
    - file2.ts

src/file1.ts

import '#root/file2.ts' // resolves to './file2'

src/subdir/sub-file.ts

import '#root/file2.ts' // resolves to '../file2'
import '#root/file1.ts' // resolves to '../file1'

Example Config

// tsconfig.json
{
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
      "@utils/*": ["utils/*"]
    }
  }
}
// core/index.ts
import { sum } from "@utils/sum";

sum(2, 3);

Gets compiled to:

// core/index.js
var sum_1 = require("../utils/sum");
sum_1.sum(2, 3);

Articles

Contributting

  • make sure to format code with prettier:
npm install --global prettier
prettier --write src/index.ts

Contributors

Thanks goes to these wonderful people (emoji key):


Daniel Perez Alvarez

๐Ÿ’ป ๐Ÿšง โš ๏ธ

ะœะธั…ะฐะนะปะพะฒ ะะฝั‚ะพะฝ

๐Ÿ’ป ๐Ÿ› โš ๏ธ

Joshua Avalon

๐Ÿ› ๐Ÿ“ฆ

Robert Laverty

๐Ÿ› โš ๏ธ

Ole Ersoy

๐Ÿ› ๐Ÿ“

sbmw

๐Ÿ›

richardspence

๐Ÿ›

Vitaly Pinchuk

๐Ÿ›

laij84

๐Ÿ›

dko-slapdash

๐Ÿ›

hedwiggggg

๐Ÿ› โš ๏ธ ๐Ÿ’ป

kuskoman

๐Ÿ“–

alex weidner

๐Ÿ›

Ron S.

๐Ÿ› โš ๏ธ ๐Ÿ’ป

Vladimir Yakovlev

๐Ÿ›

vwpo

๐Ÿ›

This project follows the all-contributors specification. Contributions of any kind welcome!

Keywords

typescript

FAQs

Package last updated on 23 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts