Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid creating conflicting import statements #11047

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/violet-moons-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/enhanced-img': patch
---

fix: avoid creating conflicting import statements
27 changes: 13 additions & 14 deletions packages/enhanced-img/src/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function image(opts) {
// TODO: clear this map in dev mode to avoid memory leak
/**
* URL to image details
* @type {Map<string, { image: import('vite-imagetools').Picture, name: string }>}
* @type {Map<string, import('vite-imagetools').Picture>}
*/
const images = new Map();

Expand Down Expand Up @@ -65,22 +65,21 @@ export function image(opts) {
}
url += 'enhanced';

const name = ASSET_PREFIX + images.size;
if (OPTIMIZABLE.test(url)) {
let details = images.get(url);
if (!details) {
let image = images.get(url);
if (!image) {
// resolves the import so that we can build the entire picture template string and don't
// need any logic blocks
const image = await resolve(opts, url, filename);
image = await resolve(opts, url, filename);
if (!image) {
return;
}
details = images.get(url) || { name, image };
images.set(url, details);
images.set(url, image);
}
s.update(node.start, node.end, img_to_picture(content, node, details));
s.update(node.start, node.end, img_to_picture(content, node, image));
} else {
// e.g. <img src="./foo.svg" /> => <img src={___ASSET___0} />
const name = ASSET_PREFIX + imports.size;
const { start, end } = src_attribute;
// update src with reference to imported asset
s.update(
Expand Down Expand Up @@ -255,9 +254,9 @@ function stringToNumber(param) {
/**
* @param {string} content
* @param {import('svelte/types/compiler/interfaces').TemplateNode} node
* @param {{ image: import('vite-imagetools').Picture, name: string }} details
* @param {import('vite-imagetools').Picture} image
*/
function img_to_picture(content, node, details) {
function img_to_picture(content, node, image) {
/** @type {Array<import('svelte/types/compiler/interfaces').BaseDirective | import('svelte/types/compiler/interfaces').Attribute | import('svelte/types/compiler/interfaces').SpreadAttribute>} attributes */
const attributes = node.attributes;
const index = attributes.findIndex((attribute) => attribute.name === 'sizes');
Expand All @@ -268,13 +267,13 @@ function img_to_picture(content, node, details) {
}

let res = '<picture>';
for (const [format, srcset] of Object.entries(details.image.sources)) {
for (const [format, srcset] of Object.entries(image.sources)) {
res += `<source srcset="${srcset}"${sizes_string} type="image/${format}" />`;
}
res += `<img ${img_attributes_to_markdown(content, attributes, {
src: details.image.img.src,
width: details.image.img.w,
height: details.image.img.h
src: image.img.src,
width: image.img.w,
height: image.img.h
})} />`;
res += '</picture>';
return res;
Expand Down
4 changes: 2 additions & 2 deletions packages/enhanced-img/test/Output.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script lang="ts">
import ___ASSET___6 from "./foo.svg";
import ___ASSET___0 from "./foo.svg";

import manual_image1 from './no.png';

Expand Down Expand Up @@ -30,7 +30,7 @@

<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>

<img src={___ASSET___6} alt="svg test" />
<img src={___ASSET___0} alt="svg test" />

{#each images as image}
{#if typeof image === 'string'}
Expand Down
Loading