tsc-alias-sync
v1.8.6
Published
Replace alias paths with relative paths after typescript compilation.
Downloads
10
Maintainers
Readme
tsc-alias-sync
Note: This package is a modification to the incredible tsc-alias package by @justkey007, meant to be used in synchronous-only environments (such as an ESLint configuration file).
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.
Comparison to tsconfig-paths
+ Compile time (no runtime dependencies)
Getting Started
First, install tsc-alias-sync as devDependency using npm.
npm install -g tsc-alias-sync
npm install --save-dev tsc-alias-sync
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:
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
}