Skip to content

Commit 6238837

Browse files
committed
chore: convert preprocessor to vite plugin
1 parent a9611fa commit 6238837

File tree

8 files changed

+490
-469
lines changed

8 files changed

+490
-469
lines changed

.changeset/curvy-flies-carry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/enhanced-img': minor
3+
---
4+
5+
feat: add support for targeting `enhanced\:img` in CSS

documentation/docs/40-best-practices/07-images.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ Doing this manually is tedious. There are a variety of techniques you can use, d
2727
`@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.
2828

2929
> [!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).
30-
>
31-
> **WARNING**: The `@sveltejs/enhanced-img` package is experimental. It uses pre-1.0 versioning and may introduce breaking changes with every minor version release.
3230
3331
### Setup
3432

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

6866
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.
6967

70-
If you wish to add styles to your `<enhanced:img>`, you should add a `class` and target that.
68+
> [!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.
7169
7270
### Dynamically choosing an image
7371

packages/enhanced-img/src/index.js

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import path from 'node:path';
22
import process from 'node:process';
33
import { imagetools } from 'vite-imagetools';
4-
import { image } from './preprocessor.js';
4+
import { image_plugin } from './vite-plugin.js';
55

66
/**
7-
* @returns {import('vite').Plugin[]}
7+
* @returns {Array<import('vite').Plugin | Promise<import('vite').Plugin>>}
88
*/
99
export function enhancedImages() {
1010
const imagetools_instance = imagetools_plugin();
@@ -13,42 +13,6 @@ export function enhancedImages() {
1313
: [];
1414
}
1515

16-
/**
17-
* Creates the Svelte image plugin which provides the preprocessor.
18-
* @param {import('vite').Plugin} imagetools_plugin
19-
* @returns {import('vite').Plugin}
20-
*/
21-
function image_plugin(imagetools_plugin) {
22-
/**
23-
* @type {{
24-
* plugin_context: import('vite').Rollup.PluginContext
25-
* vite_config: import('vite').ResolvedConfig
26-
* imagetools_plugin: import('vite').Plugin
27-
* }}
28-
*/
29-
const opts = {
30-
// @ts-expect-error populated when build starts so we cheat on type
31-
plugin_context: undefined,
32-
// @ts-expect-error populated when build starts so we cheat on type
33-
vite_config: undefined,
34-
imagetools_plugin
35-
};
36-
const preprocessor = image(opts);
37-
38-
return {
39-
name: 'vite-plugin-enhanced-img',
40-
api: {
41-
sveltePreprocess: preprocessor
42-
},
43-
configResolved(config) {
44-
opts.vite_config = config;
45-
},
46-
buildStart() {
47-
opts.plugin_context = this;
48-
}
49-
};
50-
}
51-
5216
/** @type {Record<string,string>} */
5317
const fallback = {
5418
'.avif': 'png',

0 commit comments

Comments
 (0)