{"id":30505402,"url":"https://github.com/kshutkin/package-prune","last_synced_at":"2026-02-28T03:17:01.521Z","repository":{"id":311501972,"uuid":"1032745510","full_name":"kshutkin/package-prune","owner":"kshutkin","description":"Prune package.json before publish","archived":false,"fork":false,"pushed_at":"2025-08-24T21:03:29.000Z","size":35,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-08-24T23:56:03.078Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kshutkin.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-08-05T19:00:40.000Z","updated_at":"2025-08-24T21:02:43.000Z","dependencies_parsed_at":"2025-08-24T23:56:13.744Z","dependency_job_id":"687906bc-b5d5-47ec-a604-9a0d29782137","html_url":"https://github.com/kshutkin/package-prune","commit_stats":null,"previous_names":["kshutkin/package-prune"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/kshutkin/package-prune","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kshutkin%2Fpackage-prune","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kshutkin%2Fpackage-prune/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kshutkin%2Fpackage-prune/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kshutkin%2Fpackage-prune/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kshutkin","download_url":"https://codeload.github.com/kshutkin/package-prune/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kshutkin%2Fpackage-prune/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272000395,"owners_count":24856193,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-25T02:00:12.092Z","response_time":1107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-08-25T15:22:38.457Z","updated_at":"2026-02-28T03:17:01.516Z","avatar_url":"https://github.com/kshutkin.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# package-prune\n\nPrune `package.json` before publishing to npm. Removes `devDependencies`, strips unnecessary scripts, cleans up junk files, optimizes the `files` array, and optionally flattens dist directories - so your published package is lean and clean.\n\n## Installation\n\n```sh\nnpm install -D pkgprn\n```\n\nOr with other package managers:\n\n```sh\npnpm add -D pkgprn\nyarn add -D pkgprn\n```\n\nYou can also run it directly with `npx`:\n\n```sh\nnpx pkgprn\n```\n\n## Usage\n\nThe recommended way to use `pkgprn` is as a `prepack` script in your `package.json`:\n\n```json\n{\n    \"scripts\": {\n        \"prepack\": \"pkgprn\"\n    }\n}\n```\n\nThis ensures your `package.json` is pruned automatically every time you run `npm pack` or `npm publish`.\n\nYou can also run it manually:\n\n```sh\nnpx pkgprn [options]\n```\n\nThe tool reads the `package.json` in the current working directory, applies all transformations in place, and writes the result back.\n\n## What It Does\n\nBy default, `pkgprn` performs the following steps:\n\n1. **Removes `devDependencies`** - Strips the entire `devDependencies` field from `package.json`.\n2. **Removes `packageManager`** - Strips the `packageManager` field.\n3. **Prunes scripts** - Removes scripts that are not relevant to package consumers (based on the selected [profile](#profiles)).\n4. **Removes junk files** - Deletes OS and editor artifacts (`.DS_Store`, `*.orig`, `.*.swp`, `._*`, etc.) from the package directory.\n5. **Optimizes the `files` array** - Collapses individual file entries into their parent directory when all files in that directory are already listed, and removes entries that npm always includes automatically (`package.json`, `README`, `LICENSE`).\n6. **Cleans up unlisted files** - Removes files and directories not covered by the `files` array, then drops the `files` field itself (since only the included files remain on disk).\n\nAdditional optional features can be enabled via flags:\n\n- **Flatten** dist directories into the package root.\n- **Remove sourcemaps** and their `//# sourceMappingURL=` references.\n\n## Options\n\n| Flag                  | Type                | Default   | Description                                                                                                                    |\n| --------------------- | ------------------- | --------- | ------------------------------------------------------------------------------------------------------------------------------ |\n| `--profile`           | `string`            | `library` | Script-retention profile (`library` or `app`).                                                                                 |\n| `--flatten`           | `string \\| boolean` | `false`   | Flatten dist directories to the package root. Pass without a value to auto-detect, or provide comma-separated directory names. |\n| `--remove-sourcemaps` | `boolean`           | `false`   | Delete `.map` files and strip `sourceMappingURL` comments from source files.                                                   |\n| `--optimize-files`    | `boolean`           | `true`    | Optimize the `files` array by collapsing entries.                                                                              |\n| `--cleanup-files`     | `boolean`           | `true`    | Remove files not listed in the `files` array.                                                                                  |\n| `--version`           |                     |           | Show version number.                                                                                                           |\n| `--help`              |                     |           | Show help message.                                                                                                             |\n\n## Profiles\n\nProfiles control which npm lifecycle scripts are kept in the published `package.json`. All other scripts are removed.\n\n### `library` (default)\n\nKeeps only the scripts that npm runs during package installation:\n\n- `preinstall`, `install`, `postinstall`\n- `prepublish`\n- `preprepare`, `prepare`, `postprepare`\n\n### `app`\n\nKeeps everything from the `library` profile, plus runtime and test lifecycle scripts:\n\n- `prestart`, `start`, `poststart`\n- `prerestart`, `restart`, `postrestart`\n- `prestop`, `stop`, `poststop`\n- `pretest`, `test`, `posttest`\n\n## Flattening\n\nFlattening moves files from a dist directory (e.g. `dist/`) into the package root and updates all references in `package.json` (`main`, `module`, `exports`, `bin`, `types`, `typesVersions`, etc.) accordingly.\n\n### Auto-detect\n\nWhen `--flatten` is passed without a value, `pkgprn` inspects `main`, `bin`, `module`, `exports`, `types`, and other entry-point fields to find the longest common directory prefix, and flattens that:\n\n```sh\npkgprn --flatten\n```\n\n### Explicit directories\n\nYou can specify one or more directories to flatten (comma-separated):\n\n```sh\npkgprn --flatten dist\npkgprn --flatten dist,lib\n```\n\n### What flattening does\n\n1. Identifies all files in the target directory/directories.\n2. Checks for naming conflicts with existing root-level files.\n3. Moves all files to the root (preserving subdirectory structure relative to the dist directory).\n4. Removes the now-empty dist directory.\n5. Rewrites all path references in `package.json` to point to the new locations.\n6. **Adjusts sourcemap `sources` paths** (explicit directories only) - when `.map` files are moved, their `sources` entries are rewritten so they still resolve to the correct original files. This also handles cross-directory references (e.g. a `.d.ts.map` in `types/` pointing at files in `dist/`) and incorporates any `sourceRoot` into the individual source paths.\n7. Updates the `files` array.\n8. Cleans up any leftover export-map stub directories that only contain a `package.json`.\n\n## Examples\n\n### Basic library\n\n```json\n{\n    \"scripts\": {\n        \"build\": \"tsc\",\n        \"test\": \"vitest\",\n        \"prepack\": \"pkgprn\"\n    }\n}\n```\n\nAfter packing, `build` and `test` are removed; `devDependencies` and `packageManager` are gone.\n\n### Library with flattened dist\n\n```json\n{\n    \"scripts\": {\n        \"build\": \"rollup -c\",\n        \"prepack\": \"pkgprn --flatten dist\"\n    },\n    \"main\": \"dist/index.js\",\n    \"types\": \"dist/index.d.ts\",\n    \"files\": [\"dist\"]\n}\n```\n\nAfter packing, `dist/index.js` becomes `index.js`, `main` points to `index.js`, and the `dist` directory is gone.\n\n### Application with sourcemap removal\n\n```sh\npkgprn --profile app --remove-sourcemaps\n```\n\n### Disable file cleanup\n\nIf you only want script and dependency pruning without touching files on disk:\n\n```sh\npkgprn --no-cleanup-files --no-optimize-files\n```\n\n## Programmatic API\n\nThe core pruning logic is also available as a function:\n\n```js\nimport { prunePkg } from \"pkgprn\";\n\nconst pkg = JSON.parse(await readFile(\"package.json\", \"utf8\"));\n\nawait prunePkg(\n    pkg,\n    {\n        profile: \"library\",\n        flatten: false,\n        removeSourcemaps: false,\n        optimizeFiles: true,\n        cleanupFiles: true,\n    },\n    logger,\n);\n```\n\n### `prunePkg(pkg, options, logger)`\n\n- **`pkg`** - A mutable `package.json` object. Modified in place.\n- **`options`** - An options object matching the CLI flags.\n- **`logger`** - A logger instance (from [`@niceties/logger`](https://www.npmjs.com/package/@niceties/logger)).\n\n## Ignored Files\n\n`pkgprn` automatically removes files that npm itself always ignores:\n\n- `.DS_Store`, `.git`, `.hg`, `.svn`, `CVS`\n- `.lock-wscript`, `config.gypi`, `npm-debug.log`\n- `*.orig`, `.*.swp`, `._*`, `.wafpickle-N`\n- `node_modules`, `package-lock.json`, `pnpm-lock.yaml`, `yarn.lock`, `bun.lockb`, `.npmrc`\n\nFiles that npm always includes are preserved regardless of the `files` array:\n\n- `package.json`\n- `README` (any extension, case-insensitive)\n- `LICENSE` / `LICENCE` (any extension, case-insensitive)\n- The file referenced by `main`\n- Files referenced by `bin`\n\n## License\n\n[MIT](./LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkshutkin%2Fpackage-prune","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkshutkin%2Fpackage-prune","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkshutkin%2Fpackage-prune/lists"}