Skip to content

Commit

Permalink
address todo
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Dec 19, 2023
1 parent 6e387b6 commit b3d49bc
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
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
5 changes: 2 additions & 3 deletions packages/enhanced-img/src/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,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 @@ -76,9 +77,7 @@ export function image(opts) {
image = await resolve(opts, url, filename);
if (!image) {
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))) {
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 beginning with /static/. See https://vitejs.dev/guide/assets for more details on referencing assets.`
);
Expand Down

0 comments on commit b3d49bc

Please sign in to comment.