Skip to content

Commit 8f4c6d0

Browse files
committed
feat: add templating mode selector
1 parent 2734659 commit 8f4c6d0

File tree

6 files changed

+43
-8
lines changed

6 files changed

+43
-8
lines changed

packages/editor/src/lib/Workspace.svelte.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export interface ExposedCompilerOptions {
9494
generate: 'client' | 'server';
9595
dev: boolean;
9696
modernAst: boolean;
97+
templatingMode: 'string' | 'functional';
9798
}
9899

99100
export class Workspace {
@@ -104,7 +105,8 @@ export class Workspace {
104105
#compiler_options = $state.raw<ExposedCompilerOptions>({
105106
generate: 'client',
106107
dev: false,
107-
modernAst: true
108+
modernAst: true,
109+
templatingMode: 'string'
108110
});
109111
compiled = $state<Record<string, Compiled>>({});
110112

@@ -453,6 +455,11 @@ export class Workspace {
453455
update_compiler_options(options: Partial<ExposedCompilerOptions>) {
454456
this.#compiler_options = { ...this.#compiler_options, ...options };
455457
this.#reset_diagnostics();
458+
for (let file of this.#files) {
459+
if (is_file(file)) {
460+
this.#onupdate(file);
461+
}
462+
}
456463
}
457464

458465
update_file(file: File) {

packages/editor/src/lib/compile-worker/worker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ addEventListener('message', async (event) => {
116116
: 'ssr'
117117
: options.generate,
118118
dev: options.dev,
119-
filename: file.name
119+
filename: file.name,
120+
templatingMode: options.templatingMode
120121
};
121122

122123
if (!is_svelte_3_or_4) {

packages/repl/src/lib/Output/CompilerOptions.svelte

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@
2424
{/each},
2525
</div>
2626

27+
<div class="option">
28+
<span class="key">templatingMode:</span>
29+
30+
{#each ['string', 'functional'] as const as templating_mode}
31+
<input
32+
id={templating_mode}
33+
type="radio"
34+
checked={workspace.compiler_options.templatingMode === templating_mode}
35+
value={templating_mode}
36+
onchange={() => {
37+
workspace.update_compiler_options({ templatingMode: templating_mode });
38+
}}
39+
/>
40+
<label for={templating_mode}><span class="string">"{templating_mode}"</span></label>
41+
{/each},
42+
</div>
43+
2744
<!-- svelte-ignore a11y_label_has_associated_control (TODO this warning should probably be disabled if there's a component)-->
2845
<label class="option">
2946
<span class="key">dev:</span>
@@ -55,7 +72,7 @@
5572
5673
.key {
5774
display: inline-block;
58-
width: 6em;
75+
width: 10em;
5976
}
6077
6178
.string {

packages/repl/src/lib/Output/Viewer.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
$: if (ready) proxy?.iframe_command('set_theme', { theme });
102102
103103
async function apply_bundle($bundle: Bundle | null | undefined) {
104+
console.log({ $bundle });
104105
if (!$bundle) return;
105106
106107
try {

packages/repl/src/lib/Repl.svelte

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@
9595
9696
async function rebundle() {
9797
const token = (current_token = Symbol());
98-
const result = await bundler!.bundle(workspace.files as File[]);
98+
const result = await bundler!.bundle(workspace.files as File[], {
99+
// @ts-ignore
100+
templatingMode: workspace.compiler_options.templatingMode
101+
});
99102
if (token === current_token) $bundle = result as Bundle;
100103
}
101104

packages/repl/src/lib/workers/bundler/index.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ self.addEventListener('message', async (event: MessageEvent<BundleMessageData>)
140140

141141
let cached: Record<
142142
'client' | 'server',
143-
Map<string, { code: string; result: ReturnType<typeof svelte.compile> }>
143+
Map<string, { code: string; result: ReturnType<typeof svelte.compile>; templatingMode: string }>
144144
> = {
145145
client: new Map(),
146146
server: new Map()
@@ -419,14 +419,20 @@ async function get_bundle(
419419
const cached_id = cache.get(id);
420420
let result: CompileResult;
421421

422-
if (cached_id && cached_id.code === code) {
422+
if (
423+
cached_id &&
424+
cached_id.code === code &&
425+
cached_id.templatingMode === (options as any).templatingMode
426+
) {
423427
result = cached_id.result;
424428
} else if (id.endsWith('.svelte')) {
425429
const compilerOptions: any = {
426430
...options,
427431
filename: name + '.svelte',
428432
generate: Number(svelte.VERSION.split('.')[0]) >= 5 ? 'client' : 'dom',
429-
dev: true
433+
dev: true,
434+
// @ts-expect-error
435+
templatingMode: options.templatingMode
430436
};
431437

432438
if (can_use_experimental_async) {
@@ -482,7 +488,7 @@ async function get_bundle(
482488
return null;
483489
}
484490

485-
new_cache.set(id, { code, result });
491+
new_cache.set(id, { code, result, templatingMode: (options as any).templatingMode });
486492

487493
// @ts-expect-error
488494
(result.warnings || result.stats?.warnings)?.forEach((warning) => {

0 commit comments

Comments
 (0)