From aa9bafc4febd019e61d1d6eaad2f67325ab38c13 Mon Sep 17 00:00:00 2001 From: Ian VanSchooten Date: Sat, 7 May 2022 00:42:15 -0400 Subject: [PATCH] Allow loading images from /public --- .../api/utils/getProcessedImage.js | 3 +- .../astro-imagetools/api/utils/getSrcPath.js | 31 +++++++++++++++++++ .../astro-imagetools/api/utils/getSrcset.js | 4 +-- .../astro-imagetools/plugin/hooks/load.js | 3 +- 4 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 packages/astro-imagetools/api/utils/getSrcPath.js diff --git a/packages/astro-imagetools/api/utils/getProcessedImage.js b/packages/astro-imagetools/api/utils/getProcessedImage.js index d592d6f1..db867986 100644 --- a/packages/astro-imagetools/api/utils/getProcessedImage.js +++ b/packages/astro-imagetools/api/utils/getProcessedImage.js @@ -9,6 +9,7 @@ import { supportedImageTypes, } from "../../utils/runtimeChecks.js"; import { fileURLToPath } from "url"; +import { getSrcPath } from './getSrcPath.js'; const { getImageDetails } = await (sharp ? import("./imagetools.js") @@ -93,7 +94,7 @@ export default async (src, transformConfigs) => { const path = src.replace(/\\/g, `/`); const { image, imageWidth, imageHeight, imageFormat } = await getImageDetails( - `./${src}`, + getSrcPath(src), width, height, aspect diff --git a/packages/astro-imagetools/api/utils/getSrcPath.js b/packages/astro-imagetools/api/utils/getSrcPath.js new file mode 100644 index 00000000..bf26c7f3 --- /dev/null +++ b/packages/astro-imagetools/api/utils/getSrcPath.js @@ -0,0 +1,31 @@ +import fs from 'fs'; +import path from 'path'; +import { cwd } from "../../utils/runtimeChecks.js"; +import printWarning from "../../utils/printWarning.js"; + +// To strip off params when checking for file on disk. +const paramPattern = /\?.*/ + +/** + * getSrcPath allows the use of `src` attributes relative to either the /public folder or project root. + * + * It first checks to see if the src is a file relative to the cwd (project root). + * If the file isn't found, it will look in the /public folder. + * Finally, if it still can't be found, the original input will be returned. + */ +export function getSrcPath(src) { + // If this is already resolved to a file, return it. + if (fs.existsSync(src.replace(paramPattern, ''))) return src; + + const rootPath = path.join(cwd, src); + const rootTest = rootPath.replace(paramPattern, '') + if (fs.existsSync(rootTest)) return rootPath; + + const publicPath = path.join(cwd, 'public', src); + const publicTest = publicPath.replace(paramPattern, '') + if (fs.existsSync(publicTest)) return publicPath; + + // Fallback + printWarning({message: `"${src}" could not not be found at \n${rootTest} \n\tor \n${publicTest}`}); + return src; +} diff --git a/packages/astro-imagetools/api/utils/getSrcset.js b/packages/astro-imagetools/api/utils/getSrcset.js index 0d9141b3..07967e76 100644 --- a/packages/astro-imagetools/api/utils/getSrcset.js +++ b/packages/astro-imagetools/api/utils/getSrcset.js @@ -1,5 +1,5 @@ // @ts-check -import { cwd } from "../../utils/runtimeChecks.js"; +import { getSrcPath } from './getSrcPath'; export default async function getSrcset(src, breakpoints, format, options) { options = { @@ -23,7 +23,7 @@ export default async function getSrcset(src, breakpoints, format, options) { const id = `${src}?${params.slice(1)}`; if (process.env.npm_lifecycle_event !== "dev") { - const fullPath = cwd + id; + const fullPath = getSrcPath(id); const { default: load } = await import("../../plugin/hooks/load.js"); diff --git a/packages/astro-imagetools/plugin/hooks/load.js b/packages/astro-imagetools/plugin/hooks/load.js index 6a71b149..22731934 100644 --- a/packages/astro-imagetools/plugin/hooks/load.js +++ b/packages/astro-imagetools/plugin/hooks/load.js @@ -1,6 +1,7 @@ // @ts-check import path from "path"; import objectHash from "object-hash"; +import { getSrcPath } from "../../api/utils/getSrcPath.js"; import { getCachedBuffer } from "../utils/cache.js"; import { getAssetPath, getConfigOptions } from "../utils/shared.js"; import { cwd, sharp, supportedImageTypes } from "../../utils/runtimeChecks.js"; @@ -31,7 +32,7 @@ export default async function load(id) { const { environment, projectBase, assetFileNames } = astroViteConfigs; - const src = id.startsWith(cwd) ? id : cwd + id; + const src = getSrcPath(id); const config = Object.fromEntries(searchParams);