Resvg dynamic import

Signed-off-by: Joshua Castle <26531652+Kas-tle@users.noreply.github.com>
This commit is contained in:
Joshua Castle
2024-03-24 15:06:29 -07:00
parent 0ca1cf0334
commit 793aefd381
5 changed files with 91 additions and 194 deletions
-1
View File
@@ -9,7 +9,6 @@ export type ImageGeneratorResult = {
export declare class ImageGenerator {
private args;
private satori;
private sharp;
private cache;
private outDir;
constructor(args: {
+3 -3
View File
@@ -9,7 +9,6 @@ const path_1 = tslib_1.__importDefault(require("path"));
class ImageGenerator {
args;
satori;
sharp;
cache = {};
outDir = '';
constructor(args) {
@@ -18,7 +17,6 @@ class ImageGenerator {
}
init = async () => {
this.satori = await import('satori').then((mod) => mod.default);
this.sharp = await import('sharp').then((mod) => mod.default);
if (!fs.existsSync(this.outDir))
await fsp.mkdir(this.outDir, { recursive: true });
};
@@ -30,7 +28,9 @@ class ImageGenerator {
const absolutePath = path_1.default.join(this.outDir, imageName);
const relativePath = path_1.default.join('/', this.args.dir, absolutePath.slice(this.outDir.length));
const svg = await this.satori(element, options);
await this.sharp(Buffer.from(svg)).png().toFile(absolutePath);
const { Resvg } = await import('@resvg/resvg-js').then(module => module);
const pngBuffer = new Resvg(svg).render().asPng();
await fsp.writeFile(absolutePath, pngBuffer);
const url = new URL(this.args.websiteUrl);
url.pathname = relativePath;
this.cache[hash] = {