From 5811208182fb70fad730b0e495d054ba970b9353 Mon Sep 17 00:00:00 2001 From: Tony Sullivan Date: Fri, 5 Aug 2022 20:36:30 +0000 Subject: [PATCH] Fixes local image resolution in SSR builds on Windows (#4173) * fixing SSR local file resolution path on windows * chore: add changeset * nit: fixing typo in image README file Co-authored-by: Nate Moore --- .changeset/long-bees-promise.md | 5 +++++ packages/integrations/image/README.md | 6 ++---- packages/integrations/image/src/build/ssr.ts | 11 ++++++----- packages/integrations/image/src/endpoints/prod.ts | 2 +- packages/integrations/image/src/utils/images.ts | 2 +- 5 files changed, 15 insertions(+), 11 deletions(-) create mode 100644 .changeset/long-bees-promise.md diff --git a/.changeset/long-bees-promise.md b/.changeset/long-bees-promise.md new file mode 100644 index 0000000000..af9b1a31f6 --- /dev/null +++ b/.changeset/long-bees-promise.md @@ -0,0 +1,5 @@ +--- +'@astrojs/image': patch +--- + +Fixes a bug related to local image files in SSR builds on Windows diff --git a/packages/integrations/image/README.md b/packages/integrations/image/README.md index 60c347ef9d..07b191ba64 100644 --- a/packages/integrations/image/README.md +++ b/packages/integrations/image/README.md @@ -216,17 +216,15 @@ The list of sizes that should be built for responsive images. This is combined w

**Type:** `number` | `string`
-**Required:** `true` +**Default:** `undefined`

-The desired aspect ratio of the output image. This is combined with `widhts` to calculate the final dimensions of each built image. +The desired aspect ratio of the output image. This is combined with `widths` to calculate the final dimensions of each built image. A `string` can be provided in the form of `{width}:{height}`, ex: `16:9` or `3:4`. A `number` can also be provided, useful when the aspect ratio is calculated at build time. This can be an inline number such as `1.777` or inlined as a JSX expression like `aspectRatio={16/9}`. -#### formats -

**Type:** `Array<'avif' | 'jpeg' | 'png' | 'webp'>`
diff --git a/packages/integrations/image/src/build/ssr.ts b/packages/integrations/image/src/build/ssr.ts index e8de6ae3c7..19fea5aff3 100644 --- a/packages/integrations/image/src/build/ssr.ts +++ b/packages/integrations/image/src/build/ssr.ts @@ -6,8 +6,8 @@ import { ensureDir } from '../utils/paths.js'; async function globImages(dir: URL) { const srcPath = fileURLToPath(dir); - return await glob(`${srcPath}/**/*.{heic,heif,avif,jpeg,jpg,png,tiff,webp,gif}`, { - absolute: true, + return await glob('./**/*.{heic,heif,avif,jpeg,jpg,png,tiff,webp,gif}', { + cwd: fileURLToPath(dir) }); } @@ -19,10 +19,11 @@ export interface SSRBuildParams { export async function ssrBuild({ srcDir, outDir }: SSRBuildParams) { const images = await globImages(srcDir); - for await (const image of images) { - const to = image.replace(fileURLToPath(srcDir), fileURLToPath(outDir)); + for (const image of images) { + const from = path.join(fileURLToPath(srcDir), image); + const to = path.join(fileURLToPath(outDir), image); await ensureDir(path.dirname(to)); - await fs.copyFile(image, to); + await fs.copyFile(from, to); } } diff --git a/packages/integrations/image/src/endpoints/prod.ts b/packages/integrations/image/src/endpoints/prod.ts index 657f3856da..667410a8b7 100644 --- a/packages/integrations/image/src/endpoints/prod.ts +++ b/packages/integrations/image/src/endpoints/prod.ts @@ -21,7 +21,7 @@ export const get: APIRoute = async ({ request }) => { } else { const clientRoot = new URL('../client/', import.meta.url); const localPath = new URL('.' + transform.src, clientRoot); - inputBuffer = await loadLocalImage(localPath.pathname); + inputBuffer = await loadLocalImage(localPath); } if (!inputBuffer) { diff --git a/packages/integrations/image/src/utils/images.ts b/packages/integrations/image/src/utils/images.ts index f3c5196573..cc5a26cdcb 100644 --- a/packages/integrations/image/src/utils/images.ts +++ b/packages/integrations/image/src/utils/images.ts @@ -13,7 +13,7 @@ export function isRemoteImage(src: string) { return /^http(s?):\/\//.test(src); } -export async function loadLocalImage(src: string) { +export async function loadLocalImage(src: string | URL) { try { return await fs.readFile(src); } catch {