Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for resizing images #96

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 11 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { promises as fsPromises } from 'fs'
const { readFile } = fsPromises
import yaml from 'js-yaml'
import { OutputOptions, PngOptions, JpegOptions, WebpOptions } from 'sharp'
import { PngOptions, JpegOptions, WebpOptions } from 'sharp'

const {
ImageKind,
CONFIG_PATH,
JPEG_QUALITY,
JPEG_PROGRESSIVE,
Expand All @@ -14,12 +13,20 @@ const {
COMPRESS_ONLY
} = require('./constants')

interface SizeOptions {
[key: string]: {
width?: number
height?: number
}
}

interface Config {
compressOnly: boolean
jpeg: JpegOptions
png: PngOptions
webp: WebpOptions
ignorePaths: string[]
size: SizeOptions
}

// Deprecated configuration method
Expand All @@ -38,7 +45,8 @@ const getConfig = async () => {
png: { quality: PNG_QUALITY },
webp: { quality: WEBP_QUALITY },
ignorePaths: IGNORE_PATHS,
compressOnly: COMPRESS_ONLY
compressOnly: COMPRESS_ONLY,
size: {}
}

const ymlConfig = await getYamlConfig()
Expand Down
57 changes: 49 additions & 8 deletions src/image-processing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,56 @@ const processImages = async (): Promise<ProcessedImagesResult> => {
console.log('::debug:: === Sharp library info ===')

const config = await getConfig()

const getMatchedPathsByGlobs = async (pattern: string) => {
const imagePaths = await glob(pattern, {
ignore: config.ignorePaths.map((p: string) =>
path.resolve(REPO_DIRECTORY, p)
),
nodir: true,
follow: false,
dot: true
})

return imagePaths;
}

const globPaths = `${REPO_DIRECTORY}/**/*.{${FILE_EXTENSIONS_TO_PROCESS.join(
','
)}}`

const imagePaths = await glob(globPaths, {
ignore: config.ignorePaths.map((p: string) =>
path.resolve(REPO_DIRECTORY, p)
),
nodir: true,
follow: false,
dot: true
})
const imagePaths = await getMatchedPathsByGlobs(globPaths)

/**
* Get all files which requires resizing
*
* Output:
*
* {
* "/tests/test-images/icon.png": { width: 100, height: 200 } // Sizes can be defined in config file
* }
*/
const imagesToBeResized = await Object.keys(config.size).reduce(async (agg: any, pathProvided: string) => {
const globPath = FILE_EXTENSIONS_TO_PROCESS.find(extension => pathProvided.includes(`.${extension}`)) ? pathProvided : `${pathProvided}/*.{${FILE_EXTENSIONS_TO_PROCESS.join(
','
)}}`;

const paths = await getMatchedPathsByGlobs(globPath);

if (paths.length === 0) {
return agg;
}

const imagePathWithSize = paths.reduce((agg: any, matchedPath: string) => ({
...agg,
[matchedPath]: config.size[globPath]
}), {})

return {
...agg,
...imagePathWithSize
}
}, {})

const optimisedImages: ProcessedImage[] = []
const unoptimisedImages: ProcessedImage[] = []
Expand All @@ -46,8 +84,11 @@ const processImages = async (): Promise<ProcessedImagesResult> => {
const options = config[sharpFormat]
const beforeStats = (await stat(imgPath)).size

const customSize = imagesToBeResized[imgPath];

try {
const { data, info } = await sharp(imgPath)
.resize(customSize || {})
.toFormat(sharpFormat, options)
.toBuffer({ resolveWithObject: true })

Expand Down