UNPKG

og-images-generator

Version:

Generate OG images from a static folder and / or a middleware. Extract metadata from HTML pages. No headless browser involved. Comes as a CLI, API or plugins.

31 lines (30 loc) 1.28 kB
import { connectOgImagesGenerator, } from './connect-middleware.js'; import { rollupOgImagesGenerator } from './rollup-plugin.js'; export const applyViteDevServerMiddleware = async (server) => { server.middlewares.use(await connectOgImagesGenerator({ configReloader: (() => server.ssrLoadModule('./og-images.config.js')), })); }; export function viteOgImagesGenerator(options) { let isBuild = false; const rollupPlugin = rollupOgImagesGenerator(options); // HACK: Returns as any to prevent Vite typings mismatches. return { ...rollupPlugin, config(_config, env) { isBuild = env.command === 'build'; }, async closeBundle() { // Guard: only run during an actual production build. // Without this, the hook also fires when an internal // temporary dev server (created to pre-render routes) is closed, // causing duplicate OG image generation. if (!isBuild) return; // @ts-expect-error Unsafe call // eslint-disable-next-line @typescript-eslint/no-unsafe-call await rollupPlugin.closeBundle.call(this); }, configureServer: (server) => applyViteDevServerMiddleware(server), }; }