@@ -18,6 +18,7 @@ import type { Warning } from '../../types';
18
18
import type { CompileError , CompileOptions , CompileResult } from 'svelte/compiler' ;
19
19
import type { File } from 'editor' ;
20
20
import { parseTar , type FileDescription } from 'tarparser' ;
21
+ import { max } from './semver' ;
21
22
22
23
// hack for magic-string and rollup inline sourcemaps
23
24
// do not put this into a separate module and import it, would be treeshaken in prod
@@ -230,6 +231,8 @@ async function resolve_from_pkg(
230
231
return subpath ;
231
232
}
232
233
234
+ const versions = Object . create ( null ) ;
235
+
233
236
async function get_bundle (
234
237
uid : number ,
235
238
mode : 'client' | 'server' ,
@@ -288,10 +291,46 @@ async function get_bundle(
288
291
}
289
292
290
293
const pkg_name = match [ 1 ] ;
294
+
295
+ let default_version = 'latest' ;
296
+
297
+ if ( importer ?. startsWith ( packages_url ) ) {
298
+ const path = importer . slice ( packages_url . length + 1 ) ;
299
+ const parts = path . split ( '/' ) . slice ( 0 , 2 ) ;
300
+ if ( ! parts [ 0 ] . startsWith ( '@' ) ) parts . pop ( ) ;
301
+
302
+ const importer_name_and_version = parts . join ( '/' ) ;
303
+ const importer_name = importer_name_and_version . slice (
304
+ 0 ,
305
+ importer_name_and_version . indexOf ( '@' , 1 )
306
+ ) ;
307
+
308
+ const default_versions = ( versions [ importer_name_and_version ] ??= Object . create ( null ) ) ;
309
+
310
+ if ( ! default_versions [ pkg_name ] ) {
311
+ const pkg_json_url = `${ packages_url } /${ importer_name_and_version } /package.json` ;
312
+ const pkg_json = ( await fetch_if_uncached ( pkg_json_url , uid ) ) ?. body ;
313
+ const pkg = JSON . parse ( pkg_json ?? '""' ) ;
314
+
315
+ if ( importer_name === pkg_name ) {
316
+ default_versions [ pkg_name ] = pkg . version ;
317
+ } else {
318
+ const version =
319
+ pkg . devDependencies ?. [ pkg_name ] ??
320
+ pkg . peerDependencies ?. [ pkg_name ] ??
321
+ pkg . dependencies ?. [ pkg_name ] ;
322
+
323
+ default_versions [ pkg_name ] = max ( version ) ;
324
+ }
325
+ }
326
+
327
+ default_version = default_versions [ pkg_name ] ;
328
+ }
329
+
291
330
const pkg_url =
292
331
pkg_name === 'svelte'
293
332
? `${ svelte_url } /package.json`
294
- : `${ packages_url } /${ pkg_name } @${ match [ 2 ] ?? 'latest' } /package.json` ;
333
+ : `${ packages_url } /${ pkg_name } @${ match [ 2 ] ?? default_version } /package.json` ;
295
334
const subpath = `.${ match [ 3 ] ?? '' } ` ;
296
335
297
336
// if this was imported by one of our files, add it to the `imports` set
0 commit comments