diff --git a/.changeset/neat-yaks-hope.md b/.changeset/neat-yaks-hope.md new file mode 100644 index 000000000..e892b4cc6 --- /dev/null +++ b/.changeset/neat-yaks-hope.md @@ -0,0 +1,5 @@ +--- +'@astrojs/image': patch +--- + +Automatically adds the required `vite.optimizeDeps` config for `sharp`. Also ensures that only whole numbers are passed to sharp's resize transform diff --git a/packages/integrations/image/components/Image.astro b/packages/integrations/image/components/Image.astro index 6b10959d4..51d4182a2 100644 --- a/packages/integrations/image/components/Image.astro +++ b/packages/integrations/image/components/Image.astro @@ -1,8 +1,8 @@ --- // @ts-ignore import loader from 'virtual:image-loader'; -import { getImage } from '../src'; -import type { ImageAttributes, ImageMetadata, TransformOptions, OutputFormat } from '../src/types'; +import { getImage } from '../src/index.js'; +import type { ImageAttributes, ImageMetadata, TransformOptions, OutputFormat } from '../src/types.js'; export interface LocalImageProps extends Omit, Omit { src: ImageMetadata | Promise<{ default: ImageMetadata }>; diff --git a/packages/integrations/image/package.json b/packages/integrations/image/package.json index adf4f9e1c..e496a1cbd 100644 --- a/packages/integrations/image/package.json +++ b/packages/integrations/image/package.json @@ -28,7 +28,8 @@ }, "files": [ "components", - "dist" + "dist", + "src" ], "scripts": { "build": "astro-scripts build \"src/**/*.ts\" && tsc", diff --git a/packages/integrations/image/src/index.ts b/packages/integrations/image/src/index.ts index 43cf8fd1e..753537c24 100644 --- a/packages/integrations/image/src/index.ts +++ b/packages/integrations/image/src/index.ts @@ -75,7 +75,10 @@ const createIntegration = (options: IntegrationOptions = {}): AstroIntegration = function getViteConfiguration() { return { plugins: [createPlugin(_config, resolvedOptions)], - }; + optimizeDeps: { + include: ['image-size', 'sharp'] + } + } } return { diff --git a/packages/integrations/image/src/loaders/sharp.ts b/packages/integrations/image/src/loaders/sharp.ts index d5d3da8f0..8b029a285 100644 --- a/packages/integrations/image/src/loaders/sharp.ts +++ b/packages/integrations/image/src/loaders/sharp.ts @@ -84,7 +84,9 @@ class SharpService implements SSRImageService { const sharpImage = sharp(inputBuffer, { failOnError: false }); if (transform.width || transform.height) { - sharpImage.resize(transform.width, transform.height); + const width = transform.width && Math.round(transform.width); + const height = transform.height && Math.round(transform.height); + sharpImage.resize(width, height); } if (transform.format) {