Skip to content

Commit 7b7a763

Browse files
committed
simplify a whole bunch of stuff
1 parent 51394a4 commit 7b7a763

File tree

5 files changed

+42
-101
lines changed

5 files changed

+42
-101
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ export default class Compiler {
4040

4141
/**
4242
* @param {import('$lib/types').File} file
43-
* @param {import('svelte/types/compiler').CompileOptions} options
43+
* @param {import('svelte/compiler').CompileOptions} options
4444
* @param {boolean} return_ast
45-
* @returns
45+
* @returns {Promise<import('$lib/workers/workers').CompileMessageData>}
4646
*/
4747
compile(file, options, return_ast) {
4848
return new Promise((fulfil) => {

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

Lines changed: 20 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
<script>
22
import { get_repl_context } from '$lib/context.js';
3-
import { BROWSER } from 'esm-env';
43
import { marked } from 'marked';
54
import CodeMirror from '../CodeMirror.svelte';
65
import AstView from './AstView.svelte';
7-
import Compiler from './Compiler.js';
86
import CompilerOptions from './CompilerOptions.svelte';
97
import PaneWithPanel from './PaneWithPanel.svelte';
108
import Viewer from './Viewer.svelte';
119
12-
export let svelteUrl;
13-
1410
/** @type {string | null} */
1511
export let status;
1612
@@ -35,62 +31,29 @@
3531
/** @type {'light' | 'dark'} */
3632
export let previewTheme;
3733
38-
/**
39-
* @param {import('$lib/types').File} file
40-
* @param {import('svelte/compiler').CompileOptions} options
41-
*/
42-
export async function set(file, options) {
43-
selected_type = file.type;
34+
/** @type {import('../types').File | null} */
35+
export let selected;
36+
37+
/** @type {import('../workers/workers').CompileMessageData | null} */
38+
export let compiled;
4439
45-
if (file.type === 'json') {
40+
$: if (selected) {
41+
if (selected.type === 'json') {
4642
js_editor.set({ code: `/* Select a component to see its compiled code */`, lang: 'js' });
4743
css_editor.set({ code: `/* Select a component to see its compiled code */`, lang: 'css' });
48-
return;
49-
}
50-
51-
if (file.type === 'md') {
52-
markdown = marked(file.source);
53-
return;
5444
}
5545
56-
if (!compiler) return console.error('Compiler not initialized.');
57-
58-
const compiled = await compiler.compile(file, options, showAst);
59-
if (!js_editor) return; // unmounted
60-
61-
js_editor.set({
62-
code: compiled.js,
63-
lang: 'js'
64-
});
65-
css_editor.set({ code: compiled.css, lang: 'css' });
66-
ast = compiled.ast;
67-
}
68-
69-
/**
70-
* @param {import('$lib/types').File} selected
71-
* @param {import('svelte/compiler').CompileOptions} options
72-
*/
73-
export async function update(selected, options) {
74-
if (selected.type === 'json') return;
75-
76-
if (selected.type === 'md') {
46+
else if (selected.type=== 'md') {
7747
markdown = marked(selected.source);
78-
return;
7948
}
8049
81-
if (!compiler) return console.error('Compiler not initialized.');
82-
83-
const { result, metadata } = await compiler.compile(selected, options, showAst);
84-
85-
js_editor.update({ code: result.js, lang: 'js' });
86-
css_editor.update({ code: result.css, lang: 'css' });
87-
$runes_mode = metadata?.runes;
88-
ast = result.ast;
50+
else if (compiled) {
51+
js_editor.set({ code: compiled.result.js, lang: 'js' });
52+
css_editor.set({ code: compiled.result.css, lang: 'css' });
53+
}
8954
}
9055
91-
const { module_editor, runes_mode } = get_repl_context();
92-
93-
const compiler = BROWSER ? new Compiler(svelteUrl) : null;
56+
const { module_editor } = get_repl_context();
9457
9558
/** @type {CodeMirror} */
9659
let js_editor;
@@ -100,15 +63,14 @@
10063
10164
/** @type {'result' | 'js' | 'css' | 'ast'} */
10265
let view = 'result';
103-
let selected_type = '';
10466
let markdown = '';
10567
10668
/** @type {import('svelte/types/compiler/interfaces').Ast} */
10769
let ast;
10870
</script>
10971

11072
<div class="view-toggle">
111-
{#if selected_type === 'md'}
73+
{#if selected?.type === 'md'}
11274
<button class="active">Markdown</button>
11375
{:else}
11476
<button class:active={view === 'result'} on:click={() => (view = 'result')}>Result</button>
@@ -121,7 +83,7 @@
12183
</div>
12284

12385
<!-- component viewer -->
124-
<div class="tab-content" class:visible={selected_type !== 'md' && view === 'result'}>
86+
<div class="tab-content" class:visible={selected?.type !== 'md' && view === 'result'}>
12587
<Viewer
12688
bind:error={runtimeError}
12789
{status}
@@ -133,7 +95,7 @@
13395
</div>
13496

13597
<!-- js output -->
136-
<div class="tab-content" class:visible={selected_type !== 'md' && view === 'js'}>
98+
<div class="tab-content" class:visible={selected?.type !== 'md' && view === 'js'}>
13799
{#if embedded}
138100
<CodeMirror bind:this={js_editor} errorLoc={sourceErrorLoc} readonly />
139101
{:else}
@@ -150,22 +112,22 @@
150112
</div>
151113

152114
<!-- css output -->
153-
<div class="tab-content" class:visible={selected_type !== 'md' && view === 'css'}>
115+
<div class="tab-content" class:visible={selected?.type !== 'md' && view === 'css'}>
154116
<CodeMirror bind:this={css_editor} errorLoc={sourceErrorLoc} readonly />
155117
</div>
156118

157119
<!-- ast output -->
158120
{#if showAst}
159-
<div class="tab-content" class:visible={selected_type !== 'md' && view === 'ast'}>
121+
<div class="tab-content" class:visible={selected?.type !== 'md' && view === 'ast'}>
160122
<!-- ast view interacts with the module editor, wait for it first -->
161123
{#if $module_editor}
162-
<AstView {ast} autoscroll={selected_type !== 'md' && view === 'ast'} />
124+
<AstView {ast} autoscroll={selected?.type !== 'md' && view === 'ast'} />
163125
{/if}
164126
</div>
165127
{/if}
166128

167129
<!-- markdown output -->
168-
<div class="tab-content" class:visible={selected_type === 'md'}>
130+
<div class="tab-content" class:visible={selected?.type === 'md'}>
169131
<iframe title="Markdown" srcdoc={markdown} />
170132
</div>
171133

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

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import Output from './Output/Output.svelte';
1212
import { set_repl_context } from './context.js';
1313
import { get_full_filename } from './utils.js';
14+
import Compiler from './Output/Compiler.js';
1415
1516
export let packagesUrl = 'https://unpkg.com';
1617
export let svelteUrl = `${BROWSER ? location.origin : ''}/svelte`;
@@ -64,32 +65,6 @@
6465
$files = $files.map((val) => ({ ...val, modified: false }));
6566
}
6667
67-
/** @param {{ files: import('./types').File[], css?: string }} data */
68-
export function update(data) {
69-
$files = data.files;
70-
71-
const matched_file = data.files.find((file) => get_full_filename(file) === $selected_name);
72-
73-
$selected_name = matched_file ? get_full_filename(matched_file) : 'App.svelte';
74-
75-
injectedCSS = data.css ?? '';
76-
77-
if (matched_file) {
78-
$module_editor?.update({
79-
code: matched_file.source,
80-
lang: matched_file.type
81-
});
82-
83-
$output?.update?.(matched_file, $compile_options);
84-
85-
$module_editor?.clearEditorState();
86-
}
87-
88-
populate_editor_state();
89-
90-
dispatch('change', { files: $files });
91-
}
92-
9368
/** @type {ReturnType<typeof createEventDispatcher<{ change: { files: import('./types').File[] } }>>} */
9469
const dispatch = createEventDispatcher();
9570
@@ -135,9 +110,6 @@
135110
/** @type {ReplContext['module_editor']} */
136111
const module_editor = writable(null);
137112
138-
/** @type {ReplContext['output']} */
139-
const output = writable(null);
140-
141113
/** @type {ReplContext['toggleable']} */
142114
const toggleable = writable(false);
143115
@@ -160,7 +132,6 @@
160132
compile_options,
161133
cursor_pos,
162134
module_editor,
163-
output,
164135
toggleable,
165136
runes_mode,
166137
@@ -206,8 +177,6 @@
206177
$module_editor?.clearEditorState();
207178
}
208179
209-
$output?.set($selected, $compile_options);
210-
211180
is_select_changing = false;
212181
}
213182
@@ -273,8 +242,18 @@
273242
}
274243
}
275244
276-
$: if ($output && $selected) {
277-
$output?.update?.($selected, $compile_options);
245+
const compiler = BROWSER ? new Compiler(svelteUrl) : null;
246+
247+
/** @type {import('./workers/workers').CompileMessageData | null} */
248+
let compiled = null;
249+
250+
$: if (compiler && $selected) {
251+
if ($selected.type === 'svelte' || $selected.type === 'js') {
252+
compiler.compile($selected, $compile_options, false).then((data) => {
253+
compiled = data;
254+
$runes_mode = data.metadata?.runes ?? false;
255+
});
256+
}
278257
}
279258
280259
$: mobile = width < 540;
@@ -347,15 +326,15 @@
347326
348327
<section slot="b" style="height: 100%;">
349328
<Output
350-
bind:this={$output}
351-
{svelteUrl}
352329
status={status_visible ? status : null}
353330
{embedded}
354331
{relaxed}
355332
{injectedJS}
356333
{injectedCSS}
357334
{showAst}
358335
{previewTheme}
336+
selected={$selected}
337+
{compiled}
359338
/>
360339
</section>
361340
</SplitPane>

sites/svelte-5-preview/src/lib/types.d.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ export type ReplState = {
4747
cursor_pos: number;
4848
toggleable: boolean;
4949
module_editor: import('./CodeMirror.svelte').default | null;
50-
output: import('./Output/Output.svelte').default | null;
5150
runes_mode: boolean;
5251
};
5352

@@ -62,7 +61,6 @@ export type ReplContext = {
6261
cursor_pos: Writable<ReplState['cursor_pos']>;
6362
toggleable: Writable<ReplState['toggleable']>;
6463
module_editor: Writable<ReplState['module_editor']>;
65-
output: Writable<ReplState['output']>;
6664
runes_mode: Writable<ReplState['runes_mode']>;
6765

6866
EDITOR_STATE_MAP: Map<string, EditorState>;

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,14 @@ function compile({ id, source, options, return_ast }) {
6262
generate: options.generate
6363
});
6464

65-
const { js, css, metadata } = compiled;
65+
const { js, css, warnings, metadata } = compiled;
6666

6767
return {
6868
id,
6969
result: {
7070
js: js.code,
71-
css: css?.code || `/* Add a <sty` + `le> tag to see compiled CSS */`
71+
css: css?.code || `/* Add a <sty` + `le> tag to see compiled CSS */`,
72+
warnings
7273
},
7374
metadata
7475
};
@@ -83,7 +84,8 @@ function compile({ id, source, options, return_ast }) {
8384
id,
8485
result: {
8586
js: compiled.js.code,
86-
css
87+
css,
88+
warnings: compiled.warnings
8789
},
8890
metadata: compiled.metadata
8991
};

0 commit comments

Comments
 (0)