From 4833495fa91a5e839b1ae12b7464efe55180e08e Mon Sep 17 00:00:00 2001 From: dc7290 Date: Tue, 24 May 2022 17:44:27 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=9A=80=20Measure=20the=20ASSETS?= =?UTF-8?q?=20in=20which=20errors=20occurred?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __tests__/cli/cache/index.test.ts | 5 + src/cli/index.ts | 148 ++++++++++++++++-------------- 2 files changed, 84 insertions(+), 69 deletions(-) diff --git a/__tests__/cli/cache/index.test.ts b/__tests__/cli/cache/index.test.ts index bd883ede..e766678d 100644 --- a/__tests__/cli/cache/index.test.ts +++ b/__tests__/cli/cache/index.test.ts @@ -37,10 +37,12 @@ describe('Cache', () => { let measuredCache = 0 let measuredNonCache = 0 + let measuredError = 0 const destDir = path.resolve(__dirname, 'results') const cacheMeasurement = () => (measuredCache += 1) const nonCacheMeasurement = () => (measuredNonCache += 1) + const errorMeasurement = () => (measuredError += 1) const cliProgressBarIncrement = () => undefined const srcDir = path.resolve(__dirname, 'fixtures') @@ -55,6 +57,7 @@ describe('Cache', () => { cacheDir, cacheMeasurement, nonCacheMeasurement, + errorMeasurement, cliProgressBarIncrement, originalFilePath: path.join(srcDir, item.src), ...item, @@ -71,6 +74,7 @@ describe('Cache', () => { cacheDir, cacheMeasurement, nonCacheMeasurement, + errorMeasurement, cliProgressBarIncrement, originalFilePath: path.join(srcDir, item.src), ...item, @@ -80,5 +84,6 @@ describe('Cache', () => { expect(measuredCache).toBe(2) expect(measuredNonCache).toBe(2) + expect(measuredError).toBe(0) }) }) diff --git a/src/cli/index.ts b/src/cli/index.ts index 3e12f06a..eef9e042 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -20,6 +20,7 @@ type GetOptimizeResultProps = { cacheDir: string cacheMeasurement: () => void nonCacheMeasurement: () => void + errorMeasurement: () => void cliProgressBarIncrement: () => void originalFilePath: string sharpOptions?: Config['sharpOptions'] @@ -33,6 +34,7 @@ export const getOptimizeResult: GetOptimizeResult = async ({ cacheDir, cacheMeasurement, nonCacheMeasurement, + errorMeasurement, cliProgressBarIncrement, originalFilePath, output, @@ -42,66 +44,72 @@ export const getOptimizeResult: GetOptimizeResult = async ({ sharpOptions, }) => { if (formatValidate(extension)) { - const filePath = path.join(destDir, output) - const fileDir = filePath.split('/').slice(0, -1).join('/') - fs.mkdirSync(fileDir, { recursive: true }) - - const outputPath = path.join(cacheDir, output) - const outputDir = outputPath.split('/').slice(0, -1).join('/') - fs.mkdirSync(outputDir, { recursive: true }) - - const imageBuffer = fs.readFileSync(originalFilePath) - - // Cache process - if (!noCache) { - const cacheImagesFindIndex = cacheImages.findIndex((cacheImage) => cacheImage.output === output) - const hash = createHash('sha256').update(imageBuffer).digest('hex') - - if (cacheImagesFindIndex === -1) { - cacheImages.push({ output, hash }) - } else { - const currentCacheImage = cacheImages[cacheImagesFindIndex] - if (currentCacheImage?.hash === hash) { - fs.copyFileSync(outputPath, filePath) - cacheMeasurement() - cliProgressBarIncrement() - return + try { + const filePath = path.join(destDir, output) + const fileDir = filePath.split('/').slice(0, -1).join('/') + fs.mkdirSync(fileDir, { recursive: true }) + + const outputPath = path.join(cacheDir, output) + const outputDir = outputPath.split('/').slice(0, -1).join('/') + fs.mkdirSync(outputDir, { recursive: true }) + + const imageBuffer = fs.readFileSync(originalFilePath) + + // Cache process + if (!noCache) { + const cacheImagesFindIndex = cacheImages.findIndex((cacheImage) => cacheImage.output === output) + const hash = createHash('sha256').update(imageBuffer).digest('hex') + + if (cacheImagesFindIndex === -1) { + cacheImages.push({ output, hash }) } else { - if (currentCacheImage !== undefined) currentCacheImage.hash = hash + const currentCacheImage = cacheImages[cacheImagesFindIndex] + if (currentCacheImage?.hash === hash) { + fs.copyFileSync(outputPath, filePath) + cacheMeasurement() + cliProgressBarIncrement() + return + } else { + if (currentCacheImage !== undefined) currentCacheImage.hash = hash + } } } - } - const image = sharp(imageBuffer, { sequentialRead: true }) - - image.rotate().resize({ width, withoutEnlargement: true }) - - switch (extension) { - case 'jpeg': - await image.jpeg({ quality, ...sharpOptions?.jpg }).toFile(outputPath) - break - case 'jpg': - await image.jpeg({ quality, ...sharpOptions?.jpg }).toFile(outputPath) - break - case 'png': - await image.png({ quality, ...sharpOptions?.png }).toFile(outputPath) - break - case 'webp': - await image.webp({ quality, ...sharpOptions?.webp }).toFile(outputPath) - break - case 'avif': - await image.avif({ quality, ...sharpOptions?.avif }).toFile(outputPath) - break - } + const image = sharp(imageBuffer, { sequentialRead: true }) + + image.rotate().resize({ width, withoutEnlargement: true }) + + switch (extension) { + case 'jpeg': + await image.jpeg({ quality, ...sharpOptions?.jpg }).toFile(outputPath) + break + case 'jpg': + await image.jpeg({ quality, ...sharpOptions?.jpg }).toFile(outputPath) + break + case 'png': + await image.png({ quality, ...sharpOptions?.png }).toFile(outputPath) + break + case 'webp': + await image.webp({ quality, ...sharpOptions?.webp }).toFile(outputPath) + break + case 'avif': + await image.avif({ quality, ...sharpOptions?.avif }).toFile(outputPath) + break + } - fs.copyFileSync(outputPath, filePath) + fs.copyFileSync(outputPath, filePath) - nonCacheMeasurement() - cliProgressBarIncrement() + nonCacheMeasurement() + cliProgressBarIncrement() + } catch (error) { + console.warn(error) + errorMeasurement() + } } else { - throw Error( - `Not an allowed format.\`${extension}\`\nGive \`unoptimize\`prop to /next/image to disable optimization.` + console.warn( + `${originalFilePath}: Not an allowed format.\`${extension}\`\nGive \`unoptimize\`prop to /next/image to disable optimization.` ) + errorMeasurement() } } @@ -139,6 +147,11 @@ export const optimizeImages = async ({ manifestJsonPath, noCache, config, terse let measuredCache = 0 let measuredNonCache = 0 + let measuredError = 0 + + const cacheMeasurement = () => (measuredCache += 1) + const nonCacheMeasurement = () => (measuredNonCache += 1) + const errorMeasurement = () => (measuredError += 1) for (const item of manifest) { const originalFilePath = path.join(destDir, item.src) @@ -149,8 +162,9 @@ export const optimizeImages = async ({ manifestJsonPath, noCache, config, terse noCache, cacheImages, cacheDir: defaultCacheDir, - cacheMeasurement: () => (measuredCache += 1), - nonCacheMeasurement: () => (measuredNonCache += 1), + cacheMeasurement, + nonCacheMeasurement, + errorMeasurement, cliProgressBarIncrement: terse ? () => undefined : cliProgressBarIncrement, originalFilePath, sharpOptions: config.sharpOptions ?? {}, @@ -159,23 +173,19 @@ export const optimizeImages = async ({ manifestJsonPath, noCache, config, terse ) } - try { - await Promise.all(promises) + await Promise.all(promises) - if (!noCache) { - writeCacheManifest(cacheImages) - } + if (!noCache) { + writeCacheManifest(cacheImages) + } - if (!terse) { - // eslint-disable-next-line no-console - console.log( - `Cache assets: ${measuredCache}, NonCache assets: ${measuredNonCache}\n`, - '\x1b[35m\nSuccessful optimization!', - '\x1b[39m' - ) - } - } catch (error) { - console.error('Error processing files', error) + if (!terse) { + // eslint-disable-next-line no-console + console.log( + `Cache assets: ${measuredCache}, NonCache assets: ${measuredNonCache}, Error assets: ${measuredError}\n`, + '\x1b[35m\nSuccessful optimization!', + '\x1b[39m' + ) } }