Skip to content

Commit 9dfac09

Browse files
committed
load config using vite-plugin-svelte
1 parent 6238837 commit 9dfac09

File tree

6 files changed

+17
-35
lines changed

6 files changed

+17
-35
lines changed

.changeset/curvy-flies-carry.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
'@sveltejs/enhanced-img': minor
2+
'@sveltejs/enhanced-img': patch
33
---
44

55
feat: add support for targeting `enhanced\:img` in CSS

packages/enhanced-img/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"zimmerframe": "^1.1.2"
4444
},
4545
"devDependencies": {
46+
"@sveltejs/vite-plugin-svelte": "^5.0.1",
4647
"@types/estree": "^1.0.5",
4748
"@types/node": "^18.19.48",
4849
"rollup": "^4.27.4",
@@ -52,6 +53,7 @@
5253
"vitest": "^3.0.1"
5354
},
5455
"peerDependencies": {
56+
"@sveltejs/vite-plugin-svelte": "^5.0.0",
5557
"svelte": "^5.0.0",
5658
"vite": ">= 5.0.0"
5759
}

packages/enhanced-img/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { imagetools } from 'vite-imagetools';
44
import { image_plugin } from './vite-plugin.js';
55

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

packages/enhanced-img/src/vite-plugin.js

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import { existsSync } from 'node:fs';
44
import path from 'node:path';
5-
import process from 'node:process';
6-
import url from 'node:url';
5+
import { loadSvelteConfig } from '@sveltejs/vite-plugin-svelte';
76
import MagicString from 'magic-string';
87
import sharp from 'sharp';
98
import { parse } from 'svelte-parse-markup';
@@ -15,9 +14,9 @@ const OPTIMIZABLE = /^[^?]+\.(avif|heif|gif|jpeg|jpg|png|tiff|webp)(\?.*)?$/;
1514
/**
1615
* Creates the Svelte image plugin.
1716
* @param {import('vite').Plugin<void>} imagetools_plugin
18-
* @returns {Promise<import('vite').Plugin<void>>}
17+
* @returns {import('vite').Plugin<void>}
1918
*/
20-
export async function image_plugin(imagetools_plugin) {
19+
export function image_plugin(imagetools_plugin) {
2120
// TODO: clear this map in dev mode to avoid memory leak
2221
/**
2322
* URL to image details
@@ -28,17 +27,20 @@ export async function image_plugin(imagetools_plugin) {
2827
/** @type {import('vite').ResolvedConfig} */
2928
let vite_config;
3029

31-
const svelte_config = await load_config();
32-
const extensions = svelte_config.extensions || ['.svelte'];
30+
/** @type {Partial<import('@sveltejs/vite-plugin-svelte').SvelteConfig | undefined>} */
31+
let svelte_config;
3332

3433
return {
3534
name: 'vite-plugin-enhanced-img-markup',
3635
enforce: 'pre',
37-
configResolved(config) {
36+
async configResolved(config) {
3837
vite_config = config;
38+
svelte_config = await loadSvelteConfig();
39+
if (!svelte_config) throw new Error('Could not load Svelte config file');
3940
},
4041
async transform(content, filename) {
4142
const plugin_context = this;
43+
const extensions = svelte_config?.extensions || ['.svelte'];
4244
if (extensions.some((ext) => filename.endsWith(ext))) {
4345
if (!content.includes('<enhanced:img')) {
4446
return;
@@ -378,28 +380,3 @@ function dynamic_img_to_picture(content, node, src_var_name) {
378380
</picture>
379381
{/if}`;
380382
}
381-
382-
/**
383-
* Loads and validates svelte.config.js
384-
* @param {{ cwd?: string }} options
385-
* @returns {Promise<{extensions?: string[]}>}
386-
*/
387-
export async function load_config({ cwd = process.cwd() } = {}) {
388-
const config_file = path.join(cwd, 'svelte.config.js');
389-
390-
if (!existsSync(config_file)) {
391-
return {};
392-
}
393-
394-
const config = await import(`${url.pathToFileURL(config_file).href}?ts=${Date.now()}`);
395-
396-
try {
397-
return config.default;
398-
} catch (e) {
399-
const error = /** @type {Error} */ (e);
400-
401-
// redact the stack trace — it's not helpful to users
402-
error.stack = `Could not load svelte.config.js: ${error.message}\n`;
403-
throw error;
404-
}
405-
}

packages/enhanced-img/test/markup-plugin.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const resolve = /** @param {string} file */ (file) => path.resolve(__dirname, fi
77

88
it('Image preprocess snapshot test', async () => {
99
const filename = 'Input.svelte';
10-
const vite_plugin = await image_plugin({
10+
const vite_plugin = image_plugin({
1111
name: 'vite-imagetools-mock',
1212
load(id) {
1313
if (id.includes('dev')) {

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)