mirror of
https://github.com/LukeHagar/website.git
synced 2025-12-09 12:57:48 +00:00
fix assets transform in prod
This commit is contained in:
37
scripts/transform-images.js
Normal file
37
scripts/transform-images.js
Normal file
@@ -0,0 +1,37 @@
|
||||
import { existsSync } from 'fs';
|
||||
import { globSync } from 'glob';
|
||||
import path from 'path';
|
||||
import sharp from 'sharp';
|
||||
|
||||
console.log('Transforming images...');
|
||||
|
||||
const inputFormats = ['png', 'jpg', 'svg'];
|
||||
const outputFormats = ['png', 'jpg'];
|
||||
const files = globSync(`./static/assets/**/*.{${inputFormats.join(',')}}`);
|
||||
|
||||
console.log('Found files:', files);
|
||||
|
||||
files.forEach((file) => {
|
||||
const fileName = path.basename(file, path.extname(file));
|
||||
const fileDir = path.dirname(file);
|
||||
|
||||
console.log('Transforming file:', file);
|
||||
|
||||
outputFormats.forEach((format) => {
|
||||
const outputFile = path.join(fileDir, `${fileName}.${format}`);
|
||||
|
||||
if (!existsSync(outputFile)) {
|
||||
sharp(file)
|
||||
.toFormat(format)
|
||||
.toFile(outputFile, (err) => {
|
||||
if (err) {
|
||||
console.error(`Error transforming file ${file} to ${format}:`, err);
|
||||
} else {
|
||||
console.log(`Transformed file ${file} to ${format}`);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.log(`File ${outputFile} already exists. Skipping transformation.`);
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user