Skip to content

Commit

Permalink
feat(next/image)!: remove squoosh in favor of sharp as optional d…
Browse files Browse the repository at this point in the history
…ependency (#63321)

## History

Previously, we added support for `squoosh` because it was a wasm
implementation that "just worked" on all platforms when running `next
dev` for the first time. However, it was slow so we always recommended
manually installing `sharp` for production use cases running `next
build` and `next start`.

Now that [`sharp` supports
webassembly](https://sharp.pixelplumbing.com/install#webassembly), we no
longer need to maintain `squoosh`, so it can be removed. We also don't
need to make the user install sharp manually because it can be installed
under `optionalDependencies`. I left it optional in case there was some
platform that still needed to manually install the wasm variant with
`npm install --cpu=wasm32 sharp` such as codesandbox/stackblitz (I don't
believe sharp has any fallback built in yet).

Since we can guarantee `sharp`, we can also remove `get-orientation` dep
and upgrade `image-size` dep.

I also moved an [existing `sharp`
test](#56674) into its own fixture
since it was unrelated to image optimization.

## Related Issues
- Fixes #41417
- Related #54670
- Related #54708
- Related #44804
- Related #48820
- Related #61810
- Related #61696
- Related #44685
- Closes #64362

## Breaking Change

This is a breaking change because newer versions of `sharp` no longer
support `yarn@1`.

- lovell/sharp#3750

The workaround is to install with `yarn --ignore-engines` flag.

Also note that Vercel no longer defaults to yarn when no lockfile is
found

- vercel/vercel#11131
- vercel/vercel#11242

Closes NEXT-2823
  • Loading branch information
styfle committed Apr 25, 2024
1 parent 433faa8 commit a6a6117
Show file tree
Hide file tree
Showing 64 changed files with 1,088 additions and 13,414 deletions.
1 change: 0 additions & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
/**/*image*/** @timneutkens @ijjk @shuding @styfle @huozhi @ztanner @vercel/devex
/**/*img* @timneutkens @ijjk @shuding @styfle @huozhi @ztanner @vercel/devex
/packages/next/client/use-intersection.tsx @timneutkens @ijjk @shuding @styfle
/packages/next/server/lib/squoosh/ @timneutkens @ijjk @shuding @styfle
/packages/next/server/serve-static.ts @timneutkens @ijjk @shuding @styfle @huozhi @ztanner
/packages/next/server/config.ts @timneutkens @ijjk @shuding @styfle @huozhi @ztanner

Expand Down
2 changes: 0 additions & 2 deletions docs/03-pages/02-api-reference/01-components/image-legacy.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,6 @@ If you need a different provider, you can use the [`loader`](#loader) prop with

> Images can not be optimized at build time using [`output: 'export'`](/docs/pages/building-your-application/deploying/static-exports), only on-demand. To use `next/legacy/image` with `output: 'export'`, you will need to use a different loader than the default. [Read more in the discussion.](https://github.com/vercel/next.js/discussions/19065)
> The `next/legacy/image` component's default loader uses [`squoosh`](https://www.npmjs.com/package/@squoosh/lib) because it is quick to install and suitable for a development environment. When using `next start` in your production environment, it is strongly recommended that you install [`sharp`](https://www.npmjs.com/package/sharp) by running `npm i sharp` in your project directory. This is not necessary for Vercel deployments, as `sharp` is installed automatically.
## Advanced

The following configuration is for advanced use cases and is usually not necessary. If you choose to configure the properties below, you will override any changes to the Next.js defaults in future updates.
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@
"@types/react-dom": "18.2.23",
"@types/relay-runtime": "14.1.13",
"@types/selenium-webdriver": "4.0.15",
"@types/sharp": "0.29.3",
"@types/string-hash": "1.1.1",
"@types/trusted-types": "2.0.3",
"@typescript-eslint/eslint-plugin": "6.14.0",
Expand Down Expand Up @@ -160,7 +159,7 @@
"html-validator": "5.1.18",
"http-proxy": "1.18.1",
"husky": "8.0.0",
"image-size": "0.9.3",
"image-size": "1.1.1",
"is-animated": "2.0.2",
"isomorphic-unfetch": "3.0.0",
"jest": "29.7.0",
Expand Down
6 changes: 4 additions & 2 deletions packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@
"optional": true
}
},
"optionalDependencies": {
"sharp": "^0.33.3"
},
"devDependencies": {
"@ampproject/toolbox-optimizer": "2.8.3",
"@babel/code-frame": "7.22.5",
Expand Down Expand Up @@ -234,7 +237,6 @@
"events": "3.3.0",
"find-up": "4.1.0",
"fresh": "0.5.2",
"get-orientation": "1.1.2",
"glob": "7.1.7",
"gzip-size": "5.1.1",
"http-proxy": "1.18.1",
Expand All @@ -243,7 +245,7 @@
"https-proxy-agent": "5.0.1",
"icss-utils": "5.1.0",
"ignore-loader": "0.1.2",
"image-size": "1.0.0",
"image-size": "1.1.1",
"is-docker": "2.0.0",
"is-wsl": "2.2.0",
"jest-worker": "27.5.1",
Expand Down
1 change: 0 additions & 1 deletion packages/next/src/build/collect-build-traces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ export async function collectBuildTraces({
// only ignore image-optimizer code when
// this is being handled outside of next-server
'**/next/dist/server/image-optimizer.js',
'**/next/dist/server/lib/squoosh/**/*.wasm',
]
: []),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function nextImageLoader(this: any, content: Buffer) {

const imageSizeSpan = imageLoaderSpan.traceChild('image-size-calculation')
const imageSize = await imageSizeSpan.traceAsyncFn(() =>
getImageSize(content, extension).catch((err) => err)
getImageSize(content).catch((err) => err)
)

if (imageSize instanceof Error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ async function nextMetadataImageLoader(
}

const imageSize: { width?: number; height?: number } = await getImageSize(
content,
extension as 'avif' | 'webp' | 'png' | 'jpeg'
content
).catch((err) => err)

if (imageSize instanceof Error) {
Expand Down
21 changes: 0 additions & 21 deletions packages/next/src/compiled/get-orientation/LICENSE

This file was deleted.

1 change: 0 additions & 1 deletion packages/next/src/compiled/get-orientation/index.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/next/src/compiled/get-orientation/package.json

This file was deleted.

2 changes: 1 addition & 1 deletion packages/next/src/compiled/image-size/LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright © 2017 Aditya Yadav, http://netroy.in
Copyright © 2013-Present Aditya Yadav, http://netroy.in

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/compiled/image-size/index.js

Large diffs are not rendered by default.

Loading

0 comments on commit a6a6117

Please sign in to comment.