Skip to content

Commit

Permalink
feat: 🚀 Measure the ASSETS in which errors occurred
Browse files Browse the repository at this point in the history
  • Loading branch information
dc7290 committed May 24, 2022
1 parent 8e1a4d3 commit 4833495
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 69 deletions.
5 changes: 5 additions & 0 deletions __tests__/cli/cache/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -55,6 +57,7 @@ describe('Cache', () => {
cacheDir,
cacheMeasurement,
nonCacheMeasurement,
errorMeasurement,
cliProgressBarIncrement,
originalFilePath: path.join(srcDir, item.src),
...item,
Expand All @@ -71,6 +74,7 @@ describe('Cache', () => {
cacheDir,
cacheMeasurement,
nonCacheMeasurement,
errorMeasurement,
cliProgressBarIncrement,
originalFilePath: path.join(srcDir, item.src),
...item,
Expand All @@ -80,5 +84,6 @@ describe('Cache', () => {

expect(measuredCache).toBe(2)
expect(measuredNonCache).toBe(2)
expect(measuredError).toBe(0)
})
})
148 changes: 79 additions & 69 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type GetOptimizeResultProps = {
cacheDir: string
cacheMeasurement: () => void
nonCacheMeasurement: () => void
errorMeasurement: () => void
cliProgressBarIncrement: () => void
originalFilePath: string
sharpOptions?: Config['sharpOptions']
Expand All @@ -33,6 +34,7 @@ export const getOptimizeResult: GetOptimizeResult = async ({
cacheDir,
cacheMeasurement,
nonCacheMeasurement,
errorMeasurement,
cliProgressBarIncrement,
originalFilePath,
output,
Expand All @@ -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()
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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 ?? {},
Expand All @@ -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'
)
}
}

Expand Down

1 comment on commit 4833495

@vercel
Copy link

@vercel vercel bot commented on 4833495 May 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.