-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
295 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<!-- @format --> | ||
|
||
Папка для контентных изображений. | ||
Папка для контентных изображений. Сюда автоматически попадают изображения из папки `raw/images`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
Папка для SVG-иконок, встраиваемых в CSS. | ||
Папка для SVG-иконок, встраиваемых в CSS. Сюда автоматически попадают изображения из папки `raw/icons`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
export default { | ||
js2svg: { | ||
indent: 2, | ||
pretty: true, | ||
}, | ||
multipass: true, | ||
plugins: [ | ||
...[ | ||
"cleanupAttrs", | ||
"cleanupEnableBackground", | ||
"cleanupIds", | ||
"cleanupListOfValues", | ||
"cleanupNumericValues", | ||
"collapseGroups", | ||
"convertColors", | ||
"convertEllipseToCircle", | ||
"convertPathData", | ||
"convertShapeToPath", | ||
"convertStyleToAttrs", | ||
"convertTransform", | ||
"inlineStyles", | ||
"mergePaths", | ||
"mergeStyles", | ||
"minifyStyles", | ||
"moveElemsAttrsToGroup", | ||
"moveGroupAttrsToElems", | ||
"removeComments", | ||
"removeDesc", | ||
"removeDimensions", | ||
"removeDoctype", | ||
"removeEditorsNSData", | ||
"removeEmptyAttrs", | ||
"removeEmptyContainers", | ||
"removeEmptyText", | ||
"removeHiddenElems", | ||
"removeMetadata", | ||
"removeNonInheritableGroupAttrs", | ||
"removeRasterImages", | ||
"removeScriptElement", | ||
"removeStyleElement", | ||
"removeTitle", | ||
"removeUnknownsAndDefaults", | ||
"removeUnusedNS", | ||
"removeUselessDefs", | ||
"removeUselessStrokeAndFill", | ||
"removeXMLProcInst", | ||
"reusePaths", | ||
"sortAttrs", | ||
"sortDefsChildren", | ||
].map((name) => ({ | ||
active: true, | ||
name, | ||
params: { | ||
floatPrecision: 2, | ||
}, | ||
})), | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Image } from "imagescript"; | ||
import { readdir, readFile, rm, writeFile } from "node:fs/promises"; | ||
import path from "node:path"; | ||
import { optimize } from "svgo"; | ||
|
||
import svgoConfig from "../svgo.config.js"; | ||
|
||
export const imageSet = new Set([".jpg", ".png", ".svg"]); | ||
|
||
export const processImage = async (filePath) => { | ||
const filename = path.basename(filePath); | ||
const extname = path.extname(filename); | ||
|
||
if (extname === ".svg") { | ||
const resultName = `${filePath.includes("icons") ? "src/icons" : "public/images"}/${filename}`; | ||
await writeFile( | ||
resultName, | ||
optimize(await readFile(filePath, "utf-8"), svgoConfig).data.replaceAll( | ||
"\\r\\n", | ||
"\\n", | ||
), | ||
).then(() => console.info(`${resultName} created`)); | ||
} else { | ||
const imageData = await readFile(filePath); | ||
const image = await Image.decode(imageData); | ||
const resultName = `public/images/${filename}`.replace(extname, ".webp"); | ||
const promises = [ | ||
writeFile(resultName, await image.encodeWEBP(80)).then(() => | ||
console.info(`${resultName} created`), | ||
), | ||
]; | ||
|
||
// Создаём уменьшенные копии, если в имени есть соотв. retina-индекс | ||
const [, nameBasis, retinaNumber = "1"] = filename.match(/^(.*?)@(\d)x\./); | ||
const retinaIndex = parseInt(retinaNumber, 10); | ||
if (retinaIndex > 1) { | ||
const { height, width } = image; | ||
for (let i = retinaIndex - 1; i >= 1; i--) { | ||
const coefficient = i / retinaIndex; | ||
const resultRetinaName = `public/images/${nameBasis}@${i}x.webp`; | ||
promises.push( | ||
writeFile( | ||
resultRetinaName, | ||
await image | ||
.clone() | ||
.resize(width * coefficient, height * coefficient) | ||
.encodeWEBP(80), | ||
).then(() => console.info(`${resultRetinaName} created`)), | ||
); | ||
} | ||
} | ||
|
||
await Promise.all(promises); | ||
} | ||
|
||
await rm(filePath, { force: true }); | ||
}; | ||
|
||
export const processAllImages = async () => { | ||
const filenames = await readdir("raw", { recursive: true }); | ||
const promises = []; | ||
filenames.forEach((filename) => { | ||
const extname = path.extname(filename); | ||
if ( | ||
(filename.includes("icons") && extname === ".svg") || | ||
(filename.includes("images") && imageSet.has(extname)) | ||
) { | ||
promises.push(processImage(`raw/${filename.replaceAll("\\", "/")}`)); | ||
} | ||
}); | ||
await Promise.all(promises); | ||
}; |
Oops, something went wrong.