├── .gitignore ├── package.json ├── LICENSE ├── index.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Node.js 2 | node_modules/ 3 | 4 | # Logfiles and tempfiles 5 | *.log 6 | /log/* 7 | !/log/.keep 8 | /tmp 9 | 10 | # Other unneeded files 11 | doc/ 12 | *.swp 13 | *~ 14 | .project 15 | .DS_Store 16 | .idea 17 | .secret 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@ixkaito/imagemin", 3 | "version": "0.1.0", 4 | "description": "Minify images seamlessly while preserving directory structure", 5 | "main": "index.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "git+https://github.com/ixkaito/imagemin.git" 9 | }, 10 | "keywords": [ 11 | "imagemin", 12 | "minify", 13 | "compress", 14 | "image", 15 | "images", 16 | "jpeg", 17 | "jpg", 18 | "png", 19 | "gif", 20 | "svg" 21 | ], 22 | "author": "Kite", 23 | "engines": { 24 | "node": ">=8" 25 | }, 26 | "license": "MIT", 27 | "bugs": { 28 | "url": "https://github.com/ixkaito/imagemin/issues" 29 | }, 30 | "homepage": "https://github.com/ixkaito/imagemin#readme", 31 | "dependencies": { 32 | "globby": "^11.0.0", 33 | "imagemin": "^7.0.1" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Kite 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const imagemin = require('imagemin'); 2 | const globby = require('globby'); 3 | const path = require('path'); 4 | 5 | module.exports = async (input = [], options = {}) => { 6 | const patterns = []; 7 | if (Array.isArray(input)) { 8 | input.map(pattern => { 9 | patterns.push(pattern.split(path.sep)); 10 | }); 11 | } else if(typeof input === 'string' || input instanceof String) { 12 | patterns.push(input.split(path.sep)); 13 | } else { 14 | return; 15 | } 16 | 17 | const files = await globby(input); 18 | const { destination } = options; 19 | return Promise.all( 20 | files.map(async file => { 21 | try { 22 | const pathArray = file.split(path.sep); 23 | let dirs = []; 24 | patterns.map(pattern => { 25 | pathArray.map((dir, index) => { 26 | if (dir === pattern[index] && index >= dirs.length) { 27 | dirs.push(dir); 28 | } 29 | }); 30 | }); 31 | const regex = new RegExp(`^${dirs.join(path.sep)}/?`); 32 | const subdir = path.dirname(file.replace(regex, '')); 33 | 34 | return await imagemin([file], { 35 | ...options, 36 | destination: path.join(destination, subdir) 37 | }); 38 | } catch (error) { 39 | error.message = `Error occurred when handling file: ${file}\n\n${error.stack}`; 40 | throw error; 41 | } 42 | }) 43 | ); 44 | }; 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # @ixkaito/imagemin 2 | 3 | > Minify images seamlessly while preserving directory structure 4 | 5 | Based on [`imagemin`](https://github.com/imagemin/imagemin) but preserves directory structure. Inspired by [`imagemin-dir`](https://github.com/adamduncan/imagemin-dir). 6 | 7 | ## Install 8 | 9 | ``` 10 | $ npm install @ixkaito/imagemin 11 | ``` 12 | 13 | ## Usage 14 | 15 | ```js 16 | const imagemin = require('@ixkaito/imagemin'); // alternative to imagemin 17 | const imageminJpegtran = require('imagemin-jpegtran'); 18 | const imageminPngquant = require('imagemin-pngquant'); 19 | 20 | (async () => { 21 | const files = await imagemin(['images/*.{jpg,png}'], { 22 | destination: 'build/images', 23 | plugins: [ 24 | imageminJpegtran(), 25 | imageminPngquant({ 26 | quality: [0.6, 0.8] 27 | }) 28 | ] 29 | }); 30 | 31 | console.log(files); 32 | //=> [{data: , destinationPath: 'build/images/foo.jpg'}, …] 33 | })(); 34 | ``` 35 | 36 | ## Destination Path Examples 37 | 38 | input: `'assets/_images/**/*'` 39 | destination: `'assets/images'` 40 | ``` 41 | assets/_images/foo.jpg -> assets/images/foo.jpg 42 | assets/_images/foo/bar.jpg -> assets/images/foo/bar.jpg 43 | assets/_images/foo/bar/baz.jpg -> assets/images/foo/bar/baz.jpg 44 | ``` 45 | 46 | input: `['source/**/*.{jpg,png,svg}', 'assets/**/*.{jpg,png,svg}']` 47 | destination: `'dist/assets'` 48 | ``` 49 | source/images/foo.jpg -> dist/assets/images/foo.jpg 50 | source/images/foo.png -> dist/assets/images/foo.png 51 | source/svg/foo.svg -> dist/assets/svg/foo.svg 52 | assets/images/bar.jpg -> dist/assets/images/bar.jpg 53 | assets/images/bar.png -> dist/assets/images/bar.png 54 | assets/svg/bar.svg -> dist/assets/svg/bar.svg 55 | ``` 56 | 57 | ## API 58 | 59 | `@ixkaito/imagemin` maintains the same API as `imagemin`. See https://github.com/imagemin/imagemin. 60 | --------------------------------------------------------------------------------