Clean up your code with ease
Tags: javascript, frontend, opensource, webdev
Published: 2024-01-22 14:30:00

Hi, I am the creator of the [Pure Index](https://github.com/space307/pure-index) tool. Its essential purpose is to clean your packages of unused exports with ease. In this article, I share a brief story of its inception.

I work in a monorepo that contains dozens of packages from which we build a website as a team. One of the problems we have encountered is dead code. For example, a _TextCountDown_ component was written a couple of years ago, and the code where it was used was removed at some point. Ideally, the component should have been removed as well, but it wasn't. Yes, it was a mistake, but it happened, and we needed to find a way to prevent such situations in the future.

What tools would have helped at that point? ESLint? Perhaps, but ESLint would not have identified _TextCountDown_ as unused because it was exported from a file. And that's where _ts-prune_ came on the scene. Its first run was both a disappointment and a joy for us. The significant number of unused exports displayed in the console meant that removing them would reduce the code base's size, build time, etc. We removed hundreds of such exports and all the dead code in a few iterations. That's how it seemed to us...

Packages have index files (`index.ts`, for example) that describe what the package exports. _ts-prune_ showed all exports from these files as unused, which was a false positive. Some exports were dead, perhaps whole packages, but not all. The reason was in the import paths. There was no direct connection between `import {smth} from 'my-package'` and `export {smth} from '~/src/index.ts'`. Maybe it was possible to somehow configure _ts-prune_ to handle this, but we found a case that wouldn't help.
There is a _ui-kit_ package whose code is stored in another repository, and the package itself is used in other products and internal systems.That's when I decided to create _Pure Index_. In combination with _ts-prune_, it allowed the automate the process of searching for dead code as follows:

1. search for all unused exports from the index file
2. delete them
3. run _ts-prune_ inside the package to remove the dead code

Finding unnecessary exports involves:

- collect all package exports into exports Set
- traverse all files where package import may occur
- if the import is found, remove it from exports Set
- if the size of the exports Set became equal to 0, then exit with success
- if the exports Set size is not equal to 0, then exit with an error

_Pure Index_ handles this task in ~400 ms even for large repositories, for example, when analyzing _ui-kit_ in two repositories that contain more than 15'000+ .ts and tsx files. Of course, Pure Index's algorithm is a bit more complex. You can read about it in the ["How Does It Work"](https://space307.github.io/pure-index/explanation/how-it-works/) section.

As an added bonus, _Pure Index_ allows you to collect all X package imports within a repository. For example, you want to find out how many functions from _lodash_ you use. Just run `pure-index -u lodash`, and you'll get a complete list.

_Pure Index_ is available on GitHub. Just follow the [link](https://github.com/space307/pure-index) and start using it.

P.S. I will be happy if you give a star to the repository and share your opinion about the tool 🌿