-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (94 loc) · 2.93 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import * as path from 'node:path'
import {isSvgFile} from './utilities.js'
import {isSupportedImage, squooshImages} from './squoosh.js'
import optimizeSvg from './svgo.js'
import Cache from './cache.js'
import isSvg from 'is-svg'
const getFileExtensionErrorMessage = (image) =>
`'${image.path ?? image.name}' is not a valid '${path.extname(image.path ?? image.name)}' file.`
const onFileExtensionErrorHandlers = new Map([
[
'error',
(image) => {
throw Object.assign(new Error(getFileExtensionErrorMessage(image)), {
image,
})
},
],
[
'warn',
(image) => {
console.warn(getFileExtensionErrorMessage(image))
},
],
['ignore', () => {}],
])
class ImageMinimizer {
#onFileExtensionError
#cache
constructor(options) {
let onFileExtensionError = options?.onFileExtensionError
const shouldEnableCache = options?.cache !== false
if (
typeof onFileExtensionError === 'string' &&
onFileExtensionErrorHandlers.has(onFileExtensionError)
) {
onFileExtensionError =
onFileExtensionErrorHandlers.get(onFileExtensionError)
}
this.#onFileExtensionError = onFileExtensionError
this.#cache = shouldEnableCache
? new Cache()
: {
getCachedData() {},
updateCache() {},
writeFile() {},
}
}
async process(images) {
const result = new WeakMap()
await this.#minifyWithSquoosh(result, images)
await this.#minifySvg(result, images)
this.#cache.writeFile()
return images.map((file) => result.get(file) || file.content)
}
async #minifyWithSquoosh(result, images) {
images = images.filter((image) => isSupportedImage(image.name))
if (images.length === 0) {
return
}
const compressedImages = await squooshImages(images, {
cache: this.#cache,
onFileExtensionErrorHandlers: this.#onFileExtensionError,
})
for (const [index, image] of images.entries()) {
const compressed = compressedImages[index]
this.#cache.updateCache(image, compressed)
result.set(image, compressed)
}
}
async #minifySvg(result, images) {
images = images.filter((image) => isSvgFile(image.name))
for (const image of images) {
const original = image.content
let compressed = this.#cache.getCachedData(image)
if (!compressed) {
if (this.#onFileExtensionError && !isSvg(String(original))) {
this.#onFileExtensionError(image)
}
compressed = optimizeSvg(original, {multipass: true})
}
this.#cache.updateCache(image, compressed)
result.set(image, compressed)
}
}
}
async function minifyImages(imageOrImages, options) {
const isArray = Array.isArray(imageOrImages)
const imageMinimizer = new ImageMinimizer(options)
const compressed = await imageMinimizer.process(
isArray ? imageOrImages : [imageOrImages],
)
return isArray ? compressed : compressed[0]
}
export default minifyImages