Skip to content

Commit a6fdc47

Browse files
authored
chore: show ast on playground (#9440)
The modern one
1 parent 66661ef commit a6fdc47

File tree

4 files changed

+9
-5
lines changed

4 files changed

+9
-5
lines changed

sites/svelte-5-preview/src/lib/Output/Output.svelte

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@
5858
let view = 'result';
5959
let markdown = '';
6060
61-
/** @type {import('svelte/types/compiler/interfaces').Ast} */
62-
let ast;
61+
$: ast = compiled?.result?.ast;
6362
</script>
6463
6564
<div class="view-toggle">
@@ -110,7 +109,7 @@
110109
</div>
111110
112111
<!-- ast output -->
113-
{#if showAst}
112+
{#if showAst && ast}
114113
<div class="tab-content" class:visible={selected?.type !== 'md' && view === 'ast'}>
115114
<!-- ast view interacts with the module editor, wait for it first -->
116115
{#if $module_editor}

sites/svelte-5-preview/src/lib/Repl.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@
254254
if (!compiler || !$selected) return;
255255
256256
if ($selected.type === 'svelte' || $selected.type === 'js') {
257-
compiled = await compiler.compile($selected, $compile_options, false);
257+
compiled = await compiler.compile($selected, $compile_options, true);
258258
runes = compiled.result.metadata?.runes ?? false;
259259
} else {
260260
runes = false;

sites/svelte-5-preview/src/lib/workers/compiler/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ self.window = self; //TODO: still need?: egregious hack to get magic-string to w
33

44
/**
55
* @type {{
6+
* parse: typeof import('svelte/compiler').parse;
67
* compile: typeof import('svelte/compiler').compile;
78
* compileModule: typeof import('svelte/compiler').compileModule;
89
* VERSION: string;
@@ -65,14 +66,17 @@ function compile({ id, source, options, return_ast }) {
6566

6667
const { js, css, warnings, metadata } = compiled;
6768

69+
const ast = return_ast ? svelte.parse(source, { modern: true }) : undefined;
70+
6871
return {
6972
id,
7073
result: {
7174
js: js.code,
7275
css: css?.code || `/* Add a <sty` + `le> tag to see compiled CSS */`,
7376
error: null,
7477
warnings,
75-
metadata
78+
metadata,
79+
ast
7680
}
7781
};
7882
} else if (options.filename.endsWith('.svelte.js')) {

sites/svelte-5-preview/src/routes/+page.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,5 @@
9292
on:change={change_from_editor}
9393
on:remove={change_from_editor}
9494
previewTheme={$theme.current}
95+
showAst={true}
9596
/>

0 commit comments

Comments
 (0)