From cc90d72197e1139195e9545105b9a1d339f38e1b Mon Sep 17 00:00:00 2001 From: Erika <3019731+Princesseuh@users.noreply.github.com> Date: Mon, 13 Mar 2023 18:16:17 +0100 Subject: [PATCH] Add warning when using assets with non-node adapters (#6533) * feat(assets): Add a warning that the project won't be able to build if used with a non-Node adapter * chore: changeset --- .changeset/famous-games-warn.md | 5 ++++ .../astro/src/assets/vite-plugin-assets.ts | 25 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 .changeset/famous-games-warn.md diff --git a/.changeset/famous-games-warn.md b/.changeset/famous-games-warn.md new file mode 100644 index 000000000..c87171fad --- /dev/null +++ b/.changeset/famous-games-warn.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Added a warning when trying to use `experimental.assets` with a not compatible adapter diff --git a/packages/astro/src/assets/vite-plugin-assets.ts b/packages/astro/src/assets/vite-plugin-assets.ts index 306c8909b..638fa9149 100644 --- a/packages/astro/src/assets/vite-plugin-assets.ts +++ b/packages/astro/src/assets/vite-plugin-assets.ts @@ -1,3 +1,4 @@ +import { bold } from 'kleur/colors'; import MagicString from 'magic-string'; import mime from 'mime'; import fs from 'node:fs/promises'; @@ -28,6 +29,30 @@ export default function assets({ globalThis.astroAsset = {}; + const UNSUPPORTED_ADAPTERS = new Set([ + '@astrojs/cloudflare', + '@astrojs/deno', + '@astrojs/netlify/edge-functions', + '@astrojs/vercel/edge', + ]); + + const adapterName = settings.config.adapter?.name; + if ( + ['astro/assets/services/sharp', 'astro/assets/services/squoosh'].includes( + settings.config.image.service + ) && + adapterName && + UNSUPPORTED_ADAPTERS.has(adapterName) + ) { + error( + logging, + 'assets', + `The currently selected adapter \`${adapterName}\` does not run on Node, however the currently used image service depends on Node built-ins. ${bold( + 'Your project will NOT be able to build.' + )}` + ); + } + return [ // Expose the components and different utilities from `astro:assets` and handle serving images from `/_image` in dev {