Skip to content

Commit 43a1d81

Browse files
New functionality
Added the ability to convert images from one specific type to another using an object. Now, when using the usual (in the documentation below - general) parameter objects, the original images are also transmitted as a result optimized.
1 parent c4a2421 commit 43a1d81

File tree

3 files changed

+108
-82
lines changed

3 files changed

+108
-82
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
## What is this
66
### With this thing you can: <br>
7-
- Optimize your images
7+
- Optimize your images.
88
- Convert your images to other formats (including, but not limited to `.webp` and `.avif`).
99
### Features
1010
- Using the [sharp](https://www.npmjs.com/package/sharp) plugin.
@@ -13,7 +13,7 @@
1313

1414
## Why is this
1515

16-
- [imagemin](https://www.npmjs.com/package/imagemin) is unmaintained, [see the issue](https://github.com/imagemin/imagemin/issues/385)
16+
- [imagemin](https://www.npmjs.com/package/imagemin) is unmaintained, [see the issue](https://github.com/imagemin/imagemin/issues/385).
1717
- [gulp-libsquoosh](https://www.npmjs.com/package/gulp-libsquoosh) uses the outdated library [@squoosh/lib](https://www.npmjs.com/package/@squoosh/lib), which does not have support for node > 16.0.0. In addition, the squoosh lib is no longer maintained.
1818
- [@donmahallem/gulp-sharp](https://www.npmjs.com/package/@donmahallem/gulp-sharp) does not have normal documentation.
1919
- I have not found a single plugin that would simultaneously allow you to use the [sharp library](https://www.npmjs.com/package/sharp), convert and optimize images.

index.js

Lines changed: 104 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3,94 +3,120 @@ import sharp from 'sharp'
33
import Vinyl from 'vinyl'
44

55
const ALLOWED_EXTENTIONS = [
6-
'.gif',
7-
'.png',
8-
'.jpg', '.jpeg',
9-
'.webp',
10-
'.avif',
11-
'.tiff',
12-
'.heif',
6+
'.gif',
7+
'.png',
8+
'.jpg', '.jpeg',
9+
'.webp',
10+
'.avif',
11+
'.tiff',
12+
'.heif',
1313
]
1414
const optionsByDefualt = {
15-
quality: 90,
16-
lossless: false,
17-
chromaSubsampling: '4:2:0'
15+
quality: 90,
16+
lossless: false,
17+
chromaSubsampling: '4:2:0'
1818
}
1919

2020

2121
export default function sharpOptimizeImages(options) {
22-
return obj(async function (file, enc, callback) {
23-
if (file.isNull()) {
24-
return callback(null, file)
25-
}
26-
if (ALLOWED_EXTENTIONS.includes(file.extname) == false) {
27-
console.error(`${file.basename} not supported, just copy.`)
28-
29-
return callback(null, file)
30-
}
31-
if (typeof options !== 'object') {
32-
throw new Error('Invalid parameters, they must be an object.')
33-
}
34-
35-
let convertedImages = []
36-
let optionObjects = Object.entries(options)
37-
38-
for (let [optionObjectFormat, optionObjectProps] of optionObjects) {
39-
let convertedFile = await convert(file, optionObjectFormat, optionObjectProps)
40-
convertedImages.push(convertedFile)
41-
}
42-
43-
for (let convertedImage of convertedImages) {
44-
this.push(convertedImage)
45-
}
46-
47-
return callback()
48-
})
22+
return obj(async function (file, enc, callback) {
23+
if (file.isNull()) {
24+
return callback(null, file)
25+
}
26+
if (ALLOWED_EXTENTIONS.includes(file.extname) == false) {
27+
console.error(`${file.basename} not supported, just copy.`)
28+
29+
return callback(null, file)
30+
}
31+
if (typeof options !== 'object') {
32+
throw new Error('Invalid parameters, they must be an object.')
33+
}
34+
35+
let convertedImages = []
36+
let optionObjects = Object.entries(options)
37+
38+
for (let [optionObjectFormat, optionObjectProps] of optionObjects) {
39+
let splitedObjectName = optionObjectFormat.split('_to_')
40+
let convertFromOfGeneralExtname = splitedObjectName[0]
41+
let convertToExtname = splitedObjectName[1]
42+
43+
if (extnamesIsCorrect(convertFromOfGeneralExtname, convertToExtname) == false) {
44+
throw new Error('Invalid name of an object! Make sure you have spelled the extension names correctly.')
45+
}
46+
47+
if (convertToExtname == undefined) {
48+
let optimizedFile = await convert(file, file.extname.replace('.', ''), optionObjectProps)
49+
let convertedFile = await convert(file, convertFromOfGeneralExtname, optionObjectProps)
50+
51+
convertedImages.push(optimizedFile, convertedFile)
52+
}
53+
else if (file.extname == `.${convertFromOfGeneralExtname}`) {
54+
let convertedFile = await convert(file, convertToExtname, optionObjectProps)
55+
convertedImages.push(convertedFile)
56+
}
57+
}
58+
59+
for (let convertedImage of convertedImages) {
60+
this.push(convertedImage)
61+
}
62+
63+
return callback()
64+
})
4965
}
5066

5167
async function convert(file, newFileFormat, options) {
52-
let sharpInstance = sharp(file.contents, { animated: true, limitInputPixels: false, })
53-
54-
switch (newFileFormat) {
55-
case 'gif':
56-
sharpInstance = sharpInstance.gif(Object.assign(optionsByDefualt, options))
57-
break
58-
case 'png':
59-
sharpInstance = sharpInstance.png(Object.assign(optionsByDefualt, options))
60-
break
61-
case 'jpg':
62-
case 'jpeg':
63-
sharpInstance = sharpInstance.jpeg(Object.assign(optionsByDefualt, options))
64-
break
65-
case 'webp':
66-
sharpInstance = sharpInstance.webp(Object.assign(optionsByDefualt, options))
67-
break
68-
case 'tiff':
69-
sharpInstance = sharpInstance.tiff(Object.assign(optionsByDefualt, options))
70-
break
71-
case 'avif':
72-
sharpInstance = sharpInstance.avif(Object.assign(optionsByDefualt, options))
73-
break
74-
case 'heif':
75-
sharpInstance = sharpInstance.heif(Object.assign(optionsByDefualt, options))
76-
break
77-
default:
78-
return false
79-
}
80-
81-
let buffer = await sharpInstance.toBuffer()
82-
return toVinyl(buffer, newFileFormat, file)
68+
let sharpInstance = sharp(file.contents, { animated: true, limitInputPixels: false, })
69+
70+
switch (newFileFormat) {
71+
case 'gif':
72+
sharpInstance = sharpInstance.gif(Object.assign(optionsByDefualt, options))
73+
break
74+
case 'png':
75+
sharpInstance = sharpInstance.png(Object.assign(optionsByDefualt, options))
76+
break
77+
case 'jpg':
78+
case 'jpeg':
79+
sharpInstance = sharpInstance.jpeg(Object.assign(optionsByDefualt, options))
80+
break
81+
case 'webp':
82+
sharpInstance = sharpInstance.webp(Object.assign(optionsByDefualt, options))
83+
break
84+
case 'tiff':
85+
sharpInstance = sharpInstance.tiff(Object.assign(optionsByDefualt, options))
86+
break
87+
case 'avif':
88+
sharpInstance = sharpInstance.avif(Object.assign(optionsByDefualt, options))
89+
break
90+
case 'heif':
91+
sharpInstance = sharpInstance.heif(Object.assign(optionsByDefualt, options))
92+
break
93+
default:
94+
return false
95+
}
96+
97+
let buffer = await sharpInstance.toBuffer()
98+
return toVinyl(buffer, newFileFormat, file)
8399
}
84100

85101
function toVinyl(buffer, newFileFormat, file) {
86-
if (file.extname != '.' + newFileFormat) {
87-
file.extname = '.' + newFileFormat
88-
}
89-
90-
return new Vinyl({
91-
cwd: file.cwd,
92-
base: file.base,
93-
path: file.path,
94-
contents: buffer,
95-
})
102+
let newFileName = file.basename.substr(0, file.basename.lastIndexOf(".")) + `.${newFileFormat}`
103+
let newFilePath = `${file.dirname}\\${newFileName}`
104+
105+
return new Vinyl({
106+
cwd: file.cwd,
107+
base: file.base,
108+
path: newFilePath,
109+
contents: buffer,
110+
})
111+
}
112+
113+
114+
function extnamesIsCorrect(...extnames) {
115+
for (let extname of extnames) {
116+
if (extname && ALLOWED_EXTENTIONS.includes(`.${extname}`) == false) {
117+
return false
118+
} else {
119+
return true
120+
}
121+
}
96122
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"images",
2222
"image",
2323
"compress",
24-
"conventer",
24+
"converter",
2525
"convert",
2626
"png",
2727
"jpg",
@@ -45,4 +45,4 @@
4545
"@types/through2": "^4.0.2",
4646
"@types/vinyl": "^3.0.0"
4747
}
48-
}
48+
}

0 commit comments

Comments
 (0)