A Node.js command-line tool and script to compress and optimize images, using different algorithms to reduce file size.
If you have a large number of images in a folder that need to be compressed and optimized, this package can be a lifesaver. With just one command, you can reduce the size of your images, making them load faster on your website. Whether you need to compress JPGs, optimize SVGs, or generate compressed AVIF or WebP images, this package has got you covered. It's a simple and effective way to make your website more efficient and your users happier. Give it a try and see the difference it can make!
There are three ways to use squashify:
As a Node.js script: You can run the command using a Node.js script in your package.json file by specifying the input and output directories.
As a dependency: You can include the package as a dependency in your project, and use it in your code by specifying the input and output directories, and the compression options.
As a command-line script: You can use the script from the command line by running the npx command and following the prompts to enter the input and output directories, and select the compression options for each image format.
To run the squashify command using a Node.js script in your package.json file, you can add the following code to your scripts section:
{
"scripts": {
"squashify": "npx squashify --in ./old --out ./new"
}
}
To use squashify as a dependency in your project, simply include it in your package.json file:
{
"dependencies": {
"squashify": "^1.0.0"
}
}
Then, in your project code, you can import the package and use it as follows:
const { compressImages } = require('squashify');
// Define source and destination directories
const sourceDir = 'path/to/source';
const destinationDir = 'path/to/destination';
// Define options for image compression
const options = {
jpeg: { compressor: 'mozjpeg', quality: 85, progressive: true },
png: { compressor: 'avif', quality: 80 },
svg: { plugins: 'CleanupAttrs, RemoveDoctype' }
};
// Call the compressImages function with the defined parameters
compressImages(sourceDir, destinationDir, options);
This will compress all images in the source directory and save the compressed images to the destination directory, according to the specified compression options.
To use the script, run the following command:
npx squashify --interactive
The script will prompt you to enter the source and destination directory paths. After entering the directories, the script will show the list of image formats available in the source directory. You can choose the compression options for each format.
The following command-line arguments are available:
--in
--out
--config
--verbose: Verbose mode
--extMode: Specify whenever to replace or add image extensions. Options are 'replace' or 'add' (default: 'replace').
--interactive: prompts for required options that aren't provided
--help: Shows the help message.
The script also supports an INI file named .squash
in the project directory. The file should have the following sections and keys:
[path] This section contains the in and out keys, which specify the input and output directories, respectively.
[<format>] This section specifies the compression options for a specific image format, where
[path]
in = src/image
out = images
[options]
extMode = replace
maxSize = 50
resizeMode = contain
[.jpg,.jpeg]
compressor = mozjpeg
quality = 85
progressive = true
# you can use the extension with and without the dot
[png]
compressor = avif
quality = 50
[.gif]
encodeAnimated = true
[.svg]
plugins = cleanupAttrs, removeDoctype, removeXMLProcInst, removeComments, removeMetadata, removeXMLNS, removeEditorsNSData, removeTitle, removeDesc, removeUselessDefs, removeEmptyAttrs, removeHiddenElems, removeEmptyContainers, removeEmptyText, removeUnusedNS, convertShapeToPath, sortAttrs, mergePaths, sortDefsChildren, removeDimensions, removeStyleElement, removeScriptElement, inlineStyles, removeViewBox, removeElementsByAttr, cleanupIDs, convertColors, removeRasterImages, removeUselessStrokeAndFill, removeNonInheritableGroupAttrs,
This is the list of plugins supported by SVGO: https://svgo.dev/docs/plugins/
The script supports the following image formats:
libvips
libwebp
libjpeg
mozjpeg
pngquant
svgo
This script is licensed under the MIT License. See the LICENSE file for more information.