Monday, 29 July 2019

Refactor aliased @ imports to relative paths

In modular environments that use Webpack, TypeScript or other tools that transform ES module imports, path aliases are used, a common convention is @ for src.

It's a frequent problem for me to transform a project with aliased absolute paths:

src/foo/bar/index.js

import baz from '@/baz';

to relative paths:

src/foo/bar/index.js

import baz from '../../baz';

For instance, a project that uses aliases needs to be merged with another project that doesn't use aliases, configuring the latter to use aliases isn't an option due to style guide or other causes.

This cannot be solved with simple search and replace, and fixing import paths manually is tedious and prone to errors. I expect original JavaScript/TypeScript codebase to remain intact in other respects, so transforming it with a transpiler may be not an option.

I would like to achieve this kind of refactoring with IDE of my choice (Jetbrains IDEA/Webstorm/Phpstorm) but would accept a solution with any other IDE (VS Code) or plain Node.js.

How can this be solved?



from Refactor aliased @ imports to relative paths

No comments:

Post a Comment