@@ -128,10 +128,13 @@ export function image_plugin(imagetools_plugin) {
128128 // this must come after the await so that we don't hand off processing between getting
129129 // the imports.size and incrementing the imports.size
130130 const name = imports . get ( original_url ) || '__IMPORTED_ASSET_' + imports . size + '__' ;
131+ if ( ! metadata . width || ! metadata . height ) {
132+ console . warn ( `Could not determine intrinsic dimensions for ${ resolved_id } ` ) ;
133+ }
131134 const new_markup = `<img ${ serialize_img_attributes ( content , node . attributes , {
132135 src : `{${ name } }` ,
133- width : metadata . width || 0 ,
134- height : metadata . height || 0
136+ width : metadata . width ,
137+ height : metadata . height
135138 } ) } />`;
136139 s . update ( node . start , node . end , new_markup ) ;
137140 imports . set ( original_url , name ) ;
@@ -258,8 +261,8 @@ function get_attr_value(node, attr) {
258261 * @param {import('../types/internal.js').Attribute[] } attributes
259262 * @param {{
260263 * src: string,
261- * width: string | number,
262- * height: string | number
264+ * width? : string | number,
265+ * height? : string | number
263266 * }} details
264267 */
265268function serialize_img_attributes ( content , attributes , details ) {
@@ -283,21 +286,23 @@ function serialize_img_attributes(content, attributes, details) {
283286 }
284287 }
285288 }
286- if ( ! user_width && ! user_height ) {
287- attribute_strings . push ( `width=${ details . width } ` ) ;
288- attribute_strings . push ( `height=${ details . height } ` ) ;
289- } else if ( ! user_width && user_height ) {
290- attribute_strings . push (
291- `width=${ Math . round (
292- ( stringToNumber ( details . width ) * user_height ) / stringToNumber ( details . height )
293- ) } `
294- ) ;
295- } else if ( ! user_height && user_width ) {
296- attribute_strings . push (
297- `height=${ Math . round (
298- ( stringToNumber ( details . height ) * user_width ) / stringToNumber ( details . width )
299- ) } `
300- ) ;
289+ if ( details . width && details . height ) {
290+ if ( ! user_width && ! user_height ) {
291+ attribute_strings . push ( `width=${ details . width } ` ) ;
292+ attribute_strings . push ( `height=${ details . height } ` ) ;
293+ } else if ( ! user_width && user_height ) {
294+ attribute_strings . push (
295+ `width=${ Math . round (
296+ ( stringToNumber ( details . width ) * user_height ) / stringToNumber ( details . height )
297+ ) } `
298+ ) ;
299+ } else if ( ! user_height && user_width ) {
300+ attribute_strings . push (
301+ `height=${ Math . round (
302+ ( stringToNumber ( details . height ) * user_width ) / stringToNumber ( details . width )
303+ ) } `
304+ ) ;
305+ }
301306 }
302307
303308 return attribute_strings . join ( ' ' ) ;
@@ -358,29 +363,42 @@ function to_value(src) {
358363 */
359364function dynamic_img_to_picture ( content , node , src_var_name ) {
360365 const attributes = node . attributes ;
361- const index = attributes . findIndex (
362- ( attribute ) => 'name' in attribute && attribute . name === 'sizes'
363- ) ;
366+ /**
367+ * @param attribute_name {string}
368+ */
369+ function index ( attribute_name ) {
370+ return attributes . findIndex (
371+ ( attribute ) => 'name' in attribute && attribute . name === attribute_name
372+ ) ;
373+ }
374+ const size_index = index ( 'sizes' ) ;
375+ const width_index = index ( 'width' ) ;
376+ const height_index = index ( 'height' ) ;
364377 let sizes_string = '' ;
365- if ( index >= 0 ) {
366- sizes_string = ' ' + content . substring ( attributes [ index ] . start , attributes [ index ] . end ) ;
367- attributes . splice ( index , 1 ) ;
378+ if ( size_index >= 0 ) {
379+ sizes_string =
380+ ' ' + content . substring ( attributes [ size_index ] . start , attributes [ size_index ] . end ) ;
381+ attributes . splice ( size_index , 1 ) ;
368382 }
369383
370- const details = {
371- src : `{${ src_var_name } .img.src}` ,
372- width : `{${ src_var_name } .img.w}` ,
373- height : `{${ src_var_name } .img.h}`
374- } ;
375-
376384 return `{#if typeof ${ src_var_name } === 'string'}
377- <img ${ serialize_img_attributes ( content , attributes , details ) } />
385+ {#if import.meta.DEV && ${ ! width_index && ! height_index } }
386+ ${ src_var_name } was not enhanced. Cannot determine dimensions.
387+ {:else}
388+ <img ${ serialize_img_attributes ( content , attributes , {
389+ src : `{${ src_var_name } }`
390+ } ) } />
391+ {/if}
378392{:else}
379393 <picture>
380394 {#each Object.entries(${ src_var_name } .sources) as [format, srcset]}
381395 <source {srcset}${ sizes_string } type={'image/' + format} />
382396 {/each}
383- <img ${ serialize_img_attributes ( content , attributes , details ) } />
397+ <img ${ serialize_img_attributes ( content , attributes , {
398+ src : `{${ src_var_name } .img.src}` ,
399+ width : `{${ src_var_name } .img.w}` ,
400+ height : `{${ src_var_name } .img.h}`
401+ } ) } />
384402 </picture>
385403{/if}` ;
386404}
0 commit comments