99a4ea6e9a
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
34 lines
620 B
TypeScript
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)
|
|
},
|
|
})
|
|
}
|