1. tsc-alias
Replace alias paths with relative paths after typescript compilation.
tsc-alias
Package: tsc-alias
Created by: justkey007
Last modified: Wed, 20 Sep 2023 10:25:51 GMT
Version: 1.8.8
License: MIT
Downloads: 1,916,401
Repository: https://github.com/justkey007/tsc-alias

Install

npm install tsc-alias
yarn add tsc-alias

tsc-alias

Replace alias paths with relative paths after typescript compilation. You can add aliases that reference other projects outside your tsconfig.json project by providing a relative path to the baseUrl.

npm version
License
Donate

Comparison to tsconfig-paths

+ Compile time (no runtime dependencies)

Getting Started

First, install tsc-alias as devDependency using npm.

 npm install -g tsc-alias
npm install --save-dev tsc-alias

Add it to your build scripts in package.json

 "scripts": {
  "build": "tsc --project tsconfig.json && tsc-alias -p tsconfig.json",
}

================ OR ===================

"scripts": {
  "build": "tsc && tsc-alias",
  "build:watch": "tsc && (concurrently \"tsc -w\" \"tsc-alias -w\")"
}

Issues

If you have an issue, please create one. But, before:

  • try to check the FAQ.
  • try to check if there exits alike issues.
  • try to run with --debug and check if config is correctly loaded and all sourcefiles are found.

API

Installation

 npm install tsc-alias

Usage

 import { replaceTscAliasPaths } from 'tsc-alias';

replaceTscAliasPaths(options?);

Here are all the available options:

Option Description Default Value
configFile path to tsconfig.json 'tsconfig.json'
watch Observe file changes false
outDir Run in a folder leaving the "outDir" of the tsconfig.json (relative path to tsconfig) tsconfig.compilerOptions.outDir
declarationDir Works the same as outDir but for declarationDir tsconfig.compilerOptions.declarationDir
resolveFullPaths Attempt to replace incomplete import paths (those not ending in .js) with fully resolved paths (for ECMAScript Modules compatibility) false
silent Reduced terminal output. This is a deprecated option and no longer has any effect. true
verbose Additional information is output to the terminal false
debug Debug information is send to the terminal false
replacers Files to import as extra replacers More info []
output The output object tsc-alias will send logs to. new Output(options.verbose)
fileExtensions Overwrite file extensions tsc-alias will use to scan and resolve files. undefined

Configuration via tsconfig.json Example

 {
  "compilerOptions": {
    ...
  },
  "tsc-alias": {
    "verbose": false,
    "resolveFullPaths": true,
    "replacers": {
      "exampleReplacer": {
        "enabled": true,
        "file": "./exampleReplacer.js"
      },
      "otherReplacer": {
        "enabled": true,
        "file": "./otherReplacer.js"
      }
    },
    "fileExtensions": {
      "inputGlob": "{js,jsx,mjs}",
      "outputCheck": ["js", "json", "jsx", "mjs"]
    }
  }
}

Single file replacer

We can use tsc-alias in a single file, with a function that returns the modified contents.

We prepare the replacer with prepareSingleFileReplaceTscAliasPaths(), passing the same options that we would pass to replaceTscAliasPaths(). That will return a promise of a function that receives the file contents and path, and returns the transformed contents, synchronously.

 import { prepareSingleFileReplaceTscAliasPaths } from 'tsc-alias';

const runFile: SingleFileReplacer = await prepareSingleFileReplaceTscAliasPaths(options?);

function treatFile(filePath: string) {
  const fileContents = fs.readFileSync(filePath, 'utf8');
  const newContents = runFile({fileContents, filePath});
  // do stuff with newContents
}

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code