A semantic-release plugin that cleans and optimizes package.json before publishing by removing unnecessary development and build-time properties
Daniel Bannert's open source work is supported by the community on GitHub Sponsors
When publishing packages to npm, many properties in package.json
are only needed during development and build time, but not in the published package. This plugin automatically removes unnecessary properties while preserving essential ones needed for the package to work correctly in production.
Key benefits:
- Reduces package size by removing development-only properties
- Prevents leaking internal configuration and metadata
- Maintains a clean and focused package.json for end users
- Customizable property preservation through configuration
npm install @anolilab/semantic-release-clean-package-json
yarn add @anolilab/semantic-release-clean-package-json
pnpm add @anolilab/semantic-release-clean-package-json
The plugin can be configured in the semantic-release configuration file:
Important
Very important: The plugin must be placed before the @semantic-release/github
or @semantic-release/git
and before @anolilab/semantic-release-pnpm
or @semantic-release/npm
plugin otherwise the package.json
will be cleaned and published into GitHub / Your Git Provider.
{
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@anolilab/semantic-release-clean-package-json",
"@anolilab/semantic-release-pnpm",
"@semantic-release/github"
]
}
Step | Description |
---|---|
publish |
- Creates a backup of the original package.json file - Removes all non-preserved properties from package.json - Keeps properties specified in the default list and custom keep option- Preserves specific npm scripts if they are in the keep list - Writes the cleaned package.json file |
success |
- Restores the original package.json from backup - Updates the version number to match the released version - Removes the backup file - Logs success or error messages |
Options | Description | Default |
---|---|---|
pkgRoot |
Directory path to publish. | . |
keep |
Property names to keep. | [] |
Note: The pkgRoot
directory must contain a package.json
. The version will be updated only in the package.json
and npm-shrinkwrap.json
within the pkgRoot
directory.
Note: If you use a shareable configuration that defines one of these options you can set it to false
in your semantic-release configuration in order to use the default value.
The plugin can be configured with custom properties to keep in addition to the default preserved ones:
{
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
[
"@anolilab/semantic-release-clean-package-json",
{
"keep": ["custom field"]
}
],
"@anolilab/semantic-release-pnpm",
"@semantic-release/github"
]
}
When publishing a TypeScript package, you might want to keep TypeScript-specific fields:
{
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
[
"@anolilab/semantic-release-clean-package-json",
{
// This are the default values, just a example
"keep": [
"types",
"typings",
"typesVersions",
"module"
]
}
],
"@anolilab/semantic-release-pnpm",
"@semantic-release/github"
]
}
If your package.json is not in the root directory:
{
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/github",
[
"@anolilab/semantic-release-clean-package-json",
{
"pkgRoot": "dist"
}
],
"@anolilab/semantic-release-pnpm"
]
}
By default, these properties are preserved in package.json
:
name
version
private
publishConfig
scripts.preinstall
scripts.install
scripts.postinstall
scripts.dependencies
files
bin
browser
main
man
dependencies
peerDependencies
peerDependenciesMeta
bundledDependencies
optionalDependencies
engines
os
cpu
description
keywords
author
contributors
license
homepage
repository
bugs
funding
type
exports
imports
publisher
displayName
categories
galleryBanner
preview
contributes
activationEvents
badges
markdown
qna
sponsor
extensionPack
extensionDependencies
extensionKind
icon
fesm2020
fesm2015
esm2020
es2020
types
typings
typesVersions
Libraries in this ecosystem make the best effort to track Node.js’ release schedule. Here’s a post on why we think this is important.
If you would like to help take a look at the list of issues and check our Contributing guidelines.
Note: please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
The anolilab semantic-release-clean-package-json is open-sourced software licensed under the MIT