Socket
Socket
Sign inDemoInstall

pure-index

Package Overview
Dependencies
21
Maintainers
1
Versions
75
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.51 to 0.0.52

dist/fileTraversal/getFiles/index.d.ts

4

dist/bin/collectUsages.d.ts

@@ -1,6 +0,6 @@

import { getConfig } from '../getConfig/index.js';
import { type Config } from '../getConfig/index.js';
type Params = {
config: Awaited<ReturnType<typeof getConfig>>;
config: Config;
};
declare const collectUsages: ({ config }: Params) => Promise<never>;
export { collectUsages };
import { collectUsages as _collectUsages } from '../collectUsages.js';
import { getConfig } from '../getConfig/index.js';
import {} from '../getConfig/index.js';
import { printSet, printError, createSpinner } from '../shared/index.js';

@@ -4,0 +4,0 @@ const collectUsages = async ({ config }) => {

@@ -1,6 +0,6 @@

import { getConfig } from '../getConfig/index.js';
import { type Config } from '../getConfig/index.js';
type Params = {
config: Awaited<ReturnType<typeof getConfig>>;
config: Config;
};
declare const findUnusedExports: ({ config }: Params) => Promise<never>;
export { findUnusedExports };

@@ -1,2 +0,2 @@

import { getConfig } from '../getConfig/index.js';
import {} from '../getConfig/index.js';
import { findUnusedExports as _findUnusedExports } from '../findUnusedExports.js';

@@ -3,0 +3,0 @@ import { printError, createSpinner, readJSON } from '../shared/index.js';

import { processBatch } from './processBatch/index.js';
import { getFiles } from './getFiles.js';
import { getFiles } from './getFiles/index.js';
const fileTraversal = async ({ config, pkg, cmd }) => {

@@ -4,0 +4,0 @@ const files = await getFiles({ config });

import { notNil } from '../../shared/index.js';
import { findImport } from './findImport.js';
import { traversal } from './traversal.js';
import { traversal } from './traversal/index.js';
const processBatch = async ({ cmd, files, pkg, tokens, config }) => {

@@ -5,0 +5,0 @@ const pathesPromise = files.map(async (path) => {

@@ -35,2 +35,6 @@ import { parseFile } from '@swc/core';

}
if (node.type === 'ExportDefaultExpression') {
// @ts-expect-error
result.add(node.expression.value);
}
}

@@ -37,0 +41,0 @@ return result;

{
"name": "pure-index",
"type": "module",
"version": "0.0.51",
"version": "0.0.52",
"description": "Utility for monorepos. It helps to find unused exports from packages or get a list of all unique uses of any package",

@@ -6,0 +6,0 @@ "main": "./dist/api/index.js",

# 🌿 Pure Index
Pure Index is utility for monorepos. It helps to find unused exports from packages or get a list of all unique uses of any package.
Pure Index is utility for packages. It helps to find all unused exports or collect used imports of a package. For any local repository.
## Motivation
There is a package `a` which exports 2 functions
We will use the `ui-kit` package as an example. Its _index.ts_ file looks like this
```ts
// "a" package index.ts file
export { Button } from './components/button';
export { Text } from './components/text';
export const T = () => true
export { myFn } from './myFn'
export { ThemeProvider, useTheme, type Theme } from './themes';
...
```
only 1 function from the package is used in the project
All of its exportable code requires support. But is all of it actually being used?
```ts
// some file
You can check this manually by just looping through the exports. It's time consuming and there is a chance of error. What if it's code is used by multiple repositories?
import { T } from 'a'
```
We need to automate this process!
This means that package `a` exports `myFn` for nothing, so we can remove its export and possibly remove all the code.
As the code base develops, a large number of such unnecessary exports may accumulate in the monorepo. Pure Index allows you to find such exports.
## First step
## Usage
1. Install
```sh

@@ -36,133 +29,16 @@ npm install --save-dev pure-index

2. Add the `check-exports` script in the `package.json` of each package that needs to be checked
<!-- Use the `pure-index` call to search within a monorepo and JS API to search across repositories. -->
```diff
"scripts": {
"build": "webpack ./webpack.config.js",
+ "check-exports": "pure-index",
"test": "vitest"
}
```
<!-- ## Monorepos
3. Configure
## Various repositories
4. Use flags if you need to [override](#cli) the config values for package
## How to
## Config
### Find unused exports within a monorepo
Pure Index supports three ways to define config.
### Collect usages within a monorepo
1. `.pure-index.json` config file:
### Find unused exports in various repositories
```json
{
"entry": "index.ts",
"exclude": ["node_modules"],
"extensions": ["ts", "tsx"],
"dir": "my-path",
"batch": 100
}
```
2. or `pure-index` section in `package.json`:
```json
"pure-index": {
"entry": "index.ts",
"exclude": ["node_modules"],
"extensions": ["ts", "tsx"],
"dir": "my-path",
"batch": 100
}
```
3. or a more flexible `.pure-index.js` or `.pure-index.cjs` config file:
```js
module.exports = {
entry: 'index.ts',
exclude: ['node_modules'],
extensions: ['ts', 'tsx'],
dir: 'my-path',
batch: 100
}
```
### Arguments
#### entry (String)
Path to the package index file. relative to the package directory. Default: `'lol'`
#### extensions (Array<string>)
List of file extensions to be considered during the search. Default: `[ts,tsx]`
- `entry (String)` — path to the package index file. relative to the package directory.
- `extensions (Array<string>)` — list of file extensions to be considered during the search.
- `exclude (Array<string>)` — list of directories that will be excluded when searching for imports.
- `dir (String)` — path to the directory where imports should be searched for.
- `batch (Number)` — number of files to be traversed in parallel. Changing the value may speed up or slow down the script. Choose the value yourself.
## CLI
Allows to override the config values for package.
### `--entry, -e`
<details><summary><b>Show instructions</b></summary>
```diff
"scripts": {
"build": "webpack ./webpack.config.js",
- "check-exports": "pure-index",
+ "check-exports": "pure-index --entry ./src/index.ts",
"test": "vitest"
}
```
</details>
### `--collect-usages, -u`
Outputs a list of all unique uses of the package.
```sh
npx pure-index --collect-usages my-package
npx pure-index -u my-package
npx pure-index --collect-usages react-spring
npx pure-index -u react-spring
```
Useful if the package index file contains `export *` syntax. Or to search for all uses of an external package. [More info](#export-)
## Tips
- Use [knip](https://github.com/webpro/knip) or [ts-prune](https://github.com/nadeesha/ts-prune) to clean up unused code inside packages
## Explanation
### How It Works
In fact, the task is to compare all exports and imports of the package. Anything not imported but exported are unused exports.
#### Algorithm
1. collect all package exports into _exports Set_
2. traverse all files where package import may occur
3. if import is found, remove it from _exports Set_
4. if the size of exports _exports Set_ became equal to 0, then exit with success
5. if _exports Set_ size is not equal to 0, then exit with an error
### How It Optimized
1. file reading is divided into batches
2. file is not immediately converted to AST. First the import of the package is searched for in the file. _createReadStream_ is used
3. there is an instant exit with success as soon as the size of _exports Set_ is equal to zero
## Limitations
### export \*
Pure Index when getting a list of exports does not parse `export *` to find out what is exported from there. For projects with this syntax, it may result in an inability to use the library. But Pure Index can help with replacing `export *`. Just run it with the [--collect-usages flag](#--collect-usages--u) and replace `export *` with named exports.
### Collect usages in various repositories -->
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc