Skip to content

chore: convert preprocessor to vite plugin #13617

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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/curvy-flies-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/enhanced-img': minor
---

feat: add support for targeting `enhanced\:img` in CSS
4 changes: 1 addition & 3 deletions documentation/docs/40-best-practices/07-images.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ Doing this manually is tedious. There are a variety of techniques you can use, d
`@sveltejs/enhanced-img` is a plugin offered on top of Vite's built-in asset handling. It provides plug and play image processing that serves smaller file formats like `avif` or `webp`, automatically sets the intrinsic `width` and `height` of the image to avoid layout shift, creates images of multiple sizes for various devices, and strips EXIF data for privacy. It will work in any Vite-based project including, but not limited to, SvelteKit projects.

> [!NOTE] As a build plugin, `@sveltejs/enhanced-img` can only optimize files located on your machine during the build process. If you have an image located elsewhere (such as a path served from your database, CMS, or backend), please read about [loading images dynamically from a CDN](#Loading-images-dynamically-from-a-CDN).
>
> **WARNING**: The `@sveltejs/enhanced-img` package is experimental. It uses pre-1.0 versioning and may introduce breaking changes with every minor version release.

### Setup

Expand Down Expand Up @@ -67,7 +65,7 @@ At build time, your `<enhanced:img>` tag will be replaced with an `<img>` wrappe

You should provide your image at 2x resolution for HiDPI displays (a.k.a. retina displays). `<enhanced:img>` will automatically take care of serving smaller versions to smaller devices.

If you wish to add styles to your `<enhanced:img>`, you should add a `class` and target that.
> [!NOTE] if you wish to use a [tag name CSS selector](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics/Basic_selectors#type_selectors) in your `<style>` block you will need to write `enhanced\:img` to escape the colon in the tag name.

### Dynamically choosing an image

Expand Down
2 changes: 2 additions & 0 deletions packages/enhanced-img/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"zimmerframe": "^1.1.2"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^5.0.1",
"@types/estree": "^1.0.5",
"@types/node": "^18.19.48",
"rollup": "^4.27.4",
Expand All @@ -52,6 +53,7 @@
"vitest": "^3.0.1"
},
"peerDependencies": {
"@sveltejs/vite-plugin-svelte": "^5.0.0",
"svelte": "^5.0.0",
"vite": ">= 5.0.0"
}
Expand Down
38 changes: 1 addition & 37 deletions packages/enhanced-img/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path';
import process from 'node:process';
import { imagetools } from 'vite-imagetools';
import { image } from './preprocessor.js';
import { image_plugin } from './vite-plugin.js';

/**
* @returns {import('vite').Plugin[]}
Expand All @@ -13,42 +13,6 @@ export function enhancedImages() {
: [];
}

/**
* Creates the Svelte image plugin which provides the preprocessor.
* @param {import('vite').Plugin} imagetools_plugin
* @returns {import('vite').Plugin}
*/
function image_plugin(imagetools_plugin) {
/**
* @type {{
* plugin_context: import('vite').Rollup.PluginContext
* vite_config: import('vite').ResolvedConfig
* imagetools_plugin: import('vite').Plugin
* }}
*/
const opts = {
// @ts-expect-error populated when build starts so we cheat on type
plugin_context: undefined,
// @ts-expect-error populated when build starts so we cheat on type
vite_config: undefined,
imagetools_plugin
};
const preprocessor = image(opts);

return {
name: 'vite-plugin-enhanced-img',
api: {
sveltePreprocess: preprocessor
},
configResolved(config) {
opts.vite_config = config;
},
buildStart() {
opts.plugin_context = this;
}
};
}

/** @type {Record<string,string>} */
const fallback = {
'.avif': 'png',
Expand Down
Loading
Loading