Skip to content

Commit 49b5d7f

Browse files
committed
SSR
1 parent ea19403 commit 49b5d7f

File tree

4 files changed

+23
-13
lines changed
  • packages/svelte

4 files changed

+23
-13
lines changed

packages/svelte/src/compiler/phases/3-transform/server/visitors/shared/utils.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,17 @@ export function process_children(nodes, { visit, state }) {
4444
if (node.type === 'Text' || node.type === 'Comment') {
4545
quasi.value.cooked +=
4646
node.type === 'Comment' ? `<!--${node.data}-->` : escape_html(node.data);
47-
} else if (node.type === 'ExpressionTag' && node.expression.type === 'Literal') {
48-
if (node.expression.value != null) {
49-
quasi.value.cooked += escape_html(node.expression.value + '');
50-
}
5147
} else {
52-
expressions.push(b.call('$.escape', /** @type {Expression} */ (visit(node.expression))));
48+
const evaluated = state.scope.evaluate(node.expression);
49+
50+
if (evaluated.is_known) {
51+
quasi.value.cooked += escape_html((evaluated.value ?? '') + '');
52+
} else {
53+
expressions.push(b.call('$.escape', /** @type {Expression} */ (visit(node.expression))));
5354

54-
quasi = b.quasi('', i + 1 === sequence.length);
55-
quasis.push(quasi);
55+
quasi = b.quasi('', i + 1 === sequence.length);
56+
quasis.push(quasi);
57+
}
5658
}
5759
}
5860

packages/svelte/src/compiler/phases/scope.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class Evaluation {
113113
* @readonly
114114
* @type {boolean}
115115
*/
116-
is_known = false;
116+
is_known = true;
117117

118118
/**
119119
* True if the value is known to not be null/undefined
@@ -165,7 +165,12 @@ class Evaluation {
165165
break;
166166
}
167167

168-
if (!binding.updated && binding.initial !== null) {
168+
const is_prop =
169+
binding.kind === 'prop' ||
170+
binding.kind === 'rest_prop' ||
171+
binding.kind === 'bindable_prop';
172+
173+
if (!binding.updated && binding.initial !== null && !is_prop) {
169174
const evaluation = binding.scope.evaluate(/** @type {Expression} */ (binding.initial));
170175
for (const value of evaluation.values) {
171176
this.values.add(value);
@@ -293,7 +298,7 @@ class Evaluation {
293298
break;
294299

295300
case 'UnaryExpression':
296-
const argument = scope.evaluate(expression.argument);
301+
var argument = scope.evaluate(expression.argument);
297302

298303
if (argument.is_known) {
299304
this.values.add(unary[expression.operator](argument.value));
@@ -326,6 +331,7 @@ class Evaluation {
326331
// TypeScript getting confused
327332
throw new Error(`Unknown operator ${expression.operator}`);
328333
}
334+
break;
329335

330336
default:
331337
this.values.add(UNKNOWN);
@@ -347,7 +353,9 @@ class Evaluation {
347353
}
348354
}
349355

350-
this.is_known = this.values.size === 1 && typeof this.value !== 'symbol';
356+
if (this.values.size > 1 || typeof this.value === 'symbol') {
357+
this.is_known = false;
358+
}
351359
}
352360
}
353361

packages/svelte/tests/snapshot/samples/nullish-coallescence-omittance/_expected/server/index.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ export default function Nullish_coallescence_omittance($$payload) {
44
let name = 'world';
55
let count = 0;
66

7-
$$payload.out += `<h1>Hello, ${$.escape(name)}!</h1> <b>${$.escape(1 ?? 'stuff')}${$.escape(2 ?? 'more stuff')}${$.escape(3 ?? 'even more stuff')}</b> <button>Count is ${$.escape(count)}</button> <h1>Hello, ${$.escape(name ?? 'earth' ?? null)}</h1>`;
7+
$$payload.out += `<h1>Hello, world!</h1> <b>123</b> <button>Count is ${$.escape(count)}</button> <h1>Hello, world</h1>`;
88
}

packages/svelte/tests/sourcemaps/samples/attached-sourcemap/input.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
replace_me_script = 'hello'
99
;
1010
</script>
11-
<h1 class="done_replace_style_2">{done_replace_script_2}</h1>
11+
<h1 class="done_replace_style_2">{Math.random() < 1 && done_replace_script_2}</h1>

0 commit comments

Comments
 (0)