feat: add noop service and make integrations that needs it use it (#7903)

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
This commit is contained in:
Erika 2023-08-02 20:28:58 +02:00 committed by Emanuele Stoppa
parent 35f01df797
commit c022a4217a
5 changed files with 41 additions and 9 deletions

View file

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

View file

@ -56,6 +56,7 @@
"./assets/image-endpoint": "./dist/assets/image-endpoint.js", "./assets/image-endpoint": "./dist/assets/image-endpoint.js",
"./assets/services/sharp": "./dist/assets/services/sharp.js", "./assets/services/sharp": "./dist/assets/services/sharp.js",
"./assets/services/squoosh": "./dist/assets/services/squoosh.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": "./dist/content/runtime.js",
"./content/runtime-assets": "./dist/content/runtime-assets.js", "./content/runtime-assets": "./dist/content/runtime-assets.js",
"./debug": "./components/Debug.astro", "./debug": "./components/Debug.astro",

View file

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

View file

@ -4,8 +4,7 @@ import type {
AstroFeatureMap, AstroFeatureMap,
SupportsKind, SupportsKind,
} from '../@types/astro'; } from '../@types/astro';
import { error, type LogOptions, warn } from '../core/logger/core.js'; import { error, warn, type LogOptions } from '../core/logger/core.js';
import { bold } from 'kleur/colors';
const STABLE = 'stable'; const STABLE = 'stable';
const DEPRECATED = 'deprecated'; const DEPRECATED = 'deprecated';
@ -140,9 +139,7 @@ function validateAssetsFeature(
error( error(
logging, logging,
'astro', 'astro',
`The currently selected adapter \`${adapterName}\` is not compatible with the service "Sharp". ${bold( `The currently selected adapter \`${adapterName}\` is not compatible with the image service "Sharp".`
'Your project will NOT be able to build.'
)}`
); );
return false; return false;
} }
@ -151,9 +148,7 @@ function validateAssetsFeature(
error( error(
logging, logging,
'astro', 'astro',
`The currently selected adapter \`${adapterName}\` is not compatible with the service "Squoosh". ${bold( `The currently selected adapter \`${adapterName}\` is not compatible with the image service "Squoosh".`
'Your project will NOT be able to build.'
)}`
); );
return false; return false;
} }

View file

@ -18,7 +18,7 @@ import type { SerializedSSRManifest } from '../core/app/types';
import type { PageBuildData } from '../core/build/types'; import type { PageBuildData } from '../core/build/types';
import { buildClientDirectiveEntrypoint } from '../core/client-directive/index.js'; import { buildClientDirectiveEntrypoint } from '../core/client-directive/index.js';
import { mergeConfig } from '../core/config/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 { isServerLikeOutput } from '../prerender/utils.js';
import { validateSupportedFeatures } from './astroFeaturesValidation.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; settings.adapter = adapter;
}, },