// this must come after the await so that we don't hand off processing between getting
// the imports.size and incrementing the imports.size
const name = imports.get(original_url) || '__IMPORTED_ASSET_' + imports.size + '__';
+ if (!metadata.width || !metadata.height) {
+ console.warn(`Could not determine intrinsic dimensions for ${resolved_id}`);
+ }
const new_markup = `<img ${serialize_img_attributes(content, node.attributes, {
src: `{${name}}`,
- width: metadata.width || 0,
- height: metadata.height || 0
+ width: metadata.width,
+ height: metadata.height
})} />`;
s.update(node.start, node.end, new_markup);
imports.set(original_url, name);
* @param {import('../types/internal.js').Attribute[]} attributes
* @param {{
* src: string,
- * width: string | number,
- * height: string | number
+ * width?: string | number,
+ * height?: string | number
* }} details
*/
function serialize_img_attributes(content, attributes, details) {
}
}
}
- if (!user_width && !user_height) {
- attribute_strings.push(`width=${details.width}`);
- attribute_strings.push(`height=${details.height}`);
- } else if (!user_width && user_height) {
- attribute_strings.push(
- `width=${Math.round(
- (stringToNumber(details.width) * user_height) / stringToNumber(details.height)
- )}`
- );
- } else if (!user_height && user_width) {
- attribute_strings.push(
- `height=${Math.round(
- (stringToNumber(details.height) * user_width) / stringToNumber(details.width)
- )}`
- );
+ if (details.width && details.height) {
+ if (!user_width && !user_height) {
+ attribute_strings.push(`width=${details.width}`);
+ attribute_strings.push(`height=${details.height}`);
+ } else if (!user_width && user_height) {
+ attribute_strings.push(
+ `width=${Math.round(
+ (stringToNumber(details.width) * user_height) / stringToNumber(details.height)
+ )}`
+ );
+ } else if (!user_height && user_width) {
+ attribute_strings.push(
+ `height=${Math.round(
+ (stringToNumber(details.height) * user_width) / stringToNumber(details.width)
+ )}`
+ );
+ }
}
return attribute_strings.join(' ');
*/
function dynamic_img_to_picture(content, node, src_var_name) {
const attributes = node.attributes;
- const index = attributes.findIndex(
- (attribute) => 'name' in attribute && attribute.name === 'sizes'
- );
+ /**
+ * @param attribute_name {string}
+ */
+ function index(attribute_name) {
+ return attributes.findIndex(
+ (attribute) => 'name' in attribute && attribute.name === attribute_name
+ );
+ }
+ const size_index = index('sizes');
+ const width_index = index('width');
+ const height_index = index('height');
let sizes_string = '';
- if (index >= 0) {
- sizes_string = ' ' + content.substring(attributes[index].start, attributes[index].end);
- attributes.splice(index, 1);
+ if (size_index >= 0) {
+ sizes_string =
+ ' ' + content.substring(attributes[size_index].start, attributes[size_index].end);
+ attributes.splice(size_index, 1);
}
- const details = {
- src: `{${src_var_name}.img.src}`,
- width: `{${src_var_name}.img.w}`,
- height: `{${src_var_name}.img.h}`
- };
-
return `{#if typeof ${src_var_name} === 'string'}
- <img ${serialize_img_attributes(content, attributes, details)} />
+ {#if import.meta.DEV && ${!width_index && !height_index}}
+ {${src_var_name}} was not enhanced. Cannot determine dimensions.
+ {:else}
+ <img ${serialize_img_attributes(content, attributes, {
+ src: `{${src_var_name}}`
+ })} />
+ {/if}
{:else}
<picture>
{#each Object.entries(${src_var_name}.sources) as [format, srcset]}
<source {srcset}${sizes_string} type={'image/' + format} />
{/each}
- <img ${serialize_img_attributes(content, attributes, details)} />
+ <img ${serialize_img_attributes(content, attributes, {
+ src: `{${src_var_name}.img.src}`,
+ width: `{${src_var_name}.img.w}`,
+ height: `{${src_var_name}.img.h}`
+ })} />
</picture>
{/if}`;
}
<picture><source srcset="/1 1440w, /2 960w" type="image/avif" /><source srcset="/3 1440w, /4 960w" type="image/webp" /><source srcset="5 1440w, /6 960w" type="image/png" /><img src="/7" alt="absolute path test" width=1440 height=1440 /></picture>
{#if typeof src === 'string'}
- <img src={src.img.src} alt="attribute shorthand test" width={src.img.w} height={src.img.h} />
+ {#if
+ import.meta.DEV && false}
+ {src} was not enhanced. Cannot determine dimensions.
+ {:else}
+ <img src={src} alt="attribute shorthand test" />
+ {/if}
{:else}
<picture>
{#each Object.entries(src.sources) as [format, srcset]}
{#each images as image}
{#if typeof image === 'string'}
- <img src={image.img.src} alt="opt-in test" width={image.img.w} height={image.img.h} />
+ {#if
+ import.meta.DEV && false}
+ {image} was not enhanced. Cannot determine dimensions.
+ {:else}
+ <img src={image} alt="opt-in test" />
+ {/if}
{:else}
<picture>
{#each Object.entries(image.sources) as [format, srcset]}
{#each images as _, i}
{#if typeof get_image(i) === 'string'}
- <img src={get_image(i).img.src} alt="opt-in test" width={get_image(i).img.w} height={get_image(i).img.h} />
+ {#if
+ import.meta.DEV && false}
+ {get_image(i)} was not enhanced. Cannot determine dimensions.
+ {:else}
+ <img src={get_image(i)} alt="opt-in test" />
+ {/if}
{:else}
<picture>
{#each Object.entries(get_image(i).sources) as [format, srcset]}