From c022a4217a805d223c1494e9eda4e48bbf810388 Mon Sep 17 00:00:00 2001 From: Erika <3019731+Princesseuh@users.noreply.github.com> Date: Wed, 2 Aug 2023 20:28:58 +0200 Subject: [PATCH] feat: add noop service and make integrations that needs it use it (#7903) Co-authored-by: Nate Moore --- .changeset/heavy-walls-arrive.md | 8 ++++++++ packages/astro/package.json | 1 + packages/astro/src/assets/services/noop.ts | 17 +++++++++++++++++ .../src/integrations/astroFeaturesValidation.ts | 11 +++-------- packages/astro/src/integrations/index.ts | 13 ++++++++++++- 5 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 .changeset/heavy-walls-arrive.md create mode 100644 packages/astro/src/assets/services/noop.ts diff --git a/.changeset/heavy-walls-arrive.md b/.changeset/heavy-walls-arrive.md new file mode 100644 index 000000000..68f64dacb --- /dev/null +++ b/.changeset/heavy-walls-arrive.md @@ -0,0 +1,8 @@ +--- +'@astrojs/cloudflare': major +'@astrojs/netlify': major +'@astrojs/vercel': major +'astro': major +--- + +When using an adapter that supports neither Squoosh or Sharp, Astro will now automatically use an image service that does not support processing, but still provides the other benefits of `astro:assets` such as enforcing `alt`, no CLS etc to users diff --git a/packages/astro/package.json b/packages/astro/package.json index 78e880487..1377605f3 100644 --- a/packages/astro/package.json +++ b/packages/astro/package.json @@ -56,6 +56,7 @@ "./assets/image-endpoint": "./dist/assets/image-endpoint.js", "./assets/services/sharp": "./dist/assets/services/sharp.js", "./assets/services/squoosh": "./dist/assets/services/squoosh.js", + "./assets/services/noop": "./dist/assets/services/noop.js", "./content/runtime": "./dist/content/runtime.js", "./content/runtime-assets": "./dist/content/runtime-assets.js", "./debug": "./components/Debug.astro", diff --git a/packages/astro/src/assets/services/noop.ts b/packages/astro/src/assets/services/noop.ts new file mode 100644 index 000000000..d57ffeb27 --- /dev/null +++ b/packages/astro/src/assets/services/noop.ts @@ -0,0 +1,17 @@ +import { baseService, type LocalImageService } from './service.js'; + +// Empty service used for platforms that neither support Squoosh or Sharp. +const noopService: LocalImageService = { + validateOptions: baseService.validateOptions, + getURL: baseService.getURL, + parseURL: baseService.parseURL, + getHTMLAttributes: baseService.getHTMLAttributes, + async transform(inputBuffer, transformOptions) { + return { + data: inputBuffer, + format: transformOptions.format, + }; + }, +}; + +export default noopService; diff --git a/packages/astro/src/integrations/astroFeaturesValidation.ts b/packages/astro/src/integrations/astroFeaturesValidation.ts index 6b92813c5..c494b35f4 100644 --- a/packages/astro/src/integrations/astroFeaturesValidation.ts +++ b/packages/astro/src/integrations/astroFeaturesValidation.ts @@ -4,8 +4,7 @@ import type { AstroFeatureMap, SupportsKind, } from '../@types/astro'; -import { error, type LogOptions, warn } from '../core/logger/core.js'; -import { bold } from 'kleur/colors'; +import { error, warn, type LogOptions } from '../core/logger/core.js'; const STABLE = 'stable'; const DEPRECATED = 'deprecated'; @@ -140,9 +139,7 @@ function validateAssetsFeature( error( logging, 'astro', - `The currently selected adapter \`${adapterName}\` is not compatible with the service "Sharp". ${bold( - 'Your project will NOT be able to build.' - )}` + `The currently selected adapter \`${adapterName}\` is not compatible with the image service "Sharp".` ); return false; } @@ -151,9 +148,7 @@ function validateAssetsFeature( error( logging, 'astro', - `The currently selected adapter \`${adapterName}\` is not compatible with the service "Squoosh". ${bold( - 'Your project will NOT be able to build.' - )}` + `The currently selected adapter \`${adapterName}\` is not compatible with the image service "Squoosh".` ); return false; } diff --git a/packages/astro/src/integrations/index.ts b/packages/astro/src/integrations/index.ts index 75971fa53..71c5a5e63 100644 --- a/packages/astro/src/integrations/index.ts +++ b/packages/astro/src/integrations/index.ts @@ -18,7 +18,7 @@ import type { SerializedSSRManifest } from '../core/app/types'; import type { PageBuildData } from '../core/build/types'; import { buildClientDirectiveEntrypoint } from '../core/client-directive/index.js'; import { mergeConfig } from '../core/config/index.js'; -import { info, warn, error, type LogOptions, AstroIntegrationLogger } from '../core/logger/core.js'; +import { AstroIntegrationLogger, error, info, warn, type LogOptions } from '../core/logger/core.js'; import { isServerLikeOutput } from '../prerender/utils.js'; import { validateSupportedFeatures } from './astroFeaturesValidation.js'; @@ -221,6 +221,17 @@ export async function runHookConfigDone({ ); } } + if (!validationResult.assets) { + info( + logging, + 'astro', + `The selected adapter ${adapter.name} does not support Sharp or Squoosh for image processing. To ensure your project is still able to build, image processing has been disabled.` + ); + settings.config.image.service = { + entrypoint: 'astro/assets/services/noop', + config: {}, + }; + } } settings.adapter = adapter; },