@@ -16,7 +16,7 @@ import type { BundleMessageData } from '../workers';
16
16
import type { Warning } from '../../types' ;
17
17
import type { CompileError , CompileOptions , CompileResult } from 'svelte/compiler' ;
18
18
import type { File } from 'editor' ;
19
- import { parseTar } from 'tarparser' ;
19
+ import { parseTar , type FileDescription } from 'tarparser' ;
20
20
21
21
// hack for magic-string and rollup inline sourcemaps
22
22
// 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>)
41
41
switch ( event . data . type ) {
42
42
case 'init' : {
43
43
( { packages_url, svelte_url } = event . data ) ;
44
+ const match = / ^ ( p r | c o m m i t ) - ( .+ ) / . exec ( svelte_url ) ;
44
45
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 [ ] ;
47
48
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 ) ;
54
49
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 ) {
57
53
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' } `
59
55
) ;
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 ) ;
70
65
}
71
66
} catch ( e ) {
72
67
reject_ready ( e as Error ) ;
73
68
return ;
74
69
}
75
70
}
71
+
76
72
( { version } =
77
73
package_json ?? ( await fetch ( `${ svelte_url } /package.json` ) . then ( ( r ) => r . json ( ) ) ) ) ;
78
74
console . log ( `Using Svelte compiler version ${ version } ` ) ;
0 commit comments