rsnext/packages/next/next-server/server/lib/squoosh/emscripten-utils.ts
Joe Haddad 99a4ea6e9a
feat(next/image): remove sharp for wasm variant (#22253)
This pull request removes the native `sharp` dependency (which doesn't work on some Linux variants, nor **M1 Mac**) and replaces it with a wasm equivalent.

It also reduces Next.js' installed size by 27.3 MB.

The code is adapted from the [Squoosh CLI](https://github.com/GoogleChromeLabs/squoosh).

This PR still supports:

- Rotation normalization
- Resizing
- PNG
- JPEG
- Webp

However, it (temporarily) removes support for:
- Resizing Gifs
- Resizing Tiff

(these formats still get served and rendered correctly by the image component)

---

Fixes #20456
Closes #20738
Closes #21762
2021-02-18 10:23:24 +00:00

34 lines
620 B
TypeScript

import { fileURLToPath } from 'url'
export function pathify(path: string) {
if (path.startsWith('file://')) {
path = fileURLToPath(path)
}
return path
}
export function instantiateEmscriptenWasm(
factory: (args: {
locateFile: () => string
}) => {
decode?: (
buffer: Buffer | Uint8Array,
width: number,
height: number,
opts: any
) => Buffer
encode?: (
buffer: Buffer | Uint8Array,
width: number,
height: number,
opts: any
) => Buffer
},
path: string
) {
return factory({
locateFile() {
return pathify(path)
},
})
}