-
Notifications
You must be signed in to change notification settings - Fork 5
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
Conversation
… appear mid-stream
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(); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's much cleaner. Applied in 289bcc6. Thanks :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wonderful! 🎉
@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 stripcharset
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.