typescript-transform-path-rewrite
Version:
Transform generated javascript import/require paths using typescript standard config
94 lines (72 loc) • 3.05 kB
Markdown
# typescript-transform-path-rewrite
[](https://github.com/vickvu/typescript-transform-path-rewrite/actions/workflows/main.yml)
[](https://github.com/vickvu/typescript-transform-path-rewrite/actions/workflows/publish.yml)
[](https://www.npmjs.com/package/typescript-transform-path-rewrite)
[](https://conventionalcommits.org)
[](https://github.com/prettier/prettier)
Transform generated javascript import/require paths using typescript standard config. Support typescript version 5.
## Quick start
### Installation
```bash
npm install --save-dev typescript-transform-path-rewrite
```
### Example tsconfig.json
```jsonc
{
"compilerOptions": {
"baseUrl": "src",
"paths": {
"@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](https://github.com/nonara/ts-patch)'s `tspc` instead of `tsc`
### Run with ts-node
[ts-node](https://github.com/TypeStrong/ts-node) must be used with [ts-patch](https://github.com/nonara/ts-patch)
```jsonc
{
"ts-node": {
"compiler": "ts-patch/compiler",
},
}
```
> Note: `ts-node` has [issue with Node.js 20 and ESM](https://github.com/TypeStrong/ts-node/issues/1997), 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](https://www.typescriptlang.org/tsconfig#module) configured for [ECMAScript Module](https://nodejs.org/api/esm.html) will have full extension (either `.js` or `.mjs`) as required by [Node.js](https://nodejs.org/api/esm.html#mandatory-file-extensions)
## Custom rules
Custom rules can be added using regular expression, for example
```jsonc
{
"compilerOptions": {
"baseUrl": "src",
"paths": {
"@SRC/*": ["*"]
},
"plugins": [
{
"transform": "typescript-transform-path-rewrite",
"alias": [
"@SRC/custom/(.+)$": "./my-custom/$1.js"
]
},
]
},
}
```