I want to optimize my images dynamically using the Parcel 2 built in image optimizer. The image url is coming from the data.js and then I pass it to render it.
When I use this code, it's working:
_generateMarkupProjects(project) {
const imageUrl = new URL('../../images/projectOne.webp?width=640', import.meta.url);
return `
<div class="projects__box">
<figure class="projects__img-box">
<picture>
<img
src="${imageUrl}"
sizes="(max-width: 800px) 45vw"
alt="${project.name} Photo"
class="projects__image"
/>
</picture>
<figcaption class="projects__caption">
<button class="projects__maximize-btn" data-id="${project.id}">
<svg class="projects__icon">
<use
xlink:href="${icon}#icon-maximize"
></use>
</svg>
</button>
<div class="projects__caption-content">
<h3 class="projects__caption-title u-mb-xxs">
${project.name}
</h3>
<p class="projects__caption-description u-mb-xxs">
${project.description}
</p>
<div class="projects__language">${project.languages.join(
', '
)}</div>
</div>
</figcaption>
</figure>
</div>
`;
}
But when I do this: parcel does not optimizing the image.
const imageUrl = new URL(`${project.image}?width=640`, import.meta.url);
This is how I import the project image:
//data.js
import projectOne from 'url:../images/projectOne.webp';