clean-webpack-plugin

A webpack plugin that automatically removes stale build output before and after every compilation.

Library
npm
v4.0.0
1,940stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
44/100Fair
Development Activity0
Maintenance20
Community56
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
73/100Good
Architecture78
Code Quality85
Innovation45
Learning Curve85

clean-webpack-plugin is a small, focused webpack plugin that keeps your output directory tidy. By default it deletes everything inside webpack’s output.path once before the first build, and then automatically removes any asset from a previous compilation that is no longer produced by the current one — so switching branches, renaming chunks, or changing content hashes never leaves orphaned files behind in dist/.

Under the hood it hooks into webpack’s emit and done compiler lifecycle events and delegates the actual filesystem work to del, giving it full glob pattern support (including negative ! patterns to protect specific files or directories). Options like dry (simulate without deleting), verbose (log every removal), cleanStaleWebpackAssets, and protectWebpackAssets let teams tune exactly how aggressive the cleanup is, while dangerouslyAllowCleanPatternsOutsideProject gives an explicit, clearly-named escape hatch for the rare case of cleaning paths outside the project root.

It supports webpack 4 and 5, ships full TypeScript type definitions, and requires no configuration to get useful default behavior — for most projects, adding new CleanWebpackPlugin() to the plugins array is the entire integration.

What You Get

  • Zero-config default behavior — new CleanWebpackPlugin() cleans the entire output directory once before the first build
  • Automatic removal of stale assets after every rebuild, comparing the current compilation’s asset list against the previous one
  • Glob-based pattern control via cleanOnceBeforeBuildPatterns and cleanAfterEveryBuildPatterns, including negative !exclude patterns
  • A dry mode that logs what would be deleted without touching the filesystem, useful for verifying patterns before enabling real deletion
  • Full TypeScript type definitions shipped with the package

Common Use Cases

  • Preventing old content-hashed bundles (e.g. main.[contenthash].js) from piling up in dist/ across rebuilds
  • Cleaning a build directory once at the start of a fresh webpack --mode production run without a separate rm -rf dist step in package.json scripts
  • Removing files generated by other plugins or loaders that aren’t tracked as webpack assets, via cleanAfterEveryBuildPatterns
  • Verifying which files a cleanup configuration would remove before enabling it, using dry: true and verbose: true together

Under The Hood

Architecture The entire plugin is a single class (CleanWebpackPlugin in src/clean-webpack-plugin.ts) that taps into two webpack compiler hooks: emit (used once, guarded by an initialClean flag, to run the before-build cleanup) and done (used on every compilation to diff the current asset list against the previous one and remove anything stale). State is minimal — just the running list of currentAssets and the resolved outputPath — and all constructor-time validation (rejecting non-plain-object options, rejecting the removed allowExternal option) happens up front so misconfiguration fails fast with a clear error rather than silently doing nothing.

Tech Stack Written in TypeScript and compiled with Babel (@babel/preset-typescript) rather than tsc directly, with tsc --project tsconfig.types.json used only to emit declaration files. The only runtime dependency is del (glob-based file deletion), and webpack itself is a peer dependency supporting >=4.0.0 <6.0.0. Tooling includes ESLint with a shared config, Prettier, Husky-driven pre-commit/pre-push hooks running lint-staged, and a test.all step that runs the suite against multiple installed webpack versions via a custom dev-utils/test-supported-webpack-versions.js harness.

Code Quality Test coverage is extensive relative to the implementation — the single source file is under 350 lines while its Jest test suite exceeds 1,300 lines, exercising real webpack compiler runs (via temp-sandbox for isolated filesystem fixtures) rather than mocking webpack’s internals. Errors are handled explicitly: filesystem failures are caught in removeFiles, checked against a known error-message pattern, and re-thrown with a clearer message when the failure is the common “outside project root” case. CI runs on both Linux (CircleCI) and Windows (AppVeyor) across multiple Node versions, with type-checking and linting as separate required steps.

What Makes It Unique The plugin’s main design decision is being deliberately narrow: rather than trying to be a general file-management plugin, it does exactly one thing — diffing webpack’s own asset output between builds — and exposes the underlying del glob syntax directly instead of inventing a new pattern language. The dangerouslyAllowCleanPatternsOutsideProject naming is a notable safety-by-naming choice: the option that expands blast radius outside the project directory is impossible to enable by accident because its name states the risk.

Used by 7 apps in this directory

PHP
78%
Other

Craft CMS

CMS

3,605

A developer-first PHP CMS with clean-slate content modeling, auto-generated GraphQL API, and a four-tier edition system that scales from solo projects to enterprise deployments.

View details
96
Repo Health
83
Technical
62
Dependency
Built with
PHP78%
JavaScript14%
Updated yesterday
TypeScript
98%
Other

Kibana

Analytics · Monitoring

21,284

Your open source window into the Elastic Stack — query, visualize, and act on data stored in Elasticsearch with real-time dashboards, AI-assisted search, and automated alerting.

View details
98
Repo Health
87
Technical
66
Dependency
Built with
TypeScript98%
Updated yesterday
TypeScript
85%
GPL 3.0

Notesnook

Note Taking · File Storage · Security

14,533

End-to-end encrypted, open-source note-taking where your data stays yours — even from the server.

View details
90
Repo Health
84
Technical
60
Dependency
Built with
TypeScript85%
JavaScript12%
Updated 2 days ago
JavaScript
26%
AGPL 3.0

Omnivore

Knowledge Management · Bookmarks Archiving · Note Taking

16,228

Self-hosted read-it-later platform with highlights, newsletters, PDFs, and seamless Obsidian and Logseq integration.

View details
88
Repo Health
74
Technical
65
Dependency
Built with
JavaScript26%
TypeScript25%
HTML19%
Updated 2 days ago
TypeScript
50%
ZLIB

Portainer

Devops

38,446

A lightweight, open-source web UI that puts Docker, Kubernetes, and Podman management within reach of any team—no CLI expertise required.

View details
92
Repo Health
79
Technical
65
Dependency
Built with
TypeScript50%
Go38%
Updated 5 days ago
Vue
47%
AGPL 3.0

Wiki.js

Knowledge Management · Collaboration

28,848

A modern, self-hosted wiki platform built on Node.js with a rich plugin ecosystem for authentication, search, storage, and rendering that adapts to any team's infrastructure.

View details
72
Repo Health
67
Technical
60
Dependency
Built with
Vue47%
JavaScript42%
Updated today
Go
75%
AGPL 3.0

ZITADEL

Authentication

14,949

Open-source, API-first identity platform delivering multi-tenancy, Passkeys, OIDC, SAML, and SCIM without vendor lock-in.

View details
91
Repo Health
81
Technical
68
Dependency
Built with
Go75%
TypeScript12%
Updated 3 days ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search