Skip to content

About

Transform generated javascript import/require paths using typescript standard config. Support typescript version 5/6.

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

typescript-transform-path-rewrite

Build Status Publish Status

npm Conventional Commits code style: prettier

Transform generated javascript import/require paths using typescript standard config. Support typescript version 5.

Quick start

Installation

npm install --save-dev typescript-transform-path-rewrite

Example tsconfig.json

{
    "compilerOptions": {
        "paths": {
            "@SRC/*": ["./src/*"],
        },
        "plugins": [
            // Transform javascript file
            {
                "transform": "typescript-transform-path-rewrite",
            },
            // Transform .d.ts file
            {
                "transform": "typescript-transform-path-rewrite",
                "afterDeclarations": true,
            },
        ],
    },
}

The result javascript files will have all @SRC/* import transformed to proper paths.

Run with ts-patch

Use ts-patch's tspc instead of tsc

Run with ts-node

ts-node must be used with ts-patch

{
    "ts-node": {
        "compiler": "ts-patch/compiler",
    },
}

Note: ts-node has issue with Node.js 20 and ESM, to run ts-node in this scenario, use this command

node --loader ts-node/esm <YOUR_TS_FILE>

ECMAScript Module

The transformer will make sure javascript files that are compiled with tsconfig.json's module configured for ECMAScript Module will have full extension (either .js or .mjs) as required by Node.js

Custom rules

Custom rules can be added using regular expression, for example

{
   "compilerOptions": {
       "paths": {
           "@SRC/*": ["./src/*"]
       },
       "plugins": [
           {
               "transform": "typescript-transform-path-rewrite",
               "alias": [
                    "@SRC/custom/(.+)$": "./my-custom/$1.js"
               ]
           },
       ]
   },
}

About

Transform generated javascript import/require paths using typescript standard config. Support typescript version 5/6.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages