Skip to content

fisker/image-minimizer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

image-minimizer

Build Status Coverage Npm Version MIT License

Image minimizer.

Install

yarn add image-minimizer --dev

Usage

import fs from 'node:fs/promises'
import path from 'node:path'
import minifyImages from 'image-minimizer'

const DIRECTORY = new URL('./path/to/directory/', import.meta.url)

let files = await fs.readdir(DIRECTORY, {withFileTypes: true, recursive: true})

files = await Promise.all(
  files
    .filter((dirent) => dirent.isFile())
    .map(async (file) => ({
      name: file.name,
      content: await fs.readFile(path.join(file.path, file.name)),
    })),
)

const compressed = await minifyImages(files)
console.log(compressed)
// -> [<Buffer ...>, ...]

API

minifyImages(fileOrFiles, options?)

options.cache

Type: boolean
Default: true

Enables cache.

options.onFileExtensionError

Type: 'error' | 'warn' | 'ignore' | (file) => void
Default: 'error'

Executes when file extension doesn't match actual file type.