@@ -18,6 +18,7 @@ import type { Warning } from '../../types';
1818import type { CompileError , CompileOptions , CompileResult } from 'svelte/compiler' ;
1919import type { File } from 'editor' ;
2020import { parseTar , type FileDescription } from 'tarparser' ;
21+ import { max } from './semver' ;
2122
2223// hack for magic-string and rollup inline sourcemaps
2324// 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(
230231 return subpath ;
231232}
232233
234+ const versions = Object . create ( null ) ;
235+
233236async function get_bundle (
234237 uid : number ,
235238 mode : 'client' | 'server' ,
@@ -288,10 +291,46 @@ async function get_bundle(
288291 }
289292
290293 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+
291330 const pkg_url =
292331 pkg_name === 'svelte'
293332 ? `${ 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` ;
295334 const subpath = `.${ match [ 3 ] ?? '' } ` ;
296335
297336 // if this was imported by one of our files, add it to the `imports` set
0 commit comments