pure-index
Advanced tools
Comparing version 0.0.6 to 0.0.7
import { join } from 'node:path' | ||
import { lilconfig, lilconfigSync } from 'lilconfig' | ||
import meow from 'meow' | ||
const BASE_CONFIG = { | ||
// like Jest.config.testPathIgnorePatterns | ||
// https://jestjs.io/ru/docs/configuration#testpathignorepatterns-arraystring | ||
entry: 'index.ts', | ||
exclude: new Set(['node_modules']), | ||
babelPlugins: new Set(['typescript']), | ||
indexFilePath: 'index.ts', | ||
batch: { | ||
@@ -15,2 +14,21 @@ default: 100 | ||
const cli = meow( | ||
` | ||
Options | ||
--entry, -e path to the package index file. relative to the package directory | ||
`, | ||
{ | ||
importMeta: import.meta, | ||
allowUnknownFlags: false, | ||
description: false, | ||
flags: { | ||
entry: { | ||
type: 'string', | ||
default: BASE_CONFIG.entry, | ||
shortFlag: 'e' | ||
} | ||
} | ||
} | ||
) | ||
const getConfig = async () => { | ||
@@ -20,3 +38,7 @@ const result = await lilconfig('pure-index', { | ||
}).search() | ||
console.log(cli.flags) | ||
// parseArgs(process.argv) | ||
process.exit(0) | ||
if (!result) { | ||
@@ -30,3 +52,3 @@ return BASE_CONFIG | ||
babelPlugins = [], | ||
indexFilePath = BASE_CONFIG.indexFilePath, | ||
entry = BASE_CONFIG.entry, | ||
batch = {} | ||
@@ -39,5 +61,5 @@ } | ||
: { | ||
entry, | ||
exclude: new Set([...BASE_CONFIG.exclude, ...exclude]), | ||
babelPlugins: new Set([...BASE_CONFIG.babelPlugins, ...babelPlugins]), | ||
indexFilePath, | ||
batch: { | ||
@@ -44,0 +66,0 @@ default: batch.default || BASE_CONFIG.batch.defaul |
@@ -12,4 +12,4 @@ import { join } from 'node:path' | ||
* config: { | ||
* entry: string, | ||
* babelPlugins: Set<string>, | ||
* indexFilePath: string, | ||
* }, | ||
@@ -24,3 +24,3 @@ * pkg: { | ||
const getExports = async ({ config, pkg }) => { | ||
const code = await readFile(join(pkg.path, config.indexFilePath)) | ||
const code = await readFile(join(pkg.path, config.entry)) | ||
const result = new ObservableSet() | ||
@@ -27,0 +27,0 @@ |
{ | ||
"name": "pure-index", | ||
"type": "module", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "some description", | ||
@@ -18,2 +18,3 @@ "main": "./bin/index.js", | ||
"lilconfig": "3.0.0", | ||
"meow": "13.0.0", | ||
"nanospinner": "1.1.0" | ||
@@ -20,0 +21,0 @@ }, |
104
README.md
@@ -1,1 +0,103 @@ | ||
# Pure Index | ||
# 🌿 Pure Index | ||
Pure Index is utility for monorepos. It helps to find unused exports from packages. | ||
## Motivation | ||
There is a package `a` which exports 2 functions | ||
```ts | ||
// "a" package index.ts file | ||
export const T = () => true | ||
export { myFn } from './myFn' | ||
``` | ||
only 1 function from the package is used in the project | ||
```ts | ||
// some file | ||
import { T } from 'a' | ||
``` | ||
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. | ||
## Usage | ||
1. Install | ||
```sh | ||
npm install --save-dev pure-index | ||
``` | ||
2. Add the `check-exports` script in the `package.json` of each package that needs to be checked | ||
```diff | ||
"scripts": { | ||
"build": "webpack ./webpack.config.js", | ||
+ "check-exports": "pure-index", | ||
"test": "vitest" | ||
} | ||
``` | ||
3. Configure | ||
## Config | ||
Pure Index supports two ways to define config. | ||
1. `.pure-index.json` config file: | ||
```json | ||
{ | ||
"entry": "index.ts", | ||
"exclude": ["node_modules"], | ||
"babelPlugins": ["typescript"], | ||
"batch": { | ||
"default": 100 | ||
} | ||
} | ||
``` | ||
2. or `pure-index` section in `package.json`: | ||
```json | ||
"pure-index": { | ||
"entry": "index.ts", | ||
"exclude": ["node_modules"], | ||
"babelPlugins": ["typescript"], | ||
"batch": { | ||
"default": 100 | ||
} | ||
} | ||
``` | ||
### Arguments | ||
- `entry (String)` — path to the package index file. relative to the package directory. | ||
- `exclude (Array<string>)` — list of directories that will be excluded when searching for imports. | ||
- `babelPlugins (Array<string>)` — list of babel plugins that will be used when parsing files. | ||
- `batch.default (Number)` — number of files to be traversed in parallel. changing the value may speed up or slow down the script. choose the value yourself. | ||
## 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. | ||
#### Base 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 |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14598
351
104
6
+ Addedmeow@13.0.0
+ Addedmeow@13.0.0(transitive)