Skip to content

Commit 8ad12c8

Browse files
committed
feat(webpack): use byte-for-byte copy for original images in static plugin
Switch from Sharp to fs.copyFile for original assets to ensure they are preserved exactly as-is, maintaining source quality and metadata (EXIF/ICC). Sharp is now used exclusively for generating the WebP derivatives.
1 parent 17492e8 commit 8ad12c8

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

config/webpack-static-images-plugin.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ try {
1010
}
1111

1212
/**
13-
* Webpack plugin to copy and automatically convert static images in a folder to WebP.
13+
* Webpack plugin to copy static images byte-for-byte and generate WebP derivatives with Sharp.
1414
*/
1515
class WebpackStaticImagesPlugin {
1616
/**
@@ -19,7 +19,7 @@ class WebpackStaticImagesPlugin {
1919
* @param {Object} [options={}] - Configuration options
2020
* @param {string} [options.inputDir='src/img/static'] - Input directory
2121
* @param {string} [options.outputDir='dist/images'] - Output directory
22-
* @param {number} [options.quality=80] - WebP compression quality
22+
* @param {number} [options.quality=80] - WebP output quality (originals are not re-encoded)
2323
* @param {boolean} [options.silence=false] - Disable console output
2424
*/
2525
constructor(options = {}) {
@@ -133,15 +133,13 @@ class WebpackStaticImagesPlugin {
133133
const outputOriginal = path.join(outputPath, file)
134134
const outputWebp = path.join(outputPath, `${fileName}.webp`)
135135

136-
// Prepare the Sharp instances.
137-
// (Sharp returns Promises, it is crucial to wait for them.)
138-
const copyPromise = sharp(filePath).toFile(outputOriginal)
136+
// Byte copy preserves source quality, EXIF/ICC, etc. WebP is generated separately.
137+
const copyPromise = fs.promises.copyFile(filePath, outputOriginal)
139138
const webpPromise = sharp(filePath).webp({ quality: this.options.quality }).toFile(outputWebp)
140139

141-
// Group the two actions for this file.
142140
const filePromise = Promise.all([copyPromise, webpPromise])
143141
.then(() => {
144-
this.log('log', ` ✅ Converted: ${file} (+ .webp version)`)
142+
this.log('log', ` ✅ ${file} (original copied, .webp generated)`)
145143
})
146144
.catch((err) => {
147145
this.log('error', ` ❌ Error on ${file}:`, err.message)

0 commit comments

Comments
 (0)