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

fix: better error when Sharp can't be resolved (ex: pnpm) (#8128)

* fix: better error when Sharp can't be resolved (ex: pnpm)

* chore: changeset

* Apply suggestions from code review

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>

---------

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
This commit is contained in:
Erika 2023-08-18 15:02:55 +02:00 committed by GitHub
parent dbc97b121f
commit c2c71d90c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 1 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Update error message when Sharp couldn't be found (tends to happen on pnpm notably)

View file

@ -24,3 +24,10 @@ export function sharpImageService(): ImageServiceConfig;
* Return the configuration needed to use the Squoosh-based image service
*/
export function squooshImageService(): ImageServiceConfig;
/**
* Return the configuration needed to use the passthrough image service. This image services does not perform
* any image transformations, and is mainly useful when your platform does not support other image services, or you are
* not using Astro's built-in image processing.
*/
export function passthroughImageService(): ImageServiceConfig;

View file

@ -13,3 +13,10 @@ export function squooshImageService() {
config: {},
};
}
export function passthroughImageService() {
return {
entrypoint: 'astro/assets/services/noop',
config: {},
};
}

View file

@ -1,4 +1,5 @@
import type { FormatEnum } from 'sharp';
import { AstroError, AstroErrorData } from '../../core/errors/index.js';
import type { ImageOutputFormat, ImageQualityPreset } from '../types.js';
import {
baseService,
@ -21,7 +22,7 @@ async function loadSharp() {
try {
sharpImport = (await import('sharp')).default;
} catch (e) {
throw new Error('Could not find Sharp. Please install Sharp manually into your project.');
throw new AstroError(AstroErrorData.MissingSharp);
}
return sharpImport;

View file

@ -791,6 +791,32 @@ export const InvalidDynamicRoute = {
message: (route: string, invalidParam: string, received: string) =>
`The ${invalidParam} param for route ${route} is invalid. Received **${received}**.`,
} satisfies ErrorData;
/**
* @docs
* @see
* - [Default Image Service](https://docs.astro.build/en/guides/images/#default-image-service)
* - [Image Component](https://docs.astro.build/en/guides/images/#image--astroassets)
* - [Image Services API](https://docs.astro.build/en/reference/image-service-reference/)
* @description
* Sharp is the default image service used for `astro:assets`. When using a [strict package manager](https://pnpm.io/pnpm-vs-npm#npms-flat-tree) like pnpm, Sharp must be installed manually into your project in order to use image processing.
*
* If you are not using `astro:assets` for image processing, and do not wish to install Sharp, you can configure the following passthrough image service that does no processing:
*
* ```js
* import { defineConfig, passthroughImageService } from "astro/config";
* export default defineConfig({
* image: {
* service: passthroughImageService(),
* },
* });
* ```
*/
export const MissingSharp = {
name: 'MissingSharp',
title: 'Could not find Sharp.',
message: 'Could not find Sharp. Please install Sharp (`sharp`) manually into your project.',
hint: "See Sharp's installation instructions for more information: https://sharp.pixelplumbing.com/install. If you are not relying on `astro:assets` to optimize, transform, or process any images, you can configure a passthrough image service instead of installing Sharp. See https://docs.astro.build/en/reference/errors/missing-sharp for more information.",
};
// No headings here, that way Vite errors are merged with Astro ones in the docs, which makes more sense to users.
// Vite Errors - 4xxx
/**