tsc-alias打字稿编译后将绝对路径替换为相对路径

tsc-alias 的路径替换功能,蛮适合你用绝对路径写项目但又想在编译后输出干净代码的场景。它不依赖运行时,编译完直接,挺方便。

打字稿编译后的路径别名问题,一直让人挺头疼,尤其多项目引用的时候。tsc-alias专门干这个活儿,配置完tsconfig.json里的baseUrlpaths,再在构建脚本里加一行命令就行。

安装也不复杂,全局或 dev 都行:

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

package.json里加上:

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

就这样,构建的时候它会自动帮你把@或者其他自定义别名转成相对路径。响应也快,代码也清爽。

如果你还在用webpack或者gulp别名路径,也可以看看类似工具:gulp-ts-aliastspath,还有适配webpacktsconfig-paths-webpack-plugin也不错。

用的时候注意一点:别名路径和tsconfig.json的位置一定要对得上,不然替换不到。

zip
tsc-alias-master.zip 预估大小:39个文件
folder
tsc-alias-master 文件夹
file
.gitignore 1KB
folder
projects 文件夹
folder
project4 文件夹
file
package.json 141B
folder
src 文件夹
file
index.ts 261B
folder
lib 文件夹
folder
commons 文件夹
file
index.ts 30B
folder
custom_modules 文件夹
file
calculator.ts 62B
file
index.ts 46B
file
tsconfig.json 393B
folder
project1 文件夹
file
package.json 141B
folder
src 文件夹
file
index.ts 261B
folder
lib 文件夹
folder
commons 文件夹
file
index.ts 30B
folder
custom_modules 文件夹
file
calculator.ts 62B
file
index.ts 46B
file
tsconfig.json 443B
folder
node_modules 文件夹
folder
@jkm 文件夹
file
tsconfig.json 50B
folder
project5 文件夹
file
package.json 141B
folder
src 文件夹
file
index.ts 332B
folder
lib 文件夹
folder
commons 文件夹
file
index.ts 30B
folder
custom_modules 文件夹
file
calculator.ts 62B
file
index.ts 46B
folder
scope 文件夹
file
index.ts 27B
file
tsconfig.json 423B
folder
project3 文件夹
file
package.json 128B
folder
src 文件夹
file
index.ts 68B
folder
controllers 文件夹
file
index.ts 27B
file
tsconfig.json 146B
folder
project2 文件夹
file
index.ts 30B
file
package.json 1KB
folder
src 文件夹
folder
bin 文件夹
file
index.ts 589B
file
index.ts 7KB
file
helpers.ts 3KB
file
jest.config.js 222B
file
LICENSE 1KB
file
tsconfig.json 625B
folder
.github 文件夹
folder
workflows 文件夹
file
ci.yml 352B
file
README.md 2KB
folder
tests 文件夹
file
test.spec.ts 400B
file
pnpm-lock.yaml 126KB
file
.editorconfig 245B
file
.prettierrc 63B
zip 文件大小:64.92KB