Solution 1:

If you want the output format to match the input format, you should look at force option.

sharp(input)
  .jpeg({ progressive: true, force: false })
  .png({ progressive: true, force: false })
  ...

GIF output is not supported, so GIF input will become PNG output by default.

Additional reference: https://sharp.readthedocs.io/en/v0.17.0/api-output/#jpeg

Solution 2:

You can grab file format with metadata method and select according optimisation method for it:

const image = sharp('_4_.jpg')
const meta = await image.metadata()
const { format } = meta

const config = {
  jpeg: { quality: 80 },
  webp: { quality: 80 },
  png: {compressionLevel: 8},
}

await image[format](config[format])
  .resize(1000)