0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-16 21:46:22 -05:00

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 <natemoo-re@users.noreply.github.com>
This commit is contained in:
Tony Sullivan 2022-08-05 20:36:30 +00:00 committed by GitHub
parent 66cdc7e4d0
commit 5811208182
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 15 additions and 11 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/image': patch
---
Fixes a bug related to local image files in SSR builds on Windows

View file

@ -216,17 +216,15 @@ The list of sizes that should be built for responsive images. This is combined w
<p>
**Type:** `number` | `string`<br>
**Required:** `true`
**Default:** `undefined`
</p>
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
<p>
**Type:** `Array<'avif' | 'jpeg' | 'png' | 'webp'>`<br>

View file

@ -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);
}
}

View file

@ -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) {

View file

@ -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 {