Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

typescript-transform-paths

LeDDGroup1.1mMIT3.5.5TypeScript support: included

Transforms module resolution paths using TypeScript path mapping and/or custom paths

typescript, transform, transformer, plugin, path, paths, virtual directory, import, require

readme

typescript-transform-paths

npm version Build Status Conventional Commits code style: prettier

Transform compiled source module resolution paths using TypeScript's paths config, and/or custom resolution paths.

Setup Steps

1. Install

<yarn|npm|pnpm> add -D typescript-transform-paths

2. Configure

Add it to plugins in your tsconfig.json

Example Config

{
  "compilerOptions": {
    "baseUrl": "./",
    // Configure your path mapping here
    "paths": {
      "@utils/*": ["utils/*"],
    },
    // Note: To transform paths for both the output .js and .d.ts files, you need both of the below entries
    "plugins": [
      // Transform paths in output .js files
      { "transform": "typescript-transform-paths" },

      // Transform paths in output .d.ts files (Include this line if you output declarations files)
      { "transform": "typescript-transform-paths", "afterDeclarations": true },
    ],
  },
}

Example result

core/index.ts

// The following transforms path to '../utils/sum'
import { sum } from "@utils/sum";

3. Usage

  • Compile with tsc — Use ts-patch

  • Use with ts-node — Add typescript-transform-paths/register to require config.

    tsconfig.json

    {
      "ts-node": {
        "transpileOnly": true,
        "require": [ "typescript-transform-paths/register" ],
      },
      "compilerOptions" { /* ... */ }
    }
  • Use with node — Use the register script: node -r typescript-transform-paths/register src/index.ts

  • Use with NX — Add the typescript-transform-paths/nx-transformer to project config

    project.json

    {
      /* ... */
      "targets": {
        "build": {
          /* ... */
          "options": {
            /* ... */
            "transformers": [
              {
                "name": "typescript-transform-paths/nx-transformer",
                "options": { "afterDeclarations": true },
              },
            ],
          },
        },
      },
    }

Virtual Directories

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 },
      { "transform": "typescript-transform-paths", "useRootDirs": true, "afterDeclarations": true },
    ],
  },
}

Example

- 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'

Custom Control

Exclusion patterns

You can disable transformation for paths based on the resolved file path. The exclude option allows specifying glob patterns to match against resolved file path.

For an example context in which this would be useful, see Issue #83

Example:

{
  "compilerOptions": {
    "paths": {
      "sub-module1/*": ["../../node_modules/sub-module1/*"],
      "sub-module2/*": ["../../node_modules/sub-module2/*"],
    },
    "plugins": [
      {
        "transform": "typescript-transform-paths",
        "exclude": ["**/node_modules/**"],
      },
    ],
  },
}
// This path will not be transformed
import * as sm1 from "sub-module1/index";

@transform-path tag

Use the @transform-path tag to explicitly specify the output path for a single statement.

// @transform-path https://cdnjs.cloudflare.com/ajax/libs/react/17.0.1/umd/react.production.min.js
import react from "react"; // Output path will be the url above

@no-transform-path

Use the @no-transform-path tag to explicitly disable transformation for a single statement.

// @no-transform-path
import "normally-transformed"; // This will remain 'normally-transformed', even though it has a different value in paths config

Version Compatibility

typescript-transform-paths TypeScript NodeJS
^3.5.2 >=3.6.5, >=4.x, >=5.x >=18

Project Guidelines for Contributors

  • Package Manager: yarn (yarn install)
  • Format and lint the code before commit: prettier (yarn format && yarn lint)
  • Commit messages: Conventional Commit Specs
  • Releases: changelogen (yarn release)
GH_TOKEN=$(gh auth token) yarn release

Alternatives

Maintainers


Ron S.

Daniel Perez Alvarez

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

v3.5.5

compare changes

🩹 Fixes

  • When installed typescript version contains 0 it would cause error to be thrown "Expected version to be parsed" (#399)

❤️ Contributors

v3.5.4

compare changes

🩹 Fixes

  • Respect desired path on case-insensitive file systems (#397)

📖 Documentation

  • Add version compatibility matrix in readme (#338)

🏡 Chore

  • deps-dev: Bump @eslint/js from 9.9.1 to 9.15.0 (#310)
  • deps-dev: Bump typescript-eslint from 8.13.0 to 8.15.0 (#308)
  • deps-dev: Bump eslint from 9.10.0 to 9.15.0 (#309)
  • deps-dev: Bump changelogen from 0.5.5 to 0.5.7 (#316)
  • deps-dev: Bump @types/node from 22.8.7 to 22.9.3 (#314)
  • deps-dev: Bump typescript from 5.6.3 to 5.7.2 in /test (#317)
  • deps-dev: Bump typescript-eslint from 8.15.0 to 8.16.0 (#313)
  • deps-dev: Bump eslint from 9.15.0 to 9.16.0 (#319)
  • deps-dev: Bump typescript-eslint from 8.16.0 to 8.17.0 (#320)
  • deps-dev: Update yarn from 4.4.0 to 4.5.3 (f6ccab1)
  • Update yarn.lock (b958778)
  • deps-dev: Bump ts-patch from 3.2.1 to 3.3.0 (#322)
  • deps-dev: Bump typescript-eslint from 8.17.0 to 8.18.0 (#323)
  • deps-dev: Bump typescript from 5.5.4 to 5.7.2 (#315)
  • deps-dev: Bump ts-patch from 3.2.1 to 3.3.0 in /test (#324)
  • Configure Renovate (#330)
  • Update renovate config to auto merge minor updates (b3f3f31)
  • release: V3.5.3 (4c3a6c3)
  • deps-dev: Bump @types/jest from 29.5.12 to 29.5.14 in /test (#346)

🤖 CI

  • Adjust publish workflow (1039e07)
  • Fix changelogen not pushing code (221136a)
  • Update fetch-depth on publish workflow (2fe790e)

❤️ Contributors

v3.5.3

compare changes

📖 Documentation

  • Add version compatibility matrix in readme (#338)

🏡 Chore

  • deps-dev: Bump @eslint/js from 9.9.1 to 9.15.0 (#310)
  • deps-dev: Bump typescript-eslint from 8.13.0 to 8.15.0 (#308)
  • deps-dev: Bump eslint from 9.10.0 to 9.15.0 (#309)
  • deps-dev: Bump changelogen from 0.5.5 to 0.5.7 (#316)
  • deps-dev: Bump @types/node from 22.8.7 to 22.9.3 (#314)
  • deps-dev: Bump typescript from 5.6.3 to 5.7.2 in /test (#317)
  • deps-dev: Bump typescript-eslint from 8.15.0 to 8.16.0 (#313)
  • deps-dev: Bump eslint from 9.15.0 to 9.16.0 (#319)
  • deps-dev: Bump typescript-eslint from 8.16.0 to 8.17.0 (#320)
  • deps-dev: Update yarn from 4.4.0 to 4.5.3 (f6ccab1)
  • Update yarn.lock (b958778)
  • deps-dev: Bump ts-patch from 3.2.1 to 3.3.0 (#322)
  • deps-dev: Bump typescript-eslint from 8.17.0 to 8.18.0 (#323)
  • deps-dev: Bump typescript from 5.5.4 to 5.7.2 (#315)
  • deps-dev: Bump ts-patch from 3.2.1 to 3.3.0 in /test (#324)
  • Configure Renovate (#330)
  • Update renovate config to auto merge minor updates (b3f3f31)

🤖 CI

  • Adjust publish workflow (1039e07)
  • Fix changelogen not pushing code (221136a)

❤️ Contributors

v3.5.2

compare changes

🩹 Fixes

  • Restore typescript 5.6 compatibility (#301)

🏡 Chore

  • deps-dev: Bump eslint and @types/eslint (#273)
  • deps-dev: Bump @types/node from 22.5.2 to 22.8.7 (#303)
  • deps-dev: Bump typescript-eslint from 8.3.0 to 8.13.0 (#302)

❤️ Contributors

v3.5.1

compare changes

🩹 Fixes

  • Downgrade minimatch from 10.0.1 to 9.0.5 (#272)

💅 Refactors

  • Always patch (ed3ed1f)
  • Rename typescript-number to typescript-x (36643ff)
  • Use strictest typescript configuration (a67e739)
  • Enable @typescript-eslint/no-explicit-any warning (1cddaac)
  • Add eslint-plugin-unicorn (44ad2e0)
  • Remove anys (07e5c27)

🏡 Chore

  • Rename nx test project to avoid conflict with package name (3ee03d1)
  • deps-dev: Bump typescript-eslint from 8.1.0 to 8.2.0 (#249)
  • deps-dev: Bump @types/node from 22.2.0 to 22.4.1 (#248)
  • Add danielpza to funding.yml (9aa6941)
  • deps-dev: Bump typescript-eslint from 8.2.0 to 8.3.0 (#254)
  • deps-dev: Bump @types/node from 22.4.1 to 22.5.0 (#253)
  • deps-dev: Update some dependencies (164aeac)
  • Remove eslint-plugin-unicorn (8c6ccf4)

❤️ Contributors

v3.5.0

compare changes

🚀 Enhancements

  • Add package.json exports (#218)

💅 Refactors

  • Move top level register and nx-transformer to src/ (6f0a280)
  • Add prettier-plugin-jsdoc (0b5fd4c)
  • Remove ts-expose-internals imports (7b66926)
  • Update ts-expose-internals -> @types/ts-expose-internals (56d62d2)

🏡 Chore

  • deps-dev: Remove strip-ansi (2fc9901)

❤️ Contributors

v3.4.11

compare changes

💅 Refactors

  • Remove namespaces in favor of modules (#236)
  • Fix eslint warnings (#238)
  • Fix no-require-imports eslint warning (#239)
  • Enable no-unused-vars eslint rule (2e3b9d6)
  • Remove tsconfig.base.json in favor of @tsconfig/node18 (#235)
  • Unvendor typescript types definitions (#234)
  • Remove hardcoded types (#240)

📖 Documentation

  • Remove all contributors badge (#230)
  • Remove articles section (02d7d6c)

🏡 Chore

  • Update eslint config to remove old src/declarations path (0ab8ebc)
  • Update package.json contributors (b79f96d)
  • deps-dev: Bump @types/node from 18.19.43 to 22.2.0 (#241)
  • deps-dev: Bump typescript-eslint from 8.0.1 to 8.1.0 (#243)
  • deps-dev: Bump @eslint/js from 9.8.0 to 9.9.0 (#242)
  • deps-dev: Bump eslint from 9.8.0 to 9.9.0 (#245)

✅ Tests

  • Remove yarn pack workaround (#232)

🎨 Styles

❤️ Contributors

v3.4.10

compare changes

🏡 Chore

  • Update dependencies (#226)
  • Replace standard-version with changelogen (#227)

🤖 CI

  • Generate provenance on npm publish (#229)

❤️ Contributors

3.4.9 (2024-08-07)

3.4.8 (2024-08-07)

3.4.7 (2024-02-20)

Bug Fixes

  • Numerous issues with elision due to new TS features in v5+ (fixes #184) (d4f89af)

3.4.6 (2023-01-06)

Bug Fixes

  • Cover edge case issues with case-insensitive filesystems (968ee7b)

3.4.5 (2023-01-05)

Bug Fixes

  • OSX paths can be rewritten improperly (fixes #167) (0ff48d7)

3.4.4 (2022-10-24)

Bug Fixes

  • NX Transformer entry script not included in NPM package (fixes #166) (4396e0c)

3.4.3 (2022-10-21)

Bug Fixes

3.4.2 (2022-10-21)

Bug Fixes

  • Edge cases exist which causes ts-node to fail with register script (fixes #162) (0dbe06e)

3.4.1 (2022-10-20)

Bug Fixes

  • ImportType node children do not transform (fixes #150) (70871d2)

3.4.0 (2022-10-20)

Features

  • Added NX Transformer (6619164)
  • Updated library to support latest TypeScript & ts-node (cbeb29c)

Bug Fixes

  • Fixed incorrect signature return type for transformer (fixes #156) (a40b378)

3.3.1 (2021-08-16)

Bug Fixes

  • External project references not resolved properly in composite projects (fixes #125) (4a16396)

3.3.0 (2021-08-10)

Features

  • Added typescript-transform-paths/register script (8c36b09)

3.2.1 (2021-08-05)

Bug Fixes

  • Missing type-only modifier for ts-node type import (fixes #130) (0320574)

3.2.0 (2021-08-03)

Features

  • Support transformation via ts-node transpileOnly and compiler API transformNodes (closes #123) (dd942fd)

Bug Fixes

  • Custom JSDoc tags not working for older TS (fixes #126) (d4280c3)

3.1.0 (2021-07-13)

Features

  • Add support for module augmentation (closes #122) (f9d4994)

3.0.2 (2021-07-01)

Features

  • Improved resolution strategy by using EmitHost (87294b4)

Bug Fixes

  • Accommodate TS 4.4 paths pattern caching (fixes #114) (#116) (e9b2a9f)
  • Base Node was elided if could not resolve (fixes: #119) (1b97c80)

3.0.1 (2021-06-28)

3.0.0 (2021-06-16)

Features

2.2.4 (2021-05-20)

Bug Fixes

  • Implicit 'index' was written to output filename for imports (fixes #105) (4803cd2)

2.2.3 (2021-02-02)

Bug Fixes

  • Prevent .json extension being stripped in output (fixes #95) (bcca436)

2.2.2 (2021-01-11)

Bug Fixes

  • Corrected explicit extensions fix from previous patch (a90e550)

2.2.1 (2021-01-10)

Bug Fixes

  • Preserve explicit file extensions (closes #89) (b0627f8)
  • Rely on original node for getting comment tags (closes #90) (fa978c2)

2.2.0 (2021-01-04)

Features

  • Add overwriteNodeModules option (b4a483e)
  • Added @transform-path and @no-transform-path tags for custom statement level transformation (8cab30d)
  • Added exclude option to allow excluding transformation of matching resolved paths (b1fdb54)

Bug Fixes

  • Certain edge cases existed where type elision improperly elided full import / export declarations without named bindings (closes #87) (84a7866)

2.1.0 (2020-11-27)

Features

Bug Fixes

  • Ensure we use the same typescript instance that called the plugin (Fixes #80)

Refactoring

  • Heavily refactored code base for better scale and readability
  • Refactored tests for modularity and multi-TS instance testing

2.0.4 (2020-11-23)

2.0.3 (2020-11-17)

Bug Fixes

  • Updated to more sound workaround for TS type-elision issue (fb33832)

2.0.2 (2020-10-23)

Bug Fixes

  • Leading comments elided from async import (d29c52a), closes #58

2.0.1 (2020-09-17)

Bug Fixes

2.0.0 (2020-08-04)

⚠ BREAKING CHANGES

  • Re-written to rely on TS API (#66)

Bug Fixes

1.1.15 (2020-07-30)

Bug Fixes

1.1.14 (2019-12-27)

Bug Fixes

1.1.13 (2019-11-11)

Bug Fixes

  • another edge case for implicit * path (#43) (d0f4eb7), closes #42

1.1.12 (2019-11-09)

Bug Fixes

1.1.11 (2019-10-09)

Bug Fixes

  • not working with files with multiple extensions (#37) (97454c7)

1.1.10 (2019-08-20)

Bug Fixes

  • do not transform relative paths (dd57089), closes #30
  • resolve only if file exists (e6c51e0)

1.1.9 (2019-08-20)

Bug Fixes

1.1.8 (2019-08-01)

Bug Fixes

1.1.7 (2019-07-30)

Bug Fixes

  • require statements not being transformed (#27) (822b65e)

1.1.6 (2019-07-09)

Bug Fixes

  • not working explicits exports for types on declaration files (0263f06)

1.1.5 (2019-07-09)

Bug Fixes

Tests

1.1.4 (2019-06-08)

Bug Fixes

  • some exports not working (cc2ba49)

1.1.3 (2019-05-24)

Bug Fixes

Tests

  • use release build for tests (6069d24)

1.1.2 (2019-05-21)

  • add some keywords to package.json
  • update license to MIT
  • add all contributor

1.1.1 (2019-05-18)

Bug Fixes

  • check for paths and baseUrl in tsconfig (c3710c4)
  • type only import not deleted from result file (73dd8e2), closes #9

1.1.0 (2019-05-01)

Features

1.0.2 (2019-04-24)

Bug Fixes

  • produce posix path in windows (5059c3d), closes #5

1.0.1 (2019-04-12)

Bug Fixes

  • not working for same or lower directory level (a748d6a), closes #2

1.0.0 (2019-02-02)

Features