Skip to content

Extend CSS tree shaking to account for more scenarios #1492

@asadkn

Description

@asadkn

To build upon existing style sanitizer, I have to maintain a fork due to lack of hooks. These hooks would be especially useful for the tree shaker.

  • amp_selector_should_include would be a great hook for $should_include to allow or remove classes in the tree shaking process. Something like:

$should_include = apply_filters( 'amp_selector_should_include', $should_include, $selector, $parsed_selector );

I am using this to write a custom check to strip out few more classes that linger around despite being unnecessary, like some multi-class selectors such as .something.else remaining even when unused. This filter can essentially whitelist classes that might be wrongly removed, or blacklist classes that are unnecessarily preserved.

  • Or more importantly, allowing filtering of CSS parts would be great. Just before $sheet_size is calculated, a filter such as following could be great:

$stylesheet = apply_filters( 'amp_stylesheet_part', $stylesheet );

Using this filter, a plugin/theme can do further processing to remove CSS as necessary. I am using it to filter empty @media and @support blocks but it could also be used for addition or further removal.

P.S. thanks for the tremendous effort on v1.0. I have been using the beta in production and have no complaints so far.

Metadata

Metadata

Assignees

Labels

CSSEnhancementNew feature or improvement of an existing oneGroomedP2Low priorityWS:CoreWork stream for Plugin corewontfix

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions