DEV Community

Federico Sordillo
Federico Sordillo

Posted on • Updated on

Refactoring absolute paths to relative ones

This is hardly a post, more like simple advice to anyone willing to move his/her codebase to relative paths.

I came to despise magic paths as I believe they solve no problem and usually introduce several non-trivial compilation bugs as soon as you move things around a little.

I did this kind of refactoring several times in the past and noticed that often people just go brute force at the problem, rewriting all the absolute paths one by one and wasting hours of their time.

A simple way to critically reduce the time spent on the task is to use VSCode regexp pattern matching to only target files located at a specific depth level. Assuming you have a magic path pointing to your components folder and a project structure like this:



...
├── package.json
└── src
    └── components


Enter fullscreen mode Exit fullscreen mode

You can refactor it by a simple find and replace:
Alt Text

And then you just go recursively:
Alt Text

Hope this will save someone some minutes :)

Top comments (1)

Collapse
 
gekh profile image
German Khokhlov

thanks, helped a lot