Repository navigation
Expand file tree
/
Copy pathwebpack-image-sizes-plugin.js
More file actions
806 lines (689 loc) · 26.3 KB
/
Copy pathwebpack-image-sizes-plugin.js
File metadata and controls
806 lines (689 loc) · 26.3 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
const fs = require('fs')
const path = require('path')
// Try to require sharp, fallback gracefully if not available
let sharp
try {
sharp = require('sharp')
} catch (error) {
// Note: This warning is shown before plugin instantiation, so silence option doesn't apply here
console.warn('⚠️ Sharp not available. Default image generation will be disabled.')
}
/**
* Webpack plugin that generates image-locations.json and image-sizes.json files
* by parsing JSON files in sizes/ subdirectory and TPL files in tpl/ subdirectory.
*
* @class WebpackImageSizesPlugin
*/
class WebpackImageSizesPlugin {
/**
* Creates an instance of WebpackImageSizesPlugin.
*
* @param {Object} [options={}] - Plugin configuration options
* @param {string} [options.confImgPath='assets/conf-img'] - Path to the conf-img directory
* @param {string} [options.sizesSubdir='sizes'] - Subdirectory containing JSON size files
* @param {string} [options.tplSubdir='tpl'] - Subdirectory containing TPL template files
* @param {string} [options.outputImageLocations='image-locations.json'] - Output filename for image locations
* @param {string} [options.outputImageSizes='image-sizes.json'] - Output filename for image sizes
* @param {boolean} [options.generateDefaultImages=false] - Whether to generate default images
* @param {string} [options.defaultImageSource='src/img/static/default.jpg'] - Path to the source image for generating defaults
* @param {string} [options.defaultImagesOutputDir='dist/images'] - Directory where default images will be generated
* @param {string} [options.defaultImageFormat='jpg'] - Format for generated images (jpg, png, webp, avif)
* @param {boolean} [options.silence=false] - Whether to suppress console output
* @memberof WebpackImageSizesPlugin
*/
constructor(options = {}) {
this.options = {
confImgPath: 'assets/conf-img',
sizesSubdir: 'sizes',
tplSubdir: 'tpl',
outputImageLocations: 'image-locations.json',
outputImageSizes: 'image-sizes.json',
generateDefaultImages: false,
defaultImageSource: 'src/img/static/default.webp',
defaultImagesOutputDir: 'dist/images',
defaultImageFormat: 'webp',
silence: false,
...options,
}
this.hasBeenBuiltOnce = false
}
/**
* Logs a message to the console if silence option is not enabled.
*
* @param {string} level - Log level ('log', 'warn', 'error')
* @param {...any} args - Arguments to pass to console method
* @memberof WebpackImageSizesPlugin
*/
log(level, ...args) {
if (!this.options.silence) {
console[level](...args)
}
}
/**
* Applies the plugin to the webpack compiler.
* Sets up hooks for initial build and watch mode rebuilds.
*
* @param {Object} compiler - The webpack compiler instance
* @memberof WebpackImageSizesPlugin
*/
apply(compiler) {
const { context } = compiler
const confImgPath = path.resolve(context, this.options.confImgPath)
const sizesPath = path.join(confImgPath, this.options.sizesSubdir)
const tplPath = path.join(confImgPath, this.options.tplSubdir)
// Execute on first build and each rebuild
const runGeneration = async (compilation, callback) => {
try {
let hasChanges = false
// Check if there are any changes in the conf-img directory
// Assumes that no modified files means the start of the build (yarn start || yarn build)
if (this.hasBeenBuiltOnce && compilation.modifiedFiles) {
for (const filePath of compilation.modifiedFiles) {
if (filePath.includes(this.options.confImgPath)) {
hasChanges = true
}
}
}
if (this.hasBeenBuiltOnce && !hasChanges) {
this.log('log', `✅ No changes detected in ${this.options.confImgPath}`)
if (callback) {
callback()
}
return
}
this.hasBeenBuiltOnce = true
this.log('log', '🔧 Starting WebpackImageSizesPlugin generation...')
// Check for deleted/renamed files if output files already exist
this.checkForDeletedFiles(confImgPath, sizesPath, tplPath)
// Generate image-locations.json from JSON and TPL files
const imageLocations = this.generateImageLocations(sizesPath, tplPath)
// Generate image-sizes.json from JSON files and TPL files
const imageSizes = this.generateImageSizes(sizesPath, tplPath, imageLocations)
// Clean image-locations.json from internal properties before writing
const cleanedImageLocations = this.cleanImageLocationsForOutput(imageLocations)
// Write output files
const imageLocationsPath = path.join(confImgPath, this.options.outputImageLocations)
const imageSizesPath = path.join(confImgPath, this.options.outputImageSizes)
fs.writeFileSync(imageLocationsPath, JSON.stringify(cleanedImageLocations, null, 2))
fs.writeFileSync(imageSizesPath, JSON.stringify(imageSizes, null, 2))
this.log('log', `✅ Generated ${this.options.outputImageLocations} and ${this.options.outputImageSizes}`)
// Update last generation timestamp
this.lastGenerationTime = Date.now()
// Generate default images if option is enabled
if (this.options.generateDefaultImages) {
await this.generateDefaultImages(context, imageSizes)
}
if (callback) {
callback()
}
} catch (error) {
this.log('error', '❌ Error in WebpackImageSizesPlugin:', error)
if (callback) {
callback(error)
}
}
}
// Hook for initial build only
compiler.hooks.emit.tapAsync('WebpackImageSizesPlugin', runGeneration)
// Hook for rebuilds in watch mode
compiler.hooks.watchRun.tapAsync('WebpackImageSizesPlugin', (compiler, callback) => {
this.log('log', '👀 Watch mode: checking for conf-img changes...')
runGeneration(compiler, callback)
})
// Add directories to watch
compiler.hooks.compilation.tap('WebpackImageSizesPlugin', (compilation) => {
// Watch configuration directories
if (fs.existsSync(sizesPath)) {
compilation.contextDependencies.add(sizesPath)
this.log('log', '📁 Added sizes directory to watch dependencies')
}
if (fs.existsSync(tplPath)) {
compilation.contextDependencies.add(tplPath)
this.log('log', '📁 Added tpl directory to watch dependencies')
}
})
}
/**
* Generates image sizes configuration by parsing JSON files in the sizes directory
* and extracting sizes from TPL files.
*
* @param {string} sizesPath - Path to the sizes directory containing JSON files
* @param {string} tplPath - Path to the tpl directory containing template files
* @param {Array} imageLocations - Generated image locations to extract additional sizes
* @returns {Array} Array containing image sizes configuration object
* @memberof WebpackImageSizesPlugin
*/
generateImageSizes(sizesPath, tplPath, imageLocations) {
// Completely reset image sizes
const imageSizes = [{}]
const allSizes = new Set() // To avoid duplicates
// Check if both directories exist
const sizesExists = fs.existsSync(sizesPath)
const tplExists = fs.existsSync(tplPath)
if (!sizesExists && !tplExists) {
throw new Error(`Both sizes directory (${sizesPath}) and tpl directory (${tplPath}) not found`)
}
if (!sizesExists) {
this.log('warn', `⚠️ Sizes directory not found: ${sizesPath}, continuing with tpl directory only`)
}
// Process JSON files only if sizes directory exists
if (sizesExists) {
const sizeFiles = fs.readdirSync(sizesPath).filter((file) => file.endsWith('.json'))
this.log('log', `📋 Processing ${sizeFiles.length} size files: ${sizeFiles.join(', ')}`)
sizeFiles.forEach((file) => {
try {
const filePath = path.join(sizesPath, file)
const sizesData = JSON.parse(fs.readFileSync(filePath, 'utf8'))
// Convert sizes to image-sizes.json format
sizesData.forEach((size) => {
const sizeKey = `img-${size.width}-${size.height}`
// Avoid duplicates between files
if (!allSizes.has(sizeKey)) {
allSizes.add(sizeKey)
imageSizes[0][sizeKey] = {
width: size.width.toString(),
height: size.height.toString(),
crop: size.crop,
}
}
})
} catch (error) {
this.log('error', `❌ Error parsing ${file}:`, error)
}
})
}
// Extract additional sizes from TPL files that are not in JSON files
this.extractSizesFromTPLFiles(tplPath, imageLocations, imageSizes[0], allSizes)
this.log('log', `📐 Generated ${Object.keys(imageSizes[0]).length} unique image sizes`)
return imageSizes
}
/**
* Extracts image sizes from TPL files and adds them to the sizes configuration.
* This method parses image locations to find sizes that are referenced in TPL files
* but not defined in JSON files.
*
* @param {string} tplPath - Path to the tpl directory
* @param {Array} imageLocations - Generated image locations containing srcsets
* @param {Object} imageSizesObj - Image sizes object to populate
* @param {Set} allSizes - Set of already processed sizes to avoid duplicates
* @memberof WebpackImageSizesPlugin
*/
extractSizesFromTPLFiles(tplPath, imageLocations, imageSizesObj, allSizes) {
if (!fs.existsSync(tplPath) || !imageLocations[0]) {
return
}
// Extract all unique sizes from image locations and collect crop values
const tplSizes = new Set()
const sizeCropMapping = new Map() // Store crop values for each size
Object.values(imageLocations[0]).forEach((locationArray) => {
locationArray.forEach((location) => {
if (location.srcsets) {
location.srcsets.forEach((srcset) => {
if (srcset.size && srcset.size.startsWith('img-')) {
tplSizes.add(srcset.size)
// If srcset has crop info, store it
if (srcset.crop !== undefined) {
sizeCropMapping.set(srcset.size, srcset.crop)
}
}
})
}
})
})
// Also parse TPL files directly to ensure we catch all sizes and their crop values
const tplFiles = fs.readdirSync(tplPath).filter((file) => file.endsWith('.tpl'))
tplFiles.forEach((file) => {
try {
const filePath = path.join(tplPath, file)
const tplContent = fs.readFileSync(filePath, 'utf8')
const locationData = this.parseTPLContent(tplContent)
if (locationData && locationData.srcsets && locationData.sizeCropMap) {
locationData.srcsets.forEach((srcset) => {
if (srcset.size && srcset.size.startsWith('img-')) {
tplSizes.add(srcset.size)
// Store crop value from TPL parsing
if (locationData.sizeCropMap[srcset.size] !== undefined) {
sizeCropMapping.set(srcset.size, locationData.sizeCropMap[srcset.size])
}
}
})
}
} catch (error) {
this.log('error', `❌ Error extracting sizes from TPL ${file}:`, error)
}
})
// Add sizes that are not already defined
tplSizes.forEach((sizeKey) => {
if (!allSizes.has(sizeKey)) {
// Extract width and height from size key (e.g., "img-100-100" -> width: 100, height: 100)
const matches = sizeKey.match(/img-(\d+)-(\d+)/)
if (matches) {
const width = matches[1]
const height = matches[2]
// Use the crop value from TPL parsing, default to true if not found
const cropValue = sizeCropMapping.get(sizeKey) !== undefined ? sizeCropMapping.get(sizeKey) : true
allSizes.add(sizeKey)
imageSizesObj[sizeKey] = {
width: width,
height: height,
crop: cropValue, // Use the actual crop value from TPL
}
this.log('log', `🎨 Added size from TPL: ${sizeKey} (crop: ${cropValue})`)
}
}
})
}
/**
* Parses TPL content to extract image information including srcsets, default_img, and img_base.
*
* @param {string} tplContent - Content of the TPL file
* @returns {Object|null} Parsed location data or null if no valid data found
* @memberof WebpackImageSizesPlugin
*/
parseTPLContent(tplContent) {
// Look for source tags with srcset or data-srcset containing image patterns
let sourceMatches = tplContent.match(/<source[^>]*(?:data-srcset|srcset)="([^"]*)"[^>]*>/g)
if (!sourceMatches || sourceMatches.length === 0) {
// Fallback to simple image pattern extraction
const sizeMatches = tplContent.match(/%%img-(\d+)-(\d+)%%/g)
if (sizeMatches) {
const srcsets = [...new Set(sizeMatches)].map((match) => {
const size = match.replace(/%%/g, '')
return { size, srcset: '' }
})
return { srcsets }
}
return null
}
// Parse all source tags and extract all sizes from all sources
const srcsets = []
const allSizes = new Set() // To avoid duplicates
sourceMatches.forEach((sourceMatch) => {
const srcsetMatch = sourceMatch.match(/(?:data-srcset|srcset)="([^"]*)"/)
if (srcsetMatch) {
const srcsetContent = srcsetMatch[1]
// Extract data-crop attribute value
const cropMatch = sourceMatch.match(/data-crop="([^"]*)"/)
const cropValue = cropMatch ? cropMatch[1] === 'true' : true // Default to true if not specified
// Parse patterns like: %%img-144-144%%, %%img-288-288%% 2x
const imageMatches = srcsetContent.match(/%%img-(\d+)-(\d+)%%(\s+2x)?/g)
if (imageMatches) {
imageMatches.forEach((match) => {
const sizeMatch = match.match(/%%img-(\d+)-(\d+)%%/)
const is2x = match.includes('2x')
if (sizeMatch) {
const width = parseInt(sizeMatch[1])
const height = parseInt(sizeMatch[2])
const size = `img-${width}-${height}`
// Avoid duplicates across different sources
if (!allSizes.has(size)) {
allSizes.add(size)
srcsets.push({
srcset: is2x ? '2x' : '',
size: size,
crop: cropValue, // Store crop value for later use
})
}
}
})
}
}
})
// Check if we found any sizes from all sources
if (srcsets.length === 0) {
return null
}
// All sizes have been processed above, no additional processing needed
// Determine default_img and img_base (use the largest size, typically the 2x version)
const largestSize = srcsets.reduce((largest, current) => {
const currentMatch = current.size.match(/img-(\d+)-(\d+)/)
const largestMatch = largest.size.match(/img-(\d+)-(\d+)/)
if (currentMatch && largestMatch) {
const currentArea = parseInt(currentMatch[1]) * parseInt(currentMatch[2])
const largestArea = parseInt(largestMatch[1]) * parseInt(largestMatch[2])
return currentArea > largestArea ? current : largest
}
return largest
})
const result = {
srcsets: srcsets,
default_img: `default-${largestSize.size.replace('img-', '')}.${this.options.defaultImageFormat}`,
img_base: largestSize.size,
}
return result
}
/**
* Generates image locations configuration by parsing JSON and TPL files.
*
* @param {string} sizesPath - Path to the sizes directory containing JSON files
* @param {string} tplPath - Path to the tpl directory containing template files
* @returns {Array} Array containing image locations configuration object
* @memberof WebpackImageSizesPlugin
*/
generateImageLocations(sizesPath, tplPath) {
// Completely reset image locations
const imageLocations = [{}]
const processedFiles = new Set() // For tracking processed files
// Check if both directories exist
const sizesExists = fs.existsSync(sizesPath)
const tplExists = fs.existsSync(tplPath)
if (!sizesExists && !tplExists) {
throw new Error(`Both sizes directory (${sizesPath}) and tpl directory (${tplPath}) not found`)
}
if (!sizesExists) {
this.log('warn', `⚠️ Sizes directory not found: ${sizesPath}, continuing with tpl directory only`)
}
// Process JSON files in sizes/ first (only if directory exists)
if (sizesExists) {
const sizeFiles = fs.readdirSync(sizesPath).filter((file) => file.endsWith('.json'))
this.log('log', `📋 Processing ${sizeFiles.length} JSON files from sizes/: ${sizeFiles.join(', ')}`)
sizeFiles.forEach((file) => {
try {
const filename = path.basename(file, '.json')
const filePath = path.join(sizesPath, file)
const sizesData = JSON.parse(fs.readFileSync(filePath, 'utf8'))
// Generate srcsets from sizes
const srcsets = sizesData.map((size) => ({
size: `img-${size.width}-${size.height}`,
}))
imageLocations[0][filename] = [
{
srcsets: srcsets,
},
]
processedFiles.add(filename)
} catch (error) {
this.log('error', `❌ Error parsing JSON ${file}:`, error)
}
})
}
// Then process TPL files for unprocessed or missing files
if (tplExists) {
const tplFiles = fs.readdirSync(tplPath).filter((file) => file.endsWith('.tpl'))
this.log('log', `📋 Processing ${tplFiles.length} TPL files: ${tplFiles.join(', ')}`)
tplFiles.forEach((file) => {
try {
const filename = path.basename(file, '.tpl')
const filePath = path.join(tplPath, file)
const tplContent = fs.readFileSync(filePath, 'utf8')
// Process only if not already processed by a JSON file or no JSON match
if (!processedFiles.has(filename)) {
const locationData = this.parseTPLContent(tplContent)
if (locationData && locationData.srcsets && locationData.srcsets.length > 0) {
imageLocations[0][filename] = [locationData]
processedFiles.add(filename)
this.log('log', `📝 Added location from TPL: ${filename}`)
}
}
} catch (error) {
this.log('error', `❌ Error parsing TPL ${file}:`, error)
}
})
}
const totalEntries = Object.keys(imageLocations[0]).length
this.log('log', `📍 Generated ${totalEntries} image locations (${processedFiles.size} files processed)`)
// Log processed files for debugging
if (processedFiles.size > 0) {
this.log('log', `✅ Processed files: ${Array.from(processedFiles).join(', ')}`)
}
return imageLocations
}
/**
* Generates default images from the source image based on image sizes configuration.
*
* @param {string} compilerContext - The webpack compiler context path
* @param {Array} imageSizes - Array containing image sizes configuration
* @memberof WebpackImageSizesPlugin
*/
async generateDefaultImages(compilerContext, imageSizes) {
if (!sharp) {
this.log('warn', '⚠️ Sharp not available. Skipping default image generation.')
return
}
const sourceImagePath = path.resolve(compilerContext, this.options.defaultImageSource)
const outputDir = path.resolve(compilerContext, this.options.defaultImagesOutputDir)
if (!fs.existsSync(sourceImagePath)) {
this.log('warn', `⚠️ Source image not found: ${sourceImagePath}`)
return
}
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true })
}
const sizesObj = imageSizes[0] || {}
const sizeKeys = Object.keys(sizesObj)
const format = this.options.defaultImageFormat.toLowerCase()
// Validate format
const supportedFormats = ['jpg', 'jpeg', 'png', 'webp', 'avif']
if (!supportedFormats.includes(format)) {
this.log('warn', `⚠️ Unsupported format '${format}'. Using 'jpg' instead.`)
this.options.defaultImageFormat = 'jpg'
}
this.log(
'log',
`🖼️ Processing ${sizeKeys.length} default images (${format.toUpperCase()}) from ${this.options.defaultImageSource
}`
)
let generatedCount = 0
let skippedCount = 0
const promises = sizeKeys.map(async (sizeKey) => {
const size = sizesObj[sizeKey]
const width = parseInt(size.width)
const height = parseInt(size.height)
const outputFilename = `default-${width}-${height}.${this.options.defaultImageFormat}`
const outputPath = path.join(outputDir, outputFilename)
// Check if image needs to be generated or updated
if (fs.existsSync(outputPath)) {
try {
// Compare modification times: regenerate if source is newer
const sourceStats = fs.statSync(sourceImagePath)
const outputStats = fs.statSync(outputPath)
if (sourceStats.mtime <= outputStats.mtime) {
this.log('log', `⏭️ Skipped existing: ${outputFilename}`)
skippedCount++
return
} else {
this.log('log', `🔄 Updating outdated: ${outputFilename}`)
}
} catch (error) {
// If we can't compare timestamps, regenerate to be safe
this.log('log', `🔄 Regenerating (timestamp check failed): ${outputFilename}`)
}
}
try {
let sharpInstance = sharp(sourceImagePath)
if (size.crop) {
// Crop and resize to exact dimensions
sharpInstance = sharpInstance.resize(width, height, {
fit: 'cover',
position: 'center',
})
} else {
// Resize maintaining aspect ratio
sharpInstance = sharpInstance.resize(width, height, {
fit: 'inside',
withoutEnlargement: false,
})
}
// Apply format-specific options
const formatOptions = this.getFormatOptions(this.options.defaultImageFormat)
await sharpInstance[formatOptions.method](formatOptions.options).toFile(outputPath)
this.log('log', `✨ Generated: ${outputFilename} (${width}x${height}${size.crop ? ', cropped' : ''})`)
generatedCount++
} catch (error) {
this.log('error', `❌ Error generating ${outputFilename}:`, error.message)
}
})
await Promise.all(promises)
this.log('log', `🎉 Default image generation completed! (${generatedCount} generated, ${skippedCount} skipped)`)
}
/**
* Gets format-specific Sharp options for different image formats.
*
* @param {string} format - The image format (jpg, png, webp, avif)
* @returns {Object} Object containing Sharp method and options
* @memberof WebpackImageSizesPlugin
*/
getFormatOptions(format) {
const formatLower = format.toLowerCase()
const formatConfigs = {
jpg: {
method: 'jpeg',
options: { quality: 80, progressive: true },
},
jpeg: {
method: 'jpeg',
options: { quality: 80, progressive: true },
},
png: {
method: 'png',
options: { quality: 80, progressive: true },
},
webp: {
method: 'webp',
options: { quality: 80, effort: 4 },
},
avif: {
method: 'avif',
options: { quality: 80, effort: 4 },
},
}
return formatConfigs[formatLower] || formatConfigs.jpg
}
/**
* Cleans image locations data by removing internal properties before output.
*
* @param {Array} imageLocations - Image locations data with internal properties
* @returns {Array} Cleaned image locations data
* @memberof WebpackImageSizesPlugin
*/
cleanImageLocationsForOutput(imageLocations) {
return imageLocations.map((locationGroup) => {
const cleanedGroup = {}
Object.keys(locationGroup).forEach((key) => {
cleanedGroup[key] = locationGroup[key].map((location) => {
const cleanedLocation = {
srcsets: location.srcsets.map((srcset) => ({
srcset: srcset.srcset,
size: srcset.size,
// Remove crop property from srcsets
})),
default_img: location.default_img,
img_base: location.img_base,
// Remove sizeCropMap property
}
return cleanedLocation
})
})
return cleanedGroup
})
}
/**
* Determines if generation should be skipped based on file modification times.
*
* @param {string} confImgPath - Path to the conf-img directory
* @param {string} sizesPath - Path to the sizes directory
* @param {string} tplPath - Path to the tpl directory
* @returns {boolean} True if generation should be skipped
* @memberof WebpackImageSizesPlugin
*/
shouldSkipGeneration(confImgPath, sizesPath, tplPath) {
// Skip if never generated before
if (this.lastGenerationTime === 0) {
return false
}
// Check modification times of directories and files
const checkPaths = [
{ path: sizesPath, isDir: true },
{ path: tplPath, isDir: true },
]
for (const { path: checkPath, isDir } of checkPaths) {
if (!fs.existsSync(checkPath)) {
continue
}
try {
if (isDir) {
// Check directory modification time and all files within
const dirStats = fs.statSync(checkPath)
if (dirStats.mtime.getTime() > this.lastGenerationTime) {
return false
}
// Check all files in directory
const files = fs.readdirSync(checkPath)
for (const file of files) {
const filePath = path.join(checkPath, file)
const fileStats = fs.statSync(filePath)
if (fileStats.mtime.getTime() > this.lastGenerationTime) {
return false
}
}
} else {
// Check single file modification time
const stats = fs.statSync(checkPath)
if (stats.mtime.getTime() > this.lastGenerationTime) {
return false
}
}
} catch (error) {
// If we can't check the file, don't skip generation
return false
}
}
return true
}
/**
* Checks for deleted or renamed files by comparing current files with existing configuration.
*
* @param {string} confImgPath - Path to the conf-img directory
* @param {string} sizesPath - Path to the sizes directory
* @param {string} tplPath - Path to the tpl directory
* @memberof WebpackImageSizesPlugin
*/
checkForDeletedFiles(confImgPath, sizesPath, tplPath) {
const imageLocationsPath = path.join(confImgPath, this.options.outputImageLocations)
// Check if image-locations.json file already exists
if (!fs.existsSync(imageLocationsPath)) {
this.log('log', '📄 No existing image-locations.json found, creating fresh files')
return
}
try {
// Read existing file
const existingData = JSON.parse(fs.readFileSync(imageLocationsPath, 'utf8'))
const existingEntries = Object.keys(existingData[0] || {})
// Get current files
const currentSizeFiles = fs.existsSync(sizesPath)
? fs
.readdirSync(sizesPath)
.filter((file) => file.endsWith('.json'))
.map((file) => path.basename(file, '.json'))
: []
const currentTplFiles = fs.existsSync(tplPath)
? fs
.readdirSync(tplPath)
.filter((file) => file.endsWith('.tpl'))
.map((file) => path.basename(file, '.tpl'))
: []
const currentFiles = [...new Set([...currentSizeFiles, ...currentTplFiles])]
// Detect deleted files
const deletedFiles = existingEntries.filter((entry) => !currentFiles.includes(entry))
if (deletedFiles.length > 0) {
this.log('log', `🗑️ Detected ${deletedFiles.length} deleted/renamed files: ${deletedFiles.join(', ')}`)
this.log('log', ' → These entries will be removed from generated files')
}
// Detect new files
const newFiles = currentFiles.filter((file) => !existingEntries.includes(file))
if (newFiles.length > 0) {
this.log('log', `📂 Detected ${newFiles.length} new files: ${newFiles.join(', ')}`)
this.log('log', ' → These entries will be added to generated files')
}
if (deletedFiles.length === 0 && newFiles.length === 0) {
this.log('log', '📋 No file changes detected')
}
} catch (error) {
this.log('warn', '⚠️ Could not read existing image-locations.json:', error.message)
}
}
}
// ----
// export
// ----
module.exports = WebpackImageSizesPlugin