Skip to content

Commit 3d5eda0

Browse files
committed
DRY out
1 parent 531581d commit 3d5eda0

File tree

1 file changed

+13
-18
lines changed
  • packages/editor/src/lib/compile-worker

1 file changed

+13
-18
lines changed

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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,21 @@ addEventListener('message', async (event) => {
4040
}
4141
}
4242
}
43+
4344
const { version } =
4445
package_json ?? (await fetch(`${svelte_url}/package.json`).then((r) => r.json()));
45-
if (version.startsWith('4.')) {
46-
// unpkg doesn't set the correct MIME type for .cjs files
47-
// https://github.com/mjackson/unpkg/issues/355
48-
const compiler =
49-
local_files?.find((file) => file.name === 'package/compiler.cjs')?.text ??
50-
(await fetch(`${svelte_url}/compiler.cjs`).then((r) => r.text()));
51-
(0, eval)(compiler + '\n//# sourceURL=compiler.cjs@' + version);
52-
} else if (version.startsWith('3.')) {
53-
const compiler =
54-
local_files?.find((file) => file.name === 'package/compiler.js')?.text ??
55-
(await fetch(`${svelte_url}/compiler.js`).then((r) => r.text()));
56-
(0, eval)(compiler + '\n//# sourceURL=compiler.js@' + version);
57-
} else {
58-
const compiler =
59-
local_files?.find((file) => file.name === 'package/compiler/index.js')?.text ??
60-
(await fetch(`${svelte_url}/compiler/index.js`).then((r) => r.text()));
61-
(0, eval)(compiler + '\n//# sourceURL=compiler/index.js@' + version);
62-
}
46+
47+
const compiler_file = version.startsWith('3.')
48+
? 'compiler.js'
49+
: version.startsWith('4.')
50+
? 'compiler.cjs'
51+
: 'compiler/index.js';
52+
53+
const compiler_text = local_files
54+
? local_files.find((file) => file.name === `package/${compiler_file}`)!.text
55+
: await fetch(`${svelte_url}/${compiler_file}`).then((r) => r.text());
56+
57+
(0, eval)(compiler_text + `\n//# sourceURL=${compiler_file}@` + version);
6358

6459
fulfil_ready();
6560
}

0 commit comments

Comments
 (0)