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 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/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
6 changes: 6 additions & 0 deletions packages/enhanced-img/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ 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);
Expand All @@ -40,6 +43,9 @@ function image_plugin(imagetools_plugin) {
api: {
sveltePreprocess: preprocessor
},
configResolved(config) {
opts.vite_config = config;
},
buildStart() {
opts.plugin_context = this;
}
Expand Down
14 changes: 13 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 All @@ -10,6 +13,7 @@ const OPTIMIZABLE = /^[^?]+\.(avif|heif|gif|jpeg|jpg|png|tiff|webp)(\?.*)?$/;
/**
* @param {{
* plugin_context: import('vite').Rollup.PluginContext
* vite_config: import('vite').ResolvedConfig
* imagetools_plugin: import('vite').Plugin
* }} opts
* @returns {import('svelte/types/compiler/preprocess').PreprocessorGroup}
Expand Down Expand Up @@ -72,7 +76,15 @@ 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('?'));
if (existsSync(path.resolve(opts.vite_config.publicDir, 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 it 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