Skip to content

Commit a788735

Browse files
committed
simplify with regex
1 parent 91d6e7f commit a788735

File tree

1 file changed

+18
-22
lines changed
  • packages/repl/src/lib/workers/bundler

1 file changed

+18
-22
lines changed

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

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type { BundleMessageData } from '../workers';
1616
import type { Warning } from '../../types';
1717
import type { CompileError, CompileOptions, CompileResult } from 'svelte/compiler';
1818
import type { File } from 'editor';
19-
import { parseTar } from 'tarparser';
19+
import { parseTar, type FileDescription } from 'tarparser';
2020

2121
// hack for magic-string and rollup inline sourcemaps
2222
// do not put this into a separate module and import it, would be treeshaken in prod
@@ -41,38 +41,34 @@ self.addEventListener('message', async (event: MessageEvent<BundleMessageData>)
4141
switch (event.data.type) {
4242
case 'init': {
4343
({ packages_url, svelte_url } = event.data);
44+
const match = /^(pr|commit)-(.+)/.exec(svelte_url);
4445

45-
const starts_with_pr = svelte_url.startsWith('pr-');
46-
const starts_with_commit = svelte_url.startsWith('commit-');
46+
if (match) {
47+
let local_files: FileDescription[];
4748

48-
if (starts_with_pr || starts_with_commit) {
49-
let local_files: Awaited<ReturnType<typeof parseTar>>;
50-
51-
const ref = starts_with_pr
52-
? svelte_url.substring('pr-'.length)
53-
: svelte_url.substring('commit-'.length);
5449
try {
55-
const maybe_tar = await fetch(`https://pkg.pr.new/svelte@${ref}`);
56-
if (!maybe_tar.ok)
50+
const response = await fetch(`https://pkg.pr.new/svelte@${match[2]}`);
51+
52+
if (!response.ok) {
5753
throw new Error(
58-
`impossible to fetch the compiler from this ${starts_with_pr ? 'PR' : 'commit'}`
54+
`impossible to fetch the compiler from this ${match[1] === 'pr' ? 'PR' : 'commit'}`
5955
);
60-
if (maybe_tar.headers.get('content-type') === 'application/tar+gzip') {
61-
const buffer = await maybe_tar.arrayBuffer();
62-
local_files = await parseTar(buffer);
63-
files = new Map(
64-
local_files.map((file) => [file.name.substring('package'.length), () => file.text])
65-
);
66-
const package_json_content = files.get('/package.json')?.();
67-
if (package_json_content) {
68-
package_json = JSON.parse(package_json_content);
69-
}
56+
}
57+
58+
local_files = await parseTar(await response.arrayBuffer());
59+
files = new Map(
60+
local_files.map((file) => [file.name.substring('package'.length), () => file.text])
61+
);
62+
const package_json_content = files.get('/package.json')?.();
63+
if (package_json_content) {
64+
package_json = JSON.parse(package_json_content);
7065
}
7166
} catch (e) {
7267
reject_ready(e as Error);
7368
return;
7469
}
7570
}
71+
7672
({ version } =
7773
package_json ?? (await fetch(`${svelte_url}/package.json`).then((r) => r.json())));
7874
console.log(`Using Svelte compiler version ${version}`);

0 commit comments

Comments
 (0)