Skip to content

Commit

Permalink
Allow loading images from /public
Browse files Browse the repository at this point in the history
  • Loading branch information
IanVS committed May 7, 2022
1 parent fca2b8d commit aa9bafc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/astro-imagetools/api/utils/getProcessedImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions packages/astro-imagetools/api/utils/getSrcPath.js
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 2 additions & 2 deletions packages/astro-imagetools/api/utils/getSrcset.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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");

Expand Down
3 changes: 2 additions & 1 deletion packages/astro-imagetools/plugin/hooks/load.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit aa9bafc

Please sign in to comment.