@@ -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,43 @@ 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 = parts . join ( '/' ) ;
303+
304+ const default_versions = ( versions [ importer_name ] ??= Object . create ( null ) ) ;
305+
306+ if ( ! default_versions [ pkg_name ] ) {
307+ const pkg_json_url = `${ packages_url } /${ importer_name } /package.json` ;
308+ const pkg_json = ( await fetch_if_uncached ( pkg_json_url , uid ) ) ?. body ;
309+ const pkg = JSON . parse ( pkg_json ?? '""' ) ;
310+
311+ const version =
312+ pkg . devDependencies ?. [ pkg_name ] ??
313+ pkg . peerDependencies ?. [ pkg_name ] ??
314+ pkg . dependencies ?. [ pkg_name ] ;
315+
316+ default_versions [ pkg_name ] = max ( version ) ;
317+ console . log ( {
318+ importee,
319+ importer : parts . join ( '/' ) ,
320+ default_version : default_versions [ pkg_name ]
321+ } ) ;
322+ }
323+
324+ default_version = default_versions [ pkg_name ] ;
325+ }
326+
291327 const pkg_url =
292328 pkg_name === 'svelte'
293329 ? `${ svelte_url } /package.json`
294- : `${ packages_url } /${ pkg_name } @${ match [ 2 ] ?? 'latest' } /package.json` ;
330+ : `${ packages_url } /${ pkg_name } @${ match [ 2 ] ?? default_version } /package.json` ;
295331 const subpath = `.${ match [ 3 ] ?? '' } ` ;
296332
297333 // if this was imported by one of our files, add it to the `imports` set
0 commit comments