Skip to content
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

fix: throw an error if image cannot be resolved #11346

Merged
merged 3 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix: throw an error if image cannot be resolved
  • Loading branch information
benmccann committed Dec 15, 2023
commit 6e387b6510ed1fd277bf9c3557ca4d2a1abe904e
5 changes: 5 additions & 0 deletions .changeset/polite-rice-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sveltejs/enhanced-img": patch
---

fix: throw an error if image cannot be resolved
15 changes: 14 additions & 1 deletion packages/enhanced-img/src/preprocessor.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { existsSync } from 'node:fs';
import * as path from 'node:path';

import MagicString from 'magic-string';
import { asyncWalk } from 'estree-walker';
import { parse } from 'svelte-parse-markup';
Expand Down Expand Up @@ -72,7 +75,17 @@ export function image(opts) {
// need any logic blocks
image = await resolve(opts, url, filename);
if (!image) {
return;
const file_path = url.substring(0, url.indexOf('?'));
// TODO: use kit.files.assets from the Svelte config or the Vite publicDir
// this is good enough for now since it's purely a nicety and most people won't have changed it
if (existsSync(path.resolve('static', file_path))) {
throw new Error(
`Could not locate ${file_path}. Please move it to be located relative to the page in the routes directory or reference beginning with /static/. See https://vitejs.dev/guide/assets for more details on referencing assets.`
);
}
throw new Error(
`Could not locate ${file_path}. See https://vitejs.dev/guide/assets for more details on referencing assets.`
);
}
images.set(url, image);
}
Expand Down
Loading