Skip to content

Commit 9da6508

Browse files
authored
fix: throw an error if image cannot be resolved (#11346)
1 parent 5551e35 commit 9da6508

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

.changeset/polite-rice-burn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sveltejs/enhanced-img": patch
3+
---
4+
5+
fix: throw an error if image cannot be resolved

packages/enhanced-img/src/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@ function image_plugin(imagetools_plugin) {
2525
/**
2626
* @type {{
2727
* plugin_context: import('vite').Rollup.PluginContext
28+
* vite_config: import('vite').ResolvedConfig
2829
* imagetools_plugin: import('vite').Plugin
2930
* }}
3031
*/
3132
const opts = {
3233
// @ts-expect-error populated when build starts so we cheat on type
3334
plugin_context: undefined,
35+
// @ts-expect-error populated when build starts so we cheat on type
36+
vite_config: undefined,
3437
imagetools_plugin
3538
};
3639
const preprocessor = image(opts);
@@ -40,6 +43,9 @@ function image_plugin(imagetools_plugin) {
4043
api: {
4144
sveltePreprocess: preprocessor
4245
},
46+
configResolved(config) {
47+
opts.vite_config = config;
48+
},
4349
buildStart() {
4450
opts.plugin_context = this;
4551
}

packages/enhanced-img/src/preprocessor.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { existsSync } from 'node:fs';
2+
import * as path from 'node:path';
3+
14
import MagicString from 'magic-string';
25
import { asyncWalk } from 'estree-walker';
36
import { parse } from 'svelte-parse-markup';
@@ -10,6 +13,7 @@ const OPTIMIZABLE = /^[^?]+\.(avif|heif|gif|jpeg|jpg|png|tiff|webp)(\?.*)?$/;
1013
/**
1114
* @param {{
1215
* plugin_context: import('vite').Rollup.PluginContext
16+
* vite_config: import('vite').ResolvedConfig
1317
* imagetools_plugin: import('vite').Plugin
1418
* }} opts
1519
* @returns {import('svelte/types/compiler/preprocess').PreprocessorGroup}
@@ -72,7 +76,15 @@ export function image(opts) {
7276
// need any logic blocks
7377
image = await resolve(opts, url, filename);
7478
if (!image) {
75-
return;
79+
const file_path = url.substring(0, url.indexOf('?'));
80+
if (existsSync(path.resolve(opts.vite_config.publicDir, file_path))) {
81+
throw new Error(
82+
`Could not locate ${file_path}. Please move it to be located relative to the page in the routes directory or reference it beginning with /static/. See https://vitejs.dev/guide/assets for more details on referencing assets.`
83+
);
84+
}
85+
throw new Error(
86+
`Could not locate ${file_path}. See https://vitejs.dev/guide/assets for more details on referencing assets.`
87+
);
7688
}
7789
images.set(url, image);
7890
}

0 commit comments

Comments
 (0)