typescript-transform-paths
Transforms absolute imports to relative
Last updated 4 years ago by nonara .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install typescript-transform-paths 
SYNC missed versions from official npm registry.

typescript-transform-paths

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

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

Articles

Project Guidelines for Contributors

  • Package Manager: yarn (yarn install)
  • Commit messages: Conventional Commit Specs
  • Format before commit: prettier (yarn run format)
  • Releases: standard-version (yarn run release)

Alternatives

Maintainers


Ron S.

Daniel Perez Alvarez

Current Tags

  • 3.4.0-alpha                                ...           alpha (3 years ago)
  • 2.1.0-beta                                ...           beta (4 years ago)
  • 2.1.0-beta1                                ...           beta1 (4 years ago)
  • 2.1.0-beta2                                ...           beta2 (4 years ago)
  • 3.5.2                                ...           latest (2 months ago)
  • 3.4.10-1723087060.2b01c26                                ...           testing (5 months ago)

60 Versions

  • 3.5.2                                ...           2 months ago
  • 3.5.1                                ...           3 months ago
  • 3.5.0                                ...           4 months ago
  • 3.4.11                                ...           4 months ago
  • 3.4.10                                ...           5 months ago
  • 3.4.10-1723087060.2b01c26                                ...           5 months ago
  • 3.4.10-1723086173.5a2b0c9                                ...           5 months ago
  • 3.4.9                                ...           5 months ago
  • 3.4.9-0                                ...           5 months ago
  • 3.4.8                                ...           5 months ago
  • 3.4.7                                ...           10 months ago
  • 3.4.6                                ...           2 years ago
  • 3.4.5                                ...           2 years ago
  • 3.4.4                                ...           2 years ago
  • 3.4.3                                ...           2 years ago
  • 3.4.2                                ...           2 years ago
  • 3.4.1                                ...           2 years ago
  • 3.4.0                                ...           2 years ago
  • 3.4.0-alpha                                ...           3 years ago
  • 3.3.1                                ...           3 years ago
  • 3.3.0                                ...           3 years ago
  • 3.2.1                                ...           3 years ago
  • 3.2.0                                ...           3 years ago
  • 3.1.0                                ...           3 years ago
  • 3.0.2                                ...           3 years ago
  • 3.0.1                                ...           3 years ago
  • 3.0.0                                ...           4 years ago
  • 2.2.4                                ...           4 years ago
  • 2.2.3                                ...           4 years ago
  • 2.2.2                                ...           4 years ago
  • 2.2.1                                ...           4 years ago
  • 2.2.0                                ...           4 years ago
  • 2.1.0                                ...           4 years ago
  • 2.1.0-beta2                                ...           4 years ago
  • 2.1.0-beta1 [deprecated]           ...           4 years ago
  • 2.1.0-beta                                ...           4 years ago
  • 2.0.4                                ...           4 years ago
  • 2.0.3                                ...           4 years ago
  • 2.0.2                                ...           4 years ago
  • 2.0.1                                ...           4 years ago
  • 2.0.0                                ...           4 years ago
  • 1.1.15                                ...           4 years ago
  • 1.1.14                                ...           5 years ago
  • 1.1.13                                ...           5 years ago
  • 1.1.12                                ...           5 years ago
  • 1.1.11                                ...           5 years ago
  • 1.1.10                                ...           5 years ago
  • 1.1.9                                ...           5 years ago
  • 1.1.8                                ...           5 years ago
  • 1.1.7                                ...           5 years ago
  • 1.1.6                                ...           5 years ago
  • 1.1.5                                ...           5 years ago
  • 1.1.4                                ...           6 years ago
  • 1.1.3                                ...           6 years ago
  • 1.1.2                                ...           6 years ago
  • 1.1.1                                ...           6 years ago
  • 1.1.0                                ...           6 years ago
  • 1.0.2                                ...           6 years ago
  • 1.0.1                                ...           6 years ago
  • 1.0.0                                ...           6 years ago
Maintainers (2)
Downloads
Total 0
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (11)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE