Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(image): Update image service config to allow passing settings to the service #6848

Merged
merged 8 commits into from
Apr 27, 2023

Conversation

Princesseuh
Copy link
Member

@Princesseuh Princesseuh commented Apr 14, 2023

Changes

Certain image services need a separate config, this is applicable for both remote and local image services. For instance, you might want a setting to restrict the remote patterns supported. Or for remote services, maybe your service only support a certain amount of widths and heights, or you can set defaults etc etc.

It looks like this:

export default defineConfig({
  experimental: {
    assets: true,
  },
  image: {
    service: { entrypoint: "./src/imageService.ts", config: {widths: [300, 600]}}
  },
});

and the settings are passed to all the hooks as a second parameter:

const service: ExternalImageService = {
  validateOptions(options, serviceOptions) {
  	if (!serviceOptions.widths.includes(options.width)) {
  		throw new Error("Specified width is not currently present in your service config")
  	}
  }
}

Since we have a new shape, I also added utilities functions to astro/assets to set the service:

import { squooshImageService } from "astro/assets/;

export default defineConfig({
  experimental: {
    assets: true,
  },
  image: {
    service: squooshImageService()
  },
});

Testing

Added a test

Docs

withastro/docs#3096

@changeset-bot
Copy link

changeset-bot bot commented Apr 14, 2023

🦋 Changeset detected

Latest commit: 4b6915f

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Apr 14, 2023
@Princesseuh Princesseuh changed the title feat(image): Add a setting to pass settings to the image service feat(image): Update image service config to allow passing settings to the service Apr 25, 2023
@Princesseuh Princesseuh marked this pull request as ready for review April 25, 2023 16:59
@Princesseuh Princesseuh requested a review from a team as a code owner April 25, 2023 16:59
* @type {'astro/assets/services/sharp' | 'astro/assets/services/squoosh' | string}
* @default `'astro/assets/services/squoosh'`
* @type {{entrypoint: 'astro/assets/services/sharp' | 'astro/assets/services/squoosh' | string, config: Record<string, any>}}
* @default `{entrypoint: 'astro/assets/services/squoosh', config?: {}}`
* @version 2.1.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just curious because I don't know: Do we need to worry about updating the version number here since this specific configuration will only be appropriate for 2.3.x? (Even though image.service itself was added in 2.1.0?)

Are we at all worried about this being misleading for someone who happens to be on a slightly older version of Astro before this update to the image service config?

Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general implementation looks good to me

packages/astro/src/assets/index.ts Outdated Show resolved Hide resolved
@Princesseuh Princesseuh merged commit ebae1ea into main Apr 27, 2023
@Princesseuh Princesseuh deleted the feat/image-services-options branch April 27, 2023 16:16
@astrobot-houston astrobot-houston mentioned this pull request Apr 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants