Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function optimizeImageAsync(
projectRoot: string,
inputPath: string,
quality: number
): Promise {
console.log(`\u203A Checking ${chalk.reset.bold(relative(projectRoot, inputPath))}`);
const outputPath = temporary.directory();
await sharpAsync({
input: inputPath,
output: outputPath,
quality,
// https://sharp.pixelplumbing.com/en/stable/api-output/#parameters_4
adaptiveFiltering: true,
});
return join(outputPath, basename(inputPath));
}
const { width, height } = toSize(size);
if (width <= 0 || height <= 0) {
throw Error(`Failed to process image with invalid size: { width: ${width}, height: ${height}}`);
}
const mimeType = mime.getType(icon.src);
if (!mimeType) {
throw new Error(`Invalid mimeType for image with source: ${icon.src}`);
}
const dimensions = `${width}x${height}`;
const fileName = `icon_${dimensions}.${mime.getExtension(mimeType)}`;
let imageBuffer: Buffer | null = await getImageFromCacheAsync(fileName, cacheKey);
if (!imageBuffer) {
// Putting the warning here will prevent the warning from showing if all images were reused from the cache
if (!hasWarned && !(await isAvailableAsync())) {
hasWarned = true;
// TODO: Bacon: Fallback to nodejs image resizing as native doesn't work in the host environment.
console.log('ff', cacheKey, fileName, dimensions);
console.log();
console.log(
chalk.bgYellow.black(
`PWA Images: Using node to generate images. This is much slower than using native packages.`
)
);
console.log(
chalk.yellow(
`- Optionally you can stop the process and try again after successfully running \`npm install -g sharp-cli\`.\n- If you are using \`expo-cli\` to build your project then you could use the \`--no-pwa\` flag to skip the PWA asset generation step entirely.`
)
);
}
imageBuffer = await getBufferWithMimeAsync(icon, mimeType, { width, height });
inputPath: string,
mimeType: string,
width: number,
height: number,
fit: ResizeMode = 'contain',
background: string
): Promise {
if (!(await isAvailableAsync())) {
return await jimpResize(inputPath, mimeType, width, height, fit, background);
}
const format = ensureValidMimeType(mimeType.split('/')[1]);
const outputPath = temporary.directory();
try {
await sharpAsync(
{
input: inputPath,
output: outputPath,
format,
},
[
{
operation: 'flatten',
background,
},
{
operation: 'resize',
width,
height,
fit,
background,
async function resize(
inputPath: string,
mimeType: string,
width: number,
height: number,
fit: ResizeMode = 'contain',
background: string
): Promise {
if (!(await isAvailableAsync())) {
return await jimpResize(inputPath, mimeType, width, height, fit, background);
}
const format = ensureValidMimeType(mimeType.split('/')[1]);
const outputPath = temporary.directory();
try {
await sharpAsync(
{
input: inputPath,
output: outputPath,
format,
},
[
{
operation: 'flatten',
outdated.forEach(outdatedHash => {
delete assetInfo[outdatedHash];
});
const { include, exclude, save } = options;
const quality = options.quality == null ? 80 : options.quality;
const images = include || exclude ? selectedFiles : allFiles;
for (const image of images) {
const hash = hashes[image];
if (assetInfo[hash]) {
continue;
}
if (!(await isAvailableAsync())) {
console.log(
chalk.bold.red(
`\u203A Cannot optimize images without sharp-cli.\n\u203A Run this command again after successfully installing sharp with \`${chalk.magenta`npm install -g sharp-cli`}\``
)
);
return;
}
const { size: prevSize } = statSync(image);
const newName = createNewFilename(image);
const optimizedImage = await optimizeImageAsync(projectRoot, image, quality);
const { size: newSize } = statSync(optimizedImage);
const amountSaved = prevSize - newSize;
if (amountSaved > 0) {
export async function optimizeImageAsync(inputPath: string, quality: number): Promise {
logger.global.info(`Optimizing ${inputPath}`);
const outputPath = temporary.directory();
await sharpAsync({
input: inputPath,
output: outputPath,
quality,
});
return path.join(outputPath, path.basename(inputPath));
}