Skip to content

Commit c43bfeb

Browse files
committed
fix: allow accessing snippet in script tag
1 parent 707682e commit c43bfeb

File tree

6 files changed

+37
-8
lines changed

6 files changed

+37
-8
lines changed

.changeset/few-horses-wink.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: allow accessing snippet in script tag

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** @import { ArrowFunctionExpression, BlockStatement, CallExpression } from 'estree' */
1+
/** @import { ArrowFunctionExpression, BlockStatement, CallExpression, Statement } from 'estree' */
22
/** @import { AST } from '#compiler' */
33
/** @import { ComponentContext } from '../types.js' */
44
import { dev } from '../../../../state.js';
@@ -15,21 +15,24 @@ export function SnippetBlock(node, context) {
1515
body.body.unshift(b.stmt(b.call('$.validate_snippet_args', b.id('$$payload'))));
1616
}
1717

18-
/** @type {ArrowFunctionExpression | CallExpression} */
19-
let fn = b.arrow([b.id('$$payload'), ...node.parameters], body);
18+
let fn = b.function_declaration(
19+
node.expression,
20+
[b.id('$$payload'), ...node.parameters],
21+
/** @type {BlockStatement} */ (context.visit(node.body))
22+
);
23+
24+
const push_to = node.metadata.can_hoist ? context.state.hoisted : context.state.init;
2025

2126
if (dev) {
22-
fn = b.call('$.prevent_snippet_stringification', fn);
27+
push_to.push(b.stmt(b.call('$.prevent_snippet_stringification', fn.id)));
2328
}
2429

25-
const declaration = b.declaration('const', [b.declarator(node.expression, fn)]);
26-
2730
// @ts-expect-error - TODO remove this hack once $$render_inner for legacy bindings is gone
2831
fn.___snippet = true;
2932

3033
if (node.metadata.can_hoist) {
31-
context.state.hoisted.push(declaration);
34+
push_to.push(fn);
3235
} else {
33-
context.state.init.push(declaration);
36+
push_to.push(fn);
3437
}
3538
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
compileOptions: {
5+
dev: true
6+
}
7+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function fn(snippet) {
2+
return snippet;
3+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script>
2+
import { fn } from "./fn.js";
3+
let variable = $state("var");
4+
5+
fn(test);
6+
</script>
7+
8+
{#snippet test()}
9+
{variable}
10+
{/snippet}

svelte-renderer-api

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 97817a95d6fa2d3f72c03f04a1e0882a7650cd16

0 commit comments

Comments
 (0)