Skip to content

Commit e731458

Browse files
committed
get rid of $bundle store
1 parent ca89ae9 commit e731458

File tree

10 files changed

+63
-76
lines changed

10 files changed

+63
-76
lines changed

apps/svelte.dev/src/lib/tutorial/adapters/rollup/index.svelte.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,12 @@ import Bundler from '@sveltejs/repl/bundler';
33
import * as yootils from 'yootils';
44
import type { Adapter } from '$lib/tutorial';
55
import type { File, Item } from '@sveltejs/repl/workspace';
6-
import type { BundleResult } from '@sveltejs/repl';
76

8-
/** Rollup bundler singleton */
9-
let bundler: Bundler;
7+
let done = false;
108

119
export const state = new (class RollupState {
1210
progress = $state.raw({ value: 0, text: 'initialising' });
13-
bundle = $state.raw<BundleResult | null>(null);
14-
})();
15-
16-
/**
17-
* @returns {Promise<import('$lib/tutorial').Adapter>}
18-
*/
19-
export async function create(): Promise<Adapter> {
20-
state.progress = { value: 0, text: 'loading files' };
21-
22-
let done = false;
23-
24-
bundler ??= new Bundler({
11+
bundler = new Bundler({
2512
svelte_version: 'latest',
2613
onstatus(val) {
2714
if (!done && val === null) {
@@ -30,14 +17,19 @@ export async function create(): Promise<Adapter> {
3017
}
3118
}
3219
});
20+
})();
3321

22+
/**
23+
* @returns {Promise<import('$lib/tutorial').Adapter>}
24+
*/
25+
export async function create(): Promise<Adapter> {
3426
state.progress = { value: 0.5, text: 'loading svelte compiler' };
3527

3628
/** Paths and contents of the currently loaded file stubs */
3729
let current_files: Item[] = [];
3830

3931
async function compile() {
40-
state.bundle = await bundler.bundle(
32+
await state.bundler.bundle(
4133
current_files
4234
// TODO we can probably remove all the SvelteKit specific stuff from the tutorial content once this settles down
4335
.filter((f): f is File => f.name.startsWith('/src/lib/') && f.type === 'file')

apps/svelte.dev/src/routes/tutorial/[...slug]/OutputRollup.svelte

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
import Chrome from './Chrome.svelte';
77
import Loading from './Loading.svelte';
88
import { adapter_state, update } from './adapter.svelte';
9-
import { toStore } from 'svelte/store';
10-
11-
const bundle = toStore(() => adapter_state.bundle);
129
1310
let terminal_visible = $state(false);
1411
let logs = $state<Log[]>([]);
@@ -34,7 +31,7 @@
3431
relaxed
3532
can_escape
3633
onLog={(l: Log[]) => (logs = l)}
37-
{bundle}
34+
bundler={adapter_state.bundler}
3835
theme={theme.current}
3936
injectedCSS="@import '/tutorial/shared.css';"
4037
error={null}

apps/svelte.dev/src/routes/tutorial/[...slug]/adapter.svelte.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export const adapter_state = new (class {
2121
/** Logs from the web container instance. Irrelevant for Rollup */
2222
logs = $derived(wc_state.logs || []);
2323

24-
/** Result of a rollup compile. Irrelevant for web containers */
25-
bundle = $derived(rollup_state.bundle);
24+
/** Irrelevant for web containers */
25+
bundler = $derived(rollup_state.bundler);
2626

2727
/** Startup progress */
2828
progress = $derived(

packages/repl/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
"default": "./src/lib/index.ts"
3030
},
3131
"./bundler": {
32-
"types": "./src/lib/Bundler.ts",
33-
"default": "./src/lib/Bundler.ts"
32+
"types": "./src/lib/Bundler.svelte.ts",
33+
"default": "./src/lib/Bundler.svelte.ts"
3434
},
3535
"./editor": {
3636
"types": "./src/lib/Editor/Editor.svelte",
Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import type { BundleMessageData, BundleResult } from './workers/workers';
1+
import type { BundleResult } from './public';
22
import type { File } from './Workspace.svelte';
33

44
let uid = 1;
55

66
export default class Bundler {
77
#worker: Worker;
8-
#handlers = new Map<number, (data: BundleMessageData) => void>(); // TODO this is leaky, we don't always remove handlers
8+
9+
result = $state.raw<BundleResult | null>(null);
910

1011
constructor({
1112
svelte_version,
@@ -22,7 +23,7 @@ export default class Bundler {
2223
type: 'module'
2324
});
2425

25-
this.#worker.onmessage = (event: MessageEvent<BundleMessageData>) => {
26+
this.#worker.onmessage = (event) => {
2627
if (event.data.type === 'status') {
2728
onstatus(event.data.message);
2829
return;
@@ -39,28 +40,20 @@ export default class Bundler {
3940
}
4041

4142
onstatus(null);
42-
43-
const handler = this.#handlers.get(event.data.uid)!;
44-
this.#handlers.delete(event.data.uid);
45-
46-
handler(event.data);
43+
this.result = event.data;
4744
};
4845

4946
this.#worker.postMessage({ type: 'init', svelte_version });
5047
}
5148

52-
bundle(files: File[], options: { tailwind?: boolean }): Promise<BundleResult> {
53-
return new Promise<any>((fulfil) => {
54-
this.#handlers.set(uid, fulfil);
55-
56-
this.#worker.postMessage({
57-
uid,
58-
type: 'bundle',
59-
files,
60-
options
61-
});
62-
63-
uid += 1;
49+
bundle(files: File[], options: { tailwind?: boolean }) {
50+
this.#worker.postMessage({
51+
uid,
52+
type: 'bundle',
53+
files,
54+
options
6455
});
56+
57+
uid += 1;
6558
}
6659
}

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

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts">
22
import { get_repl_context } from '../context';
33
import { BROWSER } from 'esm-env';
4-
import { onMount } from 'svelte';
4+
import { onMount, untrack } from 'svelte';
55
import Message from '../Message.svelte';
66
import PaneWithPanel from './PaneWithPanel.svelte';
77
import ReplProxy from './ReplProxy.js';
@@ -11,8 +11,8 @@
1111
import srcdoc_styles from './srcdoc/styles.css?raw';
1212
import ErrorOverlay from './ErrorOverlay.svelte';
1313
import type { CompileError } from 'svelte/compiler';
14-
import type { Writable } from 'svelte/store';
1514
import type { BundleResult } from '$lib/public';
15+
import type Bundler from '$lib/Bundler.svelte';
1616
1717
interface Props {
1818
error: Error | null;
@@ -27,8 +27,8 @@
2727
/** Any additional CSS you may want to inject */
2828
injectedCSS?: string;
2929
theme: 'light' | 'dark';
30-
/** A store containing the current bundle result. Takes precedence over REPL context, if set */
31-
bundle?: Writable<BundleResult | null> | undefined;
30+
/** The current bundler. Takes precedence over REPL context, if set */
31+
bundler?: Bundler;
3232
/** Called everytime a log is pushed. If this is set, the built-in console coming with the Viewer isn't shown */
3333
onLog?: ((logs: Log[]) => void) | undefined;
3434
}
@@ -41,12 +41,12 @@
4141
injectedJS = '',
4242
injectedCSS = '',
4343
theme,
44-
bundle = $bindable(undefined),
44+
bundler,
4545
onLog = undefined
4646
}: Props = $props();
4747
48-
const context = get_repl_context();
49-
bundle ??= context.bundle;
48+
let context = get_repl_context();
49+
let bundle = $derived((bundler ?? context?.bundler)?.result);
5050
5151
let logs: Log[] = $state([]);
5252
let log_group_stack: Log[][] = [];
@@ -119,13 +119,13 @@
119119
}
120120
});
121121
122-
async function apply_bundle($bundle: BundleResult | null | undefined) {
123-
if (!$bundle) return;
122+
async function apply_bundle(bundle: BundleResult | null) {
123+
if (!bundle) return;
124124
125125
try {
126126
clear_logs();
127127
128-
if (!$bundle.error) {
128+
if (!bundle.error) {
129129
await proxy?.eval(
130130
`
131131
${injectedJS}
@@ -175,7 +175,7 @@
175175
window._svelteTransitionManager = null;
176176
}
177177
178-
const __repl_exports = ${$bundle.client?.code};
178+
const __repl_exports = ${bundle.client?.code};
179179
{
180180
const { mount, unmount, App, untrack } = __repl_exports;
181181
@@ -204,7 +204,7 @@
204204
}
205205
//# sourceURL=playground:output
206206
`,
207-
$bundle?.tailwind ?? srcdoc_styles
207+
bundle?.tailwind ?? srcdoc_styles
208208
);
209209
error = null;
210210
}
@@ -218,7 +218,12 @@
218218
219219
$effect(() => {
220220
if (ready) {
221-
apply_bundle($bundle);
221+
const b = bundle;
222+
223+
// TODO tidy up
224+
untrack(() => {
225+
apply_bundle(b);
226+
});
222227
}
223228
});
224229
@@ -235,7 +240,7 @@
235240
});
236241
237242
function show_error(e: CompileError & { loc: { line: number; column: number } }) {
238-
const map = $bundle?.client?.map;
243+
const map = bundle?.client?.map;
239244
240245
// @ts-ignore INVESTIGATE
241246
const loc = map && getLocationFromStack(e.stack, map);
@@ -316,8 +321,8 @@
316321
srcdoc={BROWSER ? srcdoc : ''}
317322
></iframe>
318323

319-
{#if $bundle?.error}
320-
<ErrorOverlay error={$bundle.error} />
324+
{#if bundle?.error}
325+
<ErrorOverlay error={bundle.error} />
321326
{/if}
322327
{/snippet}
323328

@@ -351,7 +356,7 @@
351356
<div class="overlay">
352357
{#if error}
353358
<Message kind="error" details={error} />
354-
{:else if status || !$bundle}
359+
{:else if status || !bundle}
355360
<Message kind="info" truncate>{status || 'loading Svelte compiler...'}</Message>
356361
{/if}
357362
</div>

packages/repl/src/lib/Repl.svelte

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { ScreenToggle } from '@sveltejs/site-kit/components';
44
import { BROWSER } from 'esm-env';
55
import { writable } from 'svelte/store';
6-
import Bundler from './Bundler.js';
6+
import Bundler from './Bundler.svelte.js';
77
import ComponentSelector from './Input/ComponentSelector.svelte';
88
import Output from './Output/Output.svelte';
99
import { set_repl_context } from './context.js';
@@ -67,7 +67,7 @@
6767
// TODO get rid
6868
export function toJSON() {
6969
return {
70-
imports: $bundle?.imports ?? [],
70+
imports: bundler.result?.imports ?? [],
7171
files: workspace.files,
7272
tailwind: workspace.tailwind
7373
};
@@ -83,18 +83,10 @@
8383
workspace.mark_saved();
8484
}
8585
86-
const bundle: ReplContext['bundle'] = writable(null);
8786
const toggleable: ReplContext['toggleable'] = writable(false);
8887
89-
set_repl_context({
90-
bundle,
91-
toggleable,
92-
workspace,
93-
svelteVersion
94-
});
95-
9688
async function rebundle() {
97-
$bundle = await bundler!.bundle(workspace.files as File[], {
89+
bundler!.bundle(workspace.files as File[], {
9890
tailwind: workspace.tailwind
9991
});
10092
}
@@ -143,6 +135,13 @@
143135
})
144136
: null;
145137
138+
set_repl_context({
139+
bundler,
140+
toggleable,
141+
workspace,
142+
svelteVersion
143+
});
144+
146145
function before_unload(event: BeforeUnloadEvent) {
147146
if (Object.keys(workspace.modified).length > 0) {
148147
event.preventDefault();

packages/repl/src/lib/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ export type File = {
3131

3232
export type ReplState = {
3333
bundle: BundleResult | null;
34-
bundler: import('./Bundler').default | null;
34+
bundler: import('./Bundler.svelte').default | null;
3535
toggleable: boolean;
3636
};
3737

3838
export type ReplContext = {
39-
bundle: Writable<ReplState['bundle']>;
39+
bundler: Bundler;
4040
toggleable: Writable<ReplState['toggleable']>;
4141
workspace: Workspace;
4242
svelteVersion: string;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import svg from './plugins/svg';
1212
import replace from './plugins/replace';
1313
import loop_protect from './plugins/loop-protect';
1414
import type { Plugin, RollupCache, TransformResult } from '@rollup/browser';
15-
import type { BundleMessageData, BundleOptions, BundleResult } from '../workers';
15+
import type { BundleMessageData, BundleOptions } from '../workers';
1616
import type { Warning } from '../../types';
1717
import type { CompileError, CompileResult } from 'svelte/compiler';
1818
import type { File } from '../../Workspace.svelte';
@@ -28,6 +28,7 @@ import {
2828
resolve_subpath,
2929
resolve_version
3030
} from '../npm';
31+
import type { BundleResult } from '$lib/public';
3132

3233
// hack for magic-string and rollup inline sourcemaps
3334
// do not put this into a separate module and import it, would be treeshaken in prod

packages/repl/src/routes/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script lang="ts">
2-
import { Repl } from '$lib/Repl.svelte';
2+
import Repl from '$lib/Repl.svelte';
33
import { onMount } from 'svelte';
44
import '@sveltejs/site-kit/styles/index.css';
55

0 commit comments

Comments
 (0)