Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove @charset and @import rules when building CSS #43

Merged
merged 2 commits into from
Dec 14, 2023

Conversation

thingalon
Copy link
Member

@charset rules can only appear at the very beginning of any CSS block and are technically invalid anywhere else. As Critical CSS joins multiple files together, it's cleaner to strip charset rules. Technically charset rules are only needed for non-utf8 stylesheets, which are vanishingly rare.

On testing, it appears that non-utf8 stylesheets are currently not supported anyway, so we don't lose anything by stripping the tags. If we come across sites with non-utf8 stylesheets, we can consider adding explicit support down the road.

This update also strips out @import directives - both because they are also fussy about where they appear in a CSS file, and because by the time you @import something you are waiting on a resource to load - so it has no business in a Critical CSS block.

*** To test ***

I recommend using @haqadn's yalc setup to test the change in Boost - pc9hqz-1NI-p2 - and verify that no @charset rules appear in the resultant Critical CSS block.

@thingalon thingalon added the bug Something isn't working label Dec 12, 2023
@thingalon thingalon self-assigned this Dec 12, 2023
src/style-ast.ts Outdated
@@ -75,7 +75,7 @@ export class StyleAST {
const clone = new StyleAST( this.css, csstree.clone( this.ast ), this.errors );

clone.pruneMediaQueries();
clone.pruneKeyframes();
clone.pruneAtRules();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

going by naming only - wouldn't this remove @media too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it prunes unwanted At-rules, including @keyframes, @charset and @import. Would it help if it specifies unwanted in the fn name?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer pruneAtRules(['keyframe', 'charset', 'import']). Barring that, "unwanted" might be ok too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Yes, that's much cleaner. Applied in 289bcc6. Thanks :)

Copy link
Member

@pyronaur pyronaur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonderful! 🎉

@thingalon thingalon merged commit 1897c7b into trunk Dec 14, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants