├── .npmrc ├── .gitattributes ├── .gitignore ├── fixtures ├── test.gif ├── test.jpg ├── test.png ├── test-corrupt.jpg └── test.svg ├── .editorconfig ├── .github └── workflows │ └── main.yml ├── license ├── readme.md ├── package.json ├── test.js └── cli.js /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /fixtures/test.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imagemin/imagemin-cli/HEAD/fixtures/test.gif -------------------------------------------------------------------------------- /fixtures/test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imagemin/imagemin-cli/HEAD/fixtures/test.jpg -------------------------------------------------------------------------------- /fixtures/test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imagemin/imagemin-cli/HEAD/fixtures/test.png -------------------------------------------------------------------------------- /fixtures/test-corrupt.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/imagemin/imagemin-cli/HEAD/fixtures/test-corrupt.jpg -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 22 14 | - 18 15 | steps: 16 | - uses: actions/checkout@v4 17 | - uses: actions/setup-node@v4 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Imagemin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # imagemin-cli 2 | 3 | > Minify images 4 | 5 | *Issues with the output should be reported on the `imagemin` [issue tracker](https://github.com/imagemin/imagemin/issues).* 6 | 7 | ## Install 8 | 9 | ```sh 10 | npm install --global imagemin-cli 11 | ``` 12 | 13 | ## Usage 14 | 15 | ``` 16 | $ imagemin --help 17 | 18 | Usage 19 | $ imagemin ... --out-dir=build [--plugin= ...] 20 | $ imagemin > 21 | $ cat | imagemin > 22 | 23 | Options 24 | --plugin, -p Override the default plugins 25 | --out-dir, -o Output directory 26 | 27 | Examples 28 | $ imagemin images/* --out-dir=build 29 | $ imagemin foo.png > foo-optimized.png 30 | $ cat foo.png | imagemin > foo-optimized.png 31 | $ imagemin foo.png --plugin=pngquant > foo-optimized.png 32 | $ imagemin foo.png --plugin.pngquant.quality=0.1 --plugin.pngquant.quality=0.2 > foo-optimized.png 33 | # Non-Windows platforms may support the short CLI syntax for array arguments 34 | $ imagemin foo.png --plugin.pngquant.quality={0.1,0.2} > foo-optimized.png 35 | $ imagemin foo.png --plugin.webp.quality=95 --plugin.webp.preset=icon > foo-icon.webp 36 | ``` 37 | 38 | ## Related 39 | 40 | - [imagemin](https://github.com/imagemin/imagemin) - API for this module 41 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "imagemin-cli", 3 | "version": "8.0.0", 4 | "description": "Minify images seamlessly", 5 | "license": "MIT", 6 | "repository": "imagemin/imagemin-cli", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "type": "module", 9 | "bin": { 10 | "imagemin": "./cli.js" 11 | }, 12 | "engines": { 13 | "node": ">=18" 14 | }, 15 | "scripts": { 16 | "test": "xo && ava" 17 | }, 18 | "files": [ 19 | "cli.js" 20 | ], 21 | "keywords": [ 22 | "cli", 23 | "cli-app", 24 | "compress", 25 | "imagemin", 26 | "gif", 27 | "image", 28 | "jpeg", 29 | "jpg", 30 | "minify", 31 | "png", 32 | "svg" 33 | ], 34 | "dependencies": { 35 | "arrify": "^3.0.0", 36 | "get-stdin": "^9.0.0", 37 | "imagemin": "^9.0.0", 38 | "lodash.pairs": "^3.0.1", 39 | "meow": "^13.2.0", 40 | "ora": "^8.0.1", 41 | "plur": "^5.1.0", 42 | "strip-indent": "^4.0.0", 43 | "uint8array-extras": "^1.1.0" 44 | }, 45 | "devDependencies": { 46 | "ava": "^6.1.2", 47 | "execa": "^8.0.1", 48 | "imagemin-pngquant": "^9.0.2", 49 | "xo": "^0.58.0" 50 | }, 51 | "optionalDependencies": { 52 | "imagemin-gifsicle": "^7.0.0", 53 | "imagemin-jpegtran": "^7.0.0", 54 | "imagemin-optipng": "^8.0.0", 55 | "imagemin-svgo": "^10.0.1" 56 | }, 57 | "ava": { 58 | "workerThreads": false 59 | }, 60 | "xo": { 61 | "rules": { 62 | "n/process-exit-as-throw": 2, 63 | "n/no-unsupported-features": "off" 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import {promisify} from 'node:util'; 2 | import fs from 'node:fs'; 3 | import {dirname} from 'node:path'; 4 | import {fileURLToPath} from 'node:url'; 5 | import process from 'node:process'; 6 | import {execa} from 'execa'; 7 | import test from 'ava'; 8 | 9 | const __dirname = dirname(fileURLToPath(import.meta.url)); 10 | 11 | process.chdir(__dirname); 12 | 13 | const readFile = promisify(fs.readFile); 14 | 15 | test('show version', async t => { 16 | const {stdout} = await execa('./cli.js', ['--version']); 17 | t.is(stdout, JSON.parse(await readFile('./package.json')).version); 18 | }); 19 | 20 | test('optimize a GIF', async t => { 21 | const input = await readFile('fixtures/test.gif'); 22 | 23 | const {stdout} = await execa('./cli.js', { 24 | input, 25 | encoding: 'buffer', 26 | }); 27 | 28 | t.true(stdout.length < input.length); 29 | }); 30 | 31 | test('optimize a JPG', async t => { 32 | const input = await readFile('fixtures/test.jpg'); 33 | 34 | const {stdout} = await execa('./cli.js', { 35 | input, 36 | encoding: 'buffer', 37 | }); 38 | 39 | t.true(stdout.length < input.length); 40 | }); 41 | 42 | test('optimize a PNG', async t => { 43 | const input = await readFile('fixtures/test.png'); 44 | 45 | const {stdout} = await execa('./cli.js', { 46 | input, 47 | encoding: 'buffer', 48 | }); 49 | 50 | t.true(stdout.length < input.length); 51 | }); 52 | 53 | test('optimize a SVG', async t => { 54 | const input = await readFile('fixtures/test.svg'); 55 | 56 | const {stdout} = await execa('./cli.js', { 57 | input, 58 | encoding: 'buffer', 59 | }); 60 | 61 | t.true(stdout.length < input.length); 62 | }); 63 | 64 | test('output error on corrupt images', async t => { 65 | await t.throwsAsync(execa('./cli.js', ['fixtures/test-corrupt.jpg'])); 66 | }); 67 | 68 | test('support plugins', async t => { 69 | const input = await readFile('fixtures/test.png'); 70 | 71 | const {stdout: data} = await execa('./cli.js', ['--plugin=pngquant'], { 72 | input, 73 | encoding: 'buffer', 74 | }); 75 | 76 | const {stdout: compareData} = await execa('./cli.js', { 77 | input, 78 | encoding: 'buffer', 79 | }); 80 | 81 | t.true(data.length < compareData.length); 82 | }); 83 | 84 | test('error when trying to write multiple files to stdout', async t => { 85 | const error = await t.throwsAsync(execa('./cli.js', ['fixtures/test.{jpg,png}'])); 86 | t.is(error.stderr.trim(), 'Cannot write multiple files to stdout, specify `--out-dir`'); 87 | }); 88 | 89 | test('throw on missing plugins', async t => { 90 | const input = await readFile('fixtures/test.png'); 91 | const error = await t.throwsAsync(execa('./cli.js', ['--plugin=unicorn'], { 92 | input, 93 | encoding: 'buffer', 94 | })); 95 | 96 | t.regex(error.stderr.toString(), /Unknown plugin: unicorn/); 97 | }); 98 | 99 | test('support plugin options', async t => { 100 | const input = await readFile('fixtures/test.png'); 101 | 102 | const {stdout: data} = await execa('./cli.js', ['--plugin.pngquant.dithering=1', '--plugin.pngquant.quality=0.1', '--plugin.pngquant.quality=0.4'], { 103 | input, 104 | encoding: 'buffer', 105 | }); 106 | 107 | const {stdout: compareData} = await execa('./cli.js', { 108 | input, 109 | encoding: 'buffer', 110 | }); 111 | 112 | t.true(data.length < compareData.length); 113 | }); 114 | -------------------------------------------------------------------------------- /cli.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | import process from 'node:process'; 3 | import arrify from 'arrify'; 4 | import meow from 'meow'; 5 | import getStdin from 'get-stdin'; 6 | import imagemin from 'imagemin'; 7 | import ora from 'ora'; 8 | import plur from 'plur'; 9 | import stripIndent from 'strip-indent'; 10 | import pairs from 'lodash.pairs'; 11 | import {isUint8Array} from 'uint8array-extras'; 12 | 13 | const cli = meow(` 14 | Usage 15 | $ imagemin ... --out-dir=build [--plugin= ...] 16 | $ imagemin > 17 | $ cat | imagemin > 18 | 19 | Options 20 | --plugin, -p Override the default plugins 21 | --out-dir, -o Output directory 22 | 23 | Examples 24 | $ imagemin images/* --out-dir=build 25 | $ imagemin foo.png > foo-optimized.png 26 | $ cat foo.png | imagemin > foo-optimized.png 27 | $ imagemin foo.png --plugin=pngquant > foo-optimized.png 28 | $ imagemin foo.png --plugin.pngquant.quality=0.1 --plugin.pngquant.quality=0.2 > foo-optimized.png 29 | # Non-Windows platforms may support the short CLI syntax for array arguments 30 | $ imagemin foo.png --plugin.pngquant.quality={0.1,0.2} > foo-optimized.png 31 | $ imagemin foo.png --plugin.webp.quality=95 --plugin.webp.preset=icon > foo-icon.webp 32 | `, { 33 | importMeta: import.meta, 34 | flags: { 35 | plugin: { 36 | type: 'string', 37 | shortFlag: 'p', 38 | isMultiple: true, 39 | default: [ 40 | 'gifsicle', 41 | 'jpegtran', 42 | 'optipng', 43 | 'svgo', 44 | ], 45 | }, 46 | outDir: { 47 | type: 'string', 48 | shortFlag: 'o', 49 | }, 50 | }, 51 | }); 52 | 53 | const requirePlugins = plugins => Promise.all(plugins.map(async ([plugin, options]) => { 54 | try { 55 | const {default: _plugin} = await import(`imagemin-${plugin}`); 56 | return _plugin(options); 57 | } catch { 58 | console.error(stripIndent(` 59 | Unknown plugin: ${plugin} 60 | 61 | Did you forget to install the plugin? 62 | You can install it with: 63 | 64 | $ npm install -g imagemin-${plugin} 65 | `).trim()); 66 | 67 | process.exit(1); 68 | } 69 | })); 70 | 71 | const normalizePluginOptions = plugin => { 72 | const pluginOptionsMap = {}; 73 | 74 | for (const v of arrify(plugin)) { 75 | Object.assign( 76 | pluginOptionsMap, 77 | typeof v === 'object' 78 | ? v 79 | : {[v]: {}}, 80 | ); 81 | } 82 | 83 | return pairs(pluginOptionsMap); 84 | }; 85 | 86 | const run = async (input, {outDir, plugin} = {}) => { 87 | const pluginOptions = normalizePluginOptions(plugin); 88 | const plugins = await requirePlugins(pluginOptions); 89 | const spinner = ora('Minifying images'); 90 | 91 | if (isUint8Array(input)) { 92 | process.stdout.write(await imagemin.buffer(input, {plugins})); 93 | return; 94 | } 95 | 96 | if (outDir) { 97 | spinner.start(); 98 | } 99 | 100 | let files; 101 | try { 102 | files = await imagemin(input, {destination: outDir, plugins}); 103 | } catch (error) { 104 | spinner.stop(); 105 | throw error; 106 | } 107 | 108 | if (!outDir && files.length === 0) { 109 | return; 110 | } 111 | 112 | if (!outDir && files.length > 1) { 113 | console.error('Cannot write multiple files to stdout, specify `--out-dir`'); 114 | process.exit(1); 115 | } 116 | 117 | if (!outDir) { 118 | process.stdout.write(files[0].data); 119 | return; 120 | } 121 | 122 | spinner.stop(); 123 | 124 | console.log(`${files.length} ${plur('image', files.length)} minified`); 125 | }; 126 | 127 | if (cli.input.length === 0 && process.stdin.isTTY) { 128 | console.error('Specify at least one file path'); 129 | process.exit(1); 130 | } 131 | 132 | await run( 133 | cli.input.length > 0 134 | ? cli.input 135 | : await getStdin.buffer(), 136 | cli.flags, 137 | ); 138 | -------------------------------------------------------------------------------- /fixtures/test.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 23 | 25 | 27 | 29 | 31 | 34 | 37 | 40 | 42 | 44 | 47 | 49 | 50 | 51 | 54 | 56 | 58 | 59 | 62 | 67 | 69 | 71 | 73 | 74 | 77 | 79 | 80 | 81 | 82 | 92 | 93 | 94 | 99 | 100 | 101 | 107 | 108 | 109 | 110 | 111 | 113 | 114 | 115 | 121 | 122 | 123 | 127 | 129 | 130 | 132 | 133 | 138 | 142 | 146 | 147 | 149 | 151 | 153 | 155 | 157 | 160 | 163 | 166 | 168 | 170 | 173 | 175 | 176 | 177 | 180 | 182 | 184 | 185 | 190 | 192 | 194 | 196 | 197 | 200 | 202 | 203 | 204 | 205 | 215 | 220 | 221 | 222 | 227 | 228 | 229 | 235 | 236 | 237 | 238 | 239 | 241 | 242 | 243 | 249 | 250 | 251 | 255 | 257 | 259 | 262 | 263 | 265 | 266 | 271 | 275 | 279 | 280 | 281 | 289 | 290 | 292 | 293 | 294 | 295 | --------------------------------------------------------------------------------