Skip to content

Commit 29f96e9

Browse files
committed
less reliance on globals
1 parent f1eafaa commit 29f96e9

File tree

2 files changed

+55
-44
lines changed

2 files changed

+55
-44
lines changed

packages/repl/src/lib/Bundler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ let uid = 1;
66

77
export default class Bundler {
88
#worker: Worker;
9-
#handlers = new Map<number, (data: BundleMessageData) => void>();
9+
#handlers = new Map<number, (data: BundleMessageData) => void>(); // TODO this is leaky, we don't always remove handlers
1010

1111
constructor({
1212
svelte_version,

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

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -37,54 +37,52 @@ const ENTRYPOINT = '__entry.js';
3737
const STYLES = '__styles.js';
3838
const ESM_ENV = '__esm-env.js';
3939

40-
let svelte_version: string;
4140
let current_id: number;
4241

43-
let inited = Promise.withResolvers<void>();
44-
45-
let can_use_experimental_async = false;
46-
47-
async function init(v: string, uid: number) {
48-
({ version: svelte_version, can_use_experimental_async } = await load_svelte(v, (version) => {
49-
self.postMessage({
50-
type: 'version',
51-
uid,
52-
message: version
53-
});
54-
}));
55-
}
42+
let ready: ReturnType<typeof load_svelte>;
5643

5744
self.addEventListener('message', async (event: MessageEvent<BundleMessageData>) => {
5845
switch (event.data.type) {
5946
case 'init': {
60-
init(event.data.svelte_version, event.data.uid).then(inited.resolve, inited.reject);
47+
ready = load_svelte(event.data.svelte_version, (version) => {
48+
self.postMessage({
49+
type: 'version',
50+
message: version
51+
});
52+
});
53+
6154
break;
6255
}
6356

6457
case 'bundle': {
6558
try {
66-
await inited.promise;
59+
const { svelte, version: svelte_version, can_use_experimental_async } = await ready;
60+
const { uid, files, options } = event.data;
61+
62+
current_id = uid;
63+
64+
setTimeout(async () => {
65+
if (current_id !== uid) return;
66+
67+
const result = await bundle(
68+
svelte,
69+
svelte_version,
70+
uid,
71+
files,
72+
options,
73+
can_use_experimental_async
74+
);
75+
76+
if (JSON.stringify(result.error) === JSON.stringify(ABORT)) return;
77+
if (result && uid === current_id) postMessage(result);
78+
});
6779
} catch (e) {
6880
self.postMessage({
6981
type: 'error',
7082
uid: event.data.uid,
7183
message: `Error loading the compiler: ${(e as Error).message}`
7284
});
7385
}
74-
const { uid, files, options } = event.data;
75-
76-
if (files.length === 0) return;
77-
78-
current_id = uid;
79-
80-
setTimeout(async () => {
81-
if (current_id !== uid) return;
82-
83-
const result = await bundle({ uid, files, options });
84-
85-
if (JSON.stringify(result.error) === JSON.stringify(ABORT)) return;
86-
if (result && uid === current_id) postMessage(result);
87-
});
8886

8987
break;
9088
}
@@ -128,10 +126,13 @@ async function init_tailwind() {
128126
}
129127

130128
async function get_bundle(
129+
svelte: typeof import('svelte/compiler'),
130+
svelte_version: string,
131131
uid: number,
132132
mode: 'client' | 'server',
133133
virtual: Map<string, File>,
134-
options: BundleOptions
134+
options: BundleOptions,
135+
can_use_experimental_async: boolean
135136
) {
136137
let bundle;
137138

@@ -268,7 +269,7 @@ async function get_bundle(
268269
transform(code, id) {
269270
if (uid !== current_id) throw ABORT;
270271

271-
self.postMessage({ type: 'status', uid, message: `bundling ${id}` });
272+
self.postMessage({ type: 'status', message: `bundling ${id}` });
272273

273274
if (!/\.(svelte|js)$/.test(id)) return null;
274275

@@ -430,15 +431,14 @@ async function get_bundle(
430431

431432
export type BundleResult = ReturnType<typeof bundle>;
432433

433-
async function bundle({
434-
uid,
435-
files,
436-
options
437-
}: {
438-
uid: number;
439-
files: File[];
440-
options: BundleOptions;
441-
}) {
434+
async function bundle(
435+
svelte: typeof import('svelte/compiler'),
436+
svelte_version: string,
437+
uid: number,
438+
files: File[],
439+
options: BundleOptions,
440+
can_use_experimental_async: boolean
441+
) {
442442
if (!DEV) {
443443
console.clear();
444444
console.log(`running Svelte compiler version %c${svelte.VERSION}`, 'font-weight: bold');
@@ -505,10 +505,13 @@ async function bundle({
505505
});
506506

507507
let client: Awaited<ReturnType<typeof get_bundle>> = await get_bundle(
508+
svelte,
509+
svelte_version,
508510
uid,
509511
'client',
510512
lookup,
511-
options
513+
options,
514+
can_use_experimental_async
512515
);
513516

514517
try {
@@ -526,7 +529,15 @@ async function bundle({
526529
)?.output[0];
527530

528531
const server = false // TODO how can we do SSR?
529-
? await get_bundle(uid, 'server', lookup, options)
532+
? await get_bundle(
533+
svelte,
534+
svelte_version,
535+
uid,
536+
'server',
537+
lookup,
538+
options,
539+
can_use_experimental_async
540+
)
530541
: null;
531542

532543
if (server?.error) {

0 commit comments

Comments
 (0)