From 013e7f02b5cfcb0baf18f64b51cb99bee5e91913 Mon Sep 17 00:00:00 2001 From: Luca Vogels Date: Sat, 27 Jan 2024 23:21:45 +0100 Subject: [PATCH] Added option to prerender images without calling ImageRequestHandler --- ImageRequestHandler.d.ts | 6 ++++++ ImageRequestHandler.js | 24 +++++++++++++++++++++++- package.json | 2 +- src/ImageRequestHandler.ts | 21 ++++++++++++++++++++- 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/ImageRequestHandler.d.ts b/ImageRequestHandler.d.ts index 56bc08a..7fd0416 100644 --- a/ImageRequestHandler.d.ts +++ b/ImageRequestHandler.d.ts @@ -51,3 +51,9 @@ export type ImageRequestHandlerOptions = ImageConverterOptions & { * @returns Request handler that can be passed e.g. to express */ export declare function ImageRequestHandler(options: ImageRequestHandlerOptions): (req: Request, res: Response, next: NextFunction) => Promise; +/** + * Takes same input as ImageRequestHandler but pre-renders all images. + * @param options Options of request handler (see ImageRequestHandler). + * @returns Promise that resolves when pre-rendering is finished + */ +export declare function PrerenderImages(options: ImageRequestHandlerOptions): Promise; diff --git a/ImageRequestHandler.js b/ImageRequestHandler.js index e3337ae..ee9d02a 100644 --- a/ImageRequestHandler.js +++ b/ImageRequestHandler.js @@ -39,7 +39,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.ImageRequestHandler = void 0; +exports.PrerenderImages = exports.ImageRequestHandler = void 0; var path_1 = __importDefault(require("path")); var lup_root_1 = require("lup-root"); var ImageConverter_1 = require("./ImageConverter"); @@ -135,3 +135,25 @@ function ImageRequestHandler(options) { } exports.ImageRequestHandler = ImageRequestHandler; ; +/** + * Takes same input as ImageRequestHandler but pre-renders all images. + * @param options Options of request handler (see ImageRequestHandler). + * @returns Promise that resolves when pre-rendering is finished + */ +function PrerenderImages(options) { + return __awaiter(this, void 0, void 0, function () { + var optimizer, prerenderStorage; + return __generator(this, function (_a) { + optimizer = new ImageConverter_1.ImageConverter(options); + prerenderStorage = options.prerenderOutputDir !== '' ? new ImageStorage_1.ImageDirectoryStorage({ + dirPath: options.prerenderOutputDir || index_1.OptimizerSettings.DEFAULT_PREDENDER_OUTPUT_DIRECTORY, + }) : new ImageStorage_1.ImageInPlaceStorage(); + return [2 /*return*/, optimizer.prerender(prerenderStorage, options.srcDir, { + recursive: options.prerender, + scaleFactor: options.scaleFactor, + message: options.prerenderMessage, + })]; + }); + }); +} +exports.PrerenderImages = PrerenderImages; diff --git a/package.json b/package.json index 302dc8e..abeb805 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "lup-images", - "version": "1.2.2", + "version": "1.2.3", "description": "Optimizes images for web applications", "files": [ "image/**/*", diff --git a/src/ImageRequestHandler.ts b/src/ImageRequestHandler.ts index dc7321c..d09508f 100644 --- a/src/ImageRequestHandler.ts +++ b/src/ImageRequestHandler.ts @@ -135,4 +135,23 @@ export function ImageRequestHandler(options : ImageRequestHandlerOptions){ next(); } }; -}; \ No newline at end of file +}; + + +/** + * Takes same input as ImageRequestHandler but pre-renders all images. + * @param options Options of request handler (see ImageRequestHandler). + * @returns Promise that resolves when pre-rendering is finished + */ +export async function PrerenderImages(options: ImageRequestHandlerOptions): Promise { + const optimizer = new ImageConverter(options); + const prerenderStorage = options.prerenderOutputDir !== '' ? new ImageDirectoryStorage({ + dirPath: options.prerenderOutputDir || OptimizerSettings.DEFAULT_PREDENDER_OUTPUT_DIRECTORY, + }) : new ImageInPlaceStorage(); + + return optimizer.prerender(prerenderStorage, options.srcDir, { + recursive: options.prerender, + scaleFactor: options.scaleFactor, + message: options.prerenderMessage, + }); +} \ No newline at end of file