-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
fix: better account for render tags when pruning CSS #14456
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
Merged
Merged
Changes from 20 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
2867cc1
WIP
Rich-Harris b84ed1e
Merge branch 'main' into better-pruning
Rich-Harris 33026a7
link snippets to render tags and vice versa
Rich-Harris fdce504
WIP
Rich-Harris bc46771
Merge branch 'main' into better-pruning
Rich-Harris af7ebb0
tidy up
Rich-Harris 22d56e8
tidy up
Rich-Harris e3b6e21
merge main
Rich-Harris 344c132
update test
Rich-Harris 1dd8772
more accurate pruning
Rich-Harris 7b6aae1
failing test
Rich-Harris 3707cbb
sibling stuff
Rich-Harris 7e38ae6
merge main
Rich-Harris 947e763
add test
Rich-Harris 7520988
add failing test
Rich-Harris 0dad618
another failing test
Rich-Harris bef6c15
more tests
Rich-Harris 3d961f5
fix some stuff
Rich-Harris 21fff48
skip test for now
Rich-Harris c7273b3
changeset
Rich-Harris d1d99d2
check
Rich-Harris 9fb9881
use existing helpers
Rich-Harris e43baca
better comments
Rich-Harris 300c15d
Update packages/svelte/src/compiler/phases/2-analyze/visitors/RenderT…
Rich-Harris a22f9d5
DRY out
Rich-Harris d907155
this appears to be unnecessary
Rich-Harris 122d824
no need to pass state around
Rich-Harris fefc4e9
fix and simplify
Rich-Harris 301c402
move code to where it is used
Rich-Harris 16f28a1
this bit is nonsensical, parent cannot exist if there was no combinator
Rich-Harris bb5c311
simplify
Rich-Harris 95418a6
simplify
Rich-Harris 2a16fca
bit more
Rich-Harris 8e3a529
simplify
Rich-Harris 01ea8c2
handle `:has`
dummdidumm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'svelte': patch | ||
| --- | ||
|
|
||
| fix: better account for render tags when pruning CSS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| /** @import { AST } from '#compiler' */ | ||
| /** @import { Expression } from 'estree' */ | ||
| /** @import { AnalysisState, Context } from '../../types' */ | ||
| import * as e from '../../../../errors.js'; | ||
| import { get_attribute_expression, is_expression_attribute } from '../../../../utils/ast.js'; | ||
|
|
@@ -15,6 +16,70 @@ import { mark_subtree_dynamic } from './fragment.js'; | |
| * @param {Context} context | ||
| */ | ||
| export function visit_component(node, context) { | ||
| node.metadata.path = [...context.path]; | ||
|
|
||
| // link this node to all the snippets that it could render, so that we can prune CSS correctly | ||
| node.metadata.snippets = new Set(); | ||
|
|
||
| let resolved = true; | ||
|
|
||
| for (const attribute of node.attributes) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably not really relevant, but this iterates over the attributes, and below we're iterating over them again, feels a bit wasteful - but merging them into one probably makes everything harder to reason about |
||
| /** @type {Expression | undefined} */ | ||
| let expression; | ||
|
|
||
| if (attribute.type === 'SpreadAttribute' || attribute.type === 'BindDirective') { | ||
| resolved = false; | ||
| continue; | ||
| } | ||
|
|
||
| if (attribute.type !== 'Attribute' || attribute.value === true) { | ||
| continue; | ||
| } | ||
|
|
||
| if (Array.isArray(attribute.value)) { | ||
| if (attribute.value.length === 1 && attribute.value[0].type === 'ExpressionTag') { | ||
| expression = attribute.value[0].expression; | ||
| } | ||
| } else { | ||
| expression = attribute.value.expression; | ||
| } | ||
Rich-Harris marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (!expression) continue; | ||
|
|
||
| if (expression.type === 'Identifier') { | ||
| const binding = context.state.scope.get(expression.name); | ||
|
|
||
| if ( | ||
| binding && | ||
| binding.declaration_kind !== 'import' && | ||
| binding.kind !== 'prop' && | ||
| binding.kind !== 'rest_prop' && | ||
| binding.kind !== 'bindable_prop' && | ||
| binding.initial?.type !== 'SnippetBlock' | ||
Rich-Harris marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ) { | ||
| resolved = false; | ||
| } | ||
|
|
||
| if (binding?.initial?.type === 'SnippetBlock') { | ||
| node.metadata.snippets.add(binding.initial); | ||
| } | ||
| } else { | ||
| // we can't safely know which snippets this component could render, | ||
| // so we deopt. this _could_ result in unused CSS not being discarded | ||
| resolved = false; | ||
| } | ||
| } | ||
|
|
||
| if (resolved) { | ||
| for (const child of node.fragment.nodes) { | ||
| if (child.type === 'SnippetBlock') { | ||
| node.metadata.snippets.add(child); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| context.state.analysis.snippet_renderers.set(node, resolved); | ||
|
|
||
| mark_subtree_dynamic(context.path); | ||
|
|
||
| for (const attribute of node.attributes) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.