Converts absolute paths in TypeScript files to relative ones.
yarn add ts-absolute-paths-transformer
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();
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
.
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
.