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

fix: consider static attributes that are inlined in the template #14249

Merged
merged 8 commits into from
Nov 11, 2024
5 changes: 5 additions & 0 deletions .changeset/mighty-ads-appear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: consider static attributes that are inlined in the template
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/** @import { ComponentContext } from '../../types' */
import { is_event_attribute, is_text_attribute } from '../../../../../utils/ast.js';
import * as b from '../../../../../utils/builders.js';
import { can_inline_variable } from '../../utils.js';
import { build_template_literal, build_update } from './utils.js';

/**
Expand Down Expand Up @@ -97,7 +98,7 @@ export function process_children(nodes, initial, is_element, { visit, state }) {

let child_state = state;

if (is_static_element(node)) {
if (is_static_element(node, state.scope)) {
skipped += 1;
} else if (node.type === 'EachBlock' && nodes.length === 1 && is_element) {
node.metadata.is_controlled = true;
Expand All @@ -124,8 +125,9 @@ export function process_children(nodes, initial, is_element, { visit, state }) {

/**
* @param {SvelteNode} node
* @param {ComponentContext["state"]["scope"]} scope
*/
function is_static_element(node) {
function is_static_element(node, scope) {
if (node.type !== 'RegularElement') return false;
if (node.fragment.metadata.dynamic) return false;

Expand All @@ -139,6 +141,18 @@ function is_static_element(node) {
}

if (attribute.value !== true && !is_text_attribute(attribute)) {
// in this situation we are already inlining the binding in the template
// so even if it's not a text attribute we can consider the element static
if (
typeof attribute.value !== 'boolean' &&
!Array.isArray(attribute.value) &&
attribute.value.expression.type === 'Identifier'
) {
const binding = scope.get(attribute.value.expression.name);
if (binding) {
return can_inline_variable(binding);
}
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ var root = $.template(`<picture><source srcset="${__DECLARED_ASSET_0__}" type="i

export default function Inline_module_vars($$anchor) {
var picture = root();
var source = $.child(picture);
var source_1 = $.sibling(source, 2);
var source_2 = $.sibling(source_1, 2);
var img = $.sibling(source_2, 2);

$.next(6);
$.reset(picture);
$.append($$anchor, picture);
}
Loading