ts-absolute-paths-transformer
v1.0.0-alpha.2
Published
Transforms absolute paths in a TypeScript project to relative ones.
Downloads
105
Readme
TypeScript Absolute Paths Transformer
Converts absolute paths in TypeScript files to relative ones.
Installation
yarn add ts-absolute-paths-transformer
Setup
const srcPath = resolve(__dirname, 'src');
const transformer = new TsAbsolutePathsTransformer({
src: srcPath,
isAbsoluteModule(path: string) {
return path.startsWith('utilities');
},
resolveAbsoluteModule(path: string) {
return resolve(srcPath, path);
},
});
await transformer.transformAndSave();
Why do we need this?
TypeScript supports handy options like baseUrl
and paths
which allow you to import files using absolute paths. Unfortunately however Microsoft currently (intentionally) doesn't convert those absolute paths to relative ones at compile time. This means that when compiling your files using tsc
it leaves you with esnext code which won't work when imported into a different project because the absolute paths will break.
Hence this small utility which gives you an easy way to convert those absolute paths to relative ones yourself before you throw your code into tsc
.
Drag & drop example
Check out the example folder. It shows how to build a project that relies on being able to import modules using absolute paths by using a baseUrl
in its tsconfig
.