Skip to content

Commit

Permalink
feat: add noop service and make integrations that needs it use it (#7903
Browse files Browse the repository at this point in the history
)

Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
  • Loading branch information
2 people authored and ematipico committed Aug 8, 2023
1 parent 997a0db commit 7511a49
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
8 changes: 8 additions & 0 deletions .changeset/heavy-walls-arrive.md
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
17 changes: 17 additions & 0 deletions packages/astro/src/assets/services/noop.ts
Original file line number Diff line number Diff line change
@@ -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;
11 changes: 3 additions & 8 deletions packages/astro/src/integrations/astroFeaturesValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
13 changes: 12 additions & 1 deletion packages/astro/src/integrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
},
Expand Down

0 comments on commit 7511a49

Please sign in to comment.