Skip to content

Add a hook to allow tree shaking of unused CSS rules #4419

@bep

Description

@bep

I have tested out a hook (see link to patch below) that's logically something like shouldDropCSSRule(kind,selector). Testing it out on a real site using the NPM provided Bootstrap and build it with ESBuild with and without tree shaking (verified visually and with a Chrome diff extension):

Options Output size
Default 280 KB
Default gzipped 34 KB
Default minified 230 KB
Default minified gzipped 31 KB
Treeshaken 33 KB
Treeshaken gzipped 6.5 KB
Treeshaken minified 29 KB
Treeshaken minified gzipped 6.2 KB

I understand that this may be a hard sell, but those who don't try never win, and: 1. This should be useful to many and 2. In my head this strikes a good balance of not exposing too much internals.

Note that the code linked to below is mostly written by Claude Code for demo purposes to see how viable this approach would be, but feel free to use it as you please:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions