Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 25 additions & 13 deletions scripts/sharp-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const SHARP_OPTIONS_TYPE_MAPPER = {
jpeg: 'jpeg',
webp: 'webp',
avif: 'avif',
gif: 'gif',
gif: 'webp',
} as const satisfies { [key: string]: SharpOptionType }
export type SharpFileType = keyof typeof SHARP_OPTIONS_TYPE_MAPPER

Expand Down Expand Up @@ -93,7 +93,15 @@ export const sharpImages = async () => {
const sharpOptionType = SHARP_OPTIONS_TYPE_MAPPER[fileType]
const sharpOption = SHARP_OPTIONS[sharpOptionType]

const sharpedFilePath = filePath.replace(`.${fileType}`, `.sharp.${fileType}`)
const metadata = await sharp(filePath).metadata()
const isAnimatedWebp = fileType === 'webp' && metadata.pages && metadata.pages > 1

if (isAnimatedWebp) {
console.log(`::✧:: Skipping animated WebP file ${filename}`)
continue
}

const sharpedFilePath = filePath.replace(`.${fileType}`, `.sharp.${sharpOptionType}`)

await sharp(filePath, fileType === 'gif' ? { animated: true } : {})
[sharpOptionType](sharpOption)
Expand All @@ -111,14 +119,10 @@ export const sharpImages = async () => {
}

if (processedResult.percentChange > 0) {
await fs.writeFile(filePath, await fs.readFile(sharpedFilePath))

const avifPath = filePath.replace(`.${fileType}`, '.avif')
try {
await sharp(filePath, fileType === 'gif' ? { animated: true } : {})
.avif(SHARP_OPTIONS.avif)
.toFile(avifPath)

if (fileType !== 'gif') {
await fs.writeFile(filePath, await fs.readFile(sharpedFilePath))
const avifPath = filePath.replace(`.${fileType}`, '.avif')
await sharp(filePath).avif(SHARP_OPTIONS.avif).toFile(avifPath)
const avifStats = await fs.stat(avifPath)

if (avifStats.size < afterStats.size) {
Expand All @@ -132,16 +136,24 @@ export const sharpImages = async () => {
} else {
await unlink(avifPath)
}
} catch (error) {
console.log('::error:: Failed to convert to AVIF:', error)
}

if (fileType === 'gif') {
const webpPath = filePath.replace('.gif', '.webp')
await fs.rename(sharpedFilePath, webpPath)
const webpFilename = path.basename(webpPath)
mdxUpdates += await updateMdxReferences(filename, webpFilename)
await unlink(filePath)
}

sharpedImageList.push(processedResult)
} else {
unSharpedImageList.push(processedResult)
}

await unlink(sharpedFilePath)
if (fileType !== 'gif') {
await unlink(sharpedFilePath)
}
} catch (error) {
console.log('::error::', error)
}
Expand Down
2 changes: 1 addition & 1 deletion src/content/post/blog/cubeit-intranet-retrospect.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ SQLite3λŠ” μ„ νƒμ‚¬ν•­μ΄μ—ˆμ§€λ§Œ, κΉ€λ―Όνƒœ κ°•μ‚¬λ‹˜κ»˜μ„œ SQL 문을 μ‚¬μš©
λ””μžμΈμ΄ λ‚˜μ™€μ•Ό κ°œλ°œμ„ μ‹œμž‘ν•  수 μžˆμ–΄μ„œ 레퍼런슀λ₯Ό ν™œμš©ν•΄ 주말에 λΉ λ₯΄κ²Œ μž‘μ—…ν–ˆλ‹€.
μ‘°μž₯λ‹˜(μ§„μ§œ λ””μžμ΄λ„ˆ)이 λ ˆνΌλŸ°μŠ€λ„ κ΅¬ν•΄μ£Όμ‹œκ³  ν”Όλ“œλ°±λ„ λΉ λ₯΄κ²Œ μ£Όμ…”μ„œ μ’€ 더 μˆ˜μ›”ν•˜κ²Œ μž‘μ—…ν•  수 μžˆμ—ˆλ‹€.

![λ°˜μ‘ν˜•μœΌλ‘œ κ΅¬ν˜„ν•œ λͺ¨μŠ΅](/images/cubeit-intranet-retrospect/mediaquery.avif)
![λ°˜μ‘ν˜•μœΌλ‘œ κ΅¬ν˜„ν•œ λͺ¨μŠ΅](/images/cubeit-intranet-retrospect/mediaquery.webp)

## μ—­ν• 

Expand Down
Loading