Skip to content

Commit

Permalink
Updates the images integration to automatically mark optimizeDeps f…
Browse files Browse the repository at this point in the history
…or sharp (#3795)

* including src in npm publish

* bugfix: always round dimensions before passing to sharp.resize

* automatically add optimizeDeps vite config

* chore: changeset
  • Loading branch information
Tony Sullivan committed Jul 1, 2022
1 parent 94143fc commit d143d24
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-yaks-hope.md
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions packages/integrations/image/components/Image.astro
Original file line number Diff line number Diff line change
@@ -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<TransformOptions, 'src'>, Omit<ImageAttributes, 'src'> {
src: ImageMetadata | Promise<{ default: ImageMetadata }>;
Expand Down
3 changes: 2 additions & 1 deletion packages/integrations/image/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
},
"files": [
"components",
"dist"
"dist",
"src"
],
"scripts": {
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
Expand Down
5 changes: 4 additions & 1 deletion packages/integrations/image/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ const createIntegration = (options: IntegrationOptions = {}): AstroIntegration =
function getViteConfiguration() {
return {
plugins: [createPlugin(_config, resolvedOptions)],
};
optimizeDeps: {
include: ['image-size', 'sharp']
}
}
}

return {
Expand Down
4 changes: 3 additions & 1 deletion packages/integrations/image/src/loaders/sharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d143d24

Please sign in to comment.