gotodev-image-optimizer 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-JJABKWGE.js +705 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +348 -0
- package/dist/vite-plugin-CpGEB8EW.d.ts +63 -0
- package/dist/vite-plugin.d.ts +2 -0
- package/dist/vite-plugin.js +6 -0
- package/package.json +16 -8
- package/src/adaptive/fingerprint.ts +0 -117
- package/src/adaptive/predictive.ts +0 -91
- package/src/adaptive/tier.ts +0 -34
- package/src/components/GImage.tsx +0 -218
- package/src/core/analyzer.ts +0 -189
- package/src/core/encoder.ts +0 -244
- package/src/core/formats.ts +0 -97
- package/src/core/manifest.ts +0 -38
- package/src/core/preprocessor.ts +0 -58
- package/src/core/sanitizer.ts +0 -43
- package/src/core/tuner.ts +0 -68
- package/src/core/types.ts +0 -83
- package/src/core/validator.ts +0 -63
- package/src/index.ts +0 -13
- package/src/utils/entropy.ts +0 -66
- package/src/utils/hash.ts +0 -10
- package/src/utils/ssim.ts +0 -56
- package/src/utils/worker.ts +0 -73
- package/src/vite-plugin.ts +0 -111
package/src/vite-plugin.ts
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import { existsSync, mkdirSync } from 'node:fs'
|
|
2
|
-
import { basename, extname, resolve } from 'node:path'
|
|
3
|
-
import type { Plugin, ResolvedConfig } from 'vite'
|
|
4
|
-
import { encodeImage } from './core/encoder.ts'
|
|
5
|
-
import { addToManifest, createManifest, writeManifest } from './core/manifest.ts'
|
|
6
|
-
import type { PluginOptions, QualityTier, TierConfig } from './core/types.ts'
|
|
7
|
-
|
|
8
|
-
const IMAGE_EXTENSIONS = /\.(jpe?g|png|webp|avif|gif|svg|bmp|tiff?|ico)$/i
|
|
9
|
-
|
|
10
|
-
const DEFAULT_TIERS: Record<QualityTier, TierConfig> = {
|
|
11
|
-
ultra: { quality: 90, widths: [480, 768, 1024, 1920] },
|
|
12
|
-
high: { quality: 80, widths: [480, 768, 1024] },
|
|
13
|
-
medium: { quality: 60, widths: [480, 768] },
|
|
14
|
-
low: { quality: 40, widths: [480] },
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export default function gotodevImageOptimizer(userOptions: PluginOptions = {}): Plugin {
|
|
18
|
-
let config: ResolvedConfig
|
|
19
|
-
let manifest = createManifest()
|
|
20
|
-
const options: PluginOptions = {
|
|
21
|
-
tiers: { ...DEFAULT_TIERS, ...userOptions.tiers },
|
|
22
|
-
adaptive: userOptions.adaptive ?? true,
|
|
23
|
-
autoTune: userOptions.autoTune ?? true,
|
|
24
|
-
preprocess: userOptions.preprocess ?? true,
|
|
25
|
-
faceDetection: userOptions.faceDetection ?? true,
|
|
26
|
-
formats: userOptions.formats ?? ['avif', 'webp', 'jpeg'],
|
|
27
|
-
maxFileSize: userOptions.maxFileSize ?? 50 * 1024 * 1024,
|
|
28
|
-
verbose: userOptions.verbose ?? false,
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return {
|
|
32
|
-
name: 'gotodev-image-optimizer',
|
|
33
|
-
enforce: 'post',
|
|
34
|
-
|
|
35
|
-
configResolved(resolved: ResolvedConfig) {
|
|
36
|
-
config = resolved
|
|
37
|
-
},
|
|
38
|
-
|
|
39
|
-
async buildStart() {
|
|
40
|
-
manifest = createManifest()
|
|
41
|
-
},
|
|
42
|
-
|
|
43
|
-
async transform(_code: string, id: string) {
|
|
44
|
-
if (!IMAGE_EXTENSIONS.test(id)) return
|
|
45
|
-
if (id.includes('node_modules')) return
|
|
46
|
-
|
|
47
|
-
const outDir = resolve(config.root, config.build.outDir ?? 'dist', 'assets')
|
|
48
|
-
if (!existsSync(outDir)) {
|
|
49
|
-
mkdirSync(outDir, { recursive: true })
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
const tiers = options.tiers as Record<QualityTier, TierConfig>
|
|
53
|
-
|
|
54
|
-
const formats = options.formats ?? (['avif', 'webp', 'jpeg'] as const)
|
|
55
|
-
|
|
56
|
-
try {
|
|
57
|
-
const entry = await encodeImage(id, {
|
|
58
|
-
widths: tiers.high.widths,
|
|
59
|
-
formats: [...formats],
|
|
60
|
-
tiers,
|
|
61
|
-
autoTune: options.autoTune ?? true,
|
|
62
|
-
adaptive: options.adaptive ?? true,
|
|
63
|
-
preprocess: options.preprocess ?? true,
|
|
64
|
-
faceDetection: options.faceDetection ?? true,
|
|
65
|
-
outDir,
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
const key = basename(id)
|
|
69
|
-
addToManifest(manifest, key, entry)
|
|
70
|
-
|
|
71
|
-
if (options.verbose) {
|
|
72
|
-
console.log(`[gotodev-image-optimizer] Optimized: ${key}`)
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const manifestData = JSON.stringify(entry)
|
|
76
|
-
const manifestPath = resolve(outDir, 'gimage-manifest.json')
|
|
77
|
-
writeManifest(manifest, manifestPath)
|
|
78
|
-
|
|
79
|
-
return {
|
|
80
|
-
code: `export default ${manifestData};`,
|
|
81
|
-
map: null,
|
|
82
|
-
}
|
|
83
|
-
} catch (error) {
|
|
84
|
-
if (options.verbose) {
|
|
85
|
-
console.error(`[gotodev-image-optimizer] Failed to optimize ${id}:`, error)
|
|
86
|
-
}
|
|
87
|
-
return {
|
|
88
|
-
code: `export default ${JSON.stringify({
|
|
89
|
-
src: basename(id),
|
|
90
|
-
width: 0,
|
|
91
|
-
height: 0,
|
|
92
|
-
format: extname(id).slice(1),
|
|
93
|
-
placeholder: '',
|
|
94
|
-
variants: [],
|
|
95
|
-
tiers: {},
|
|
96
|
-
})};`,
|
|
97
|
-
map: null,
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
},
|
|
101
|
-
|
|
102
|
-
closeBundle() {
|
|
103
|
-
const outDir = resolve(config.root, config.build.outDir ?? 'dist', 'assets')
|
|
104
|
-
if (!existsSync(outDir)) {
|
|
105
|
-
mkdirSync(outDir, { recursive: true })
|
|
106
|
-
}
|
|
107
|
-
const manifestPath = resolve(outDir, 'gimage-manifest.json')
|
|
108
|
-
writeManifest(manifest, manifestPath)
|
|
109
|
-
},
|
|
110
|
-
}
|
|
111
|
-
}
|