Skip to content

Commit 4b8c249

Browse files
committed
better inlining
1 parent 3acb74f commit 4b8c249

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/Fragment.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ function join_template(items) {
227227

228228
const last = /** @type {TemplateElement} */ (expression.quasis.at(-1));
229229
quasi.value.cooked += /** @type {string} */ (last.value.cooked);
230+
} else if (expression.type === 'Literal') {
231+
/** @type {string} */ (quasi.value.cooked) += expression.value;
230232
} else {
231233
template.expressions.push(expression);
232234
template.quasis.push((quasi = b.quasi('')));

packages/svelte/src/compiler/phases/3-transform/client/visitors/RegularElement.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @import { Expression, ExpressionStatement, Identifier, MemberExpression, ObjectExpression, Statement } from 'estree' */
1+
/** @import { Expression, ExpressionStatement, Identifier, Literal, MemberExpression, ObjectExpression, Statement } from 'estree' */
22
/** @import { AST } from '#compiler' */
33
/** @import { SourceLocation } from '#shared' */
44
/** @import { ComponentClientTransformState, ComponentContext } from '../types' */
@@ -596,10 +596,31 @@ function build_element_attribute_update_assignment(element, node_id, attribute,
596596
is_inlinable_attribute(attribute);
597597

598598
if (can_inline) {
599-
// TODO would be great if we could avoid `$.attr` when we know the value
600-
context.state.template.push(
601-
b.call('$.attr', b.literal(name), value, is_boolean_attribute(name) && b.true)
602-
);
599+
/** @type {Literal | undefined} */
600+
let literal = undefined;
601+
602+
if (value.type === 'Literal') {
603+
literal = value;
604+
} else if (value.type === 'Identifier') {
605+
const binding = context.state.scope.get(value.name);
606+
if (binding && binding.initial?.type === 'Literal' && !binding.reassigned) {
607+
literal = binding.initial;
608+
}
609+
}
610+
611+
if (literal && escape_html(literal.value, true) === String(literal.value)) {
612+
if (is_boolean_attribute(name)) {
613+
if (literal.value) {
614+
context.state.template.push(` ${name}`);
615+
}
616+
} else {
617+
context.state.template.push(` ${name}="`, value, '"');
618+
}
619+
} else {
620+
context.state.template.push(
621+
b.call('$.attr', b.literal(name), value, is_boolean_attribute(name) && b.true)
622+
);
623+
}
603624
} else {
604625
state.init.push(update);
605626
}

0 commit comments

Comments
 (0)