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.
@@ -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
- }