From 4aea702ef0d11571b29ddb56babb478ff036e15b Mon Sep 17 00:00:00 2001 From: kaioduarte Date: Wed, 10 Aug 2022 12:15:23 +0100 Subject: [PATCH 1/2] perf(swagger-module): lazy load swagger-ui-dist module --- lib/swagger-module.ts | 3 ++- lib/swagger-ui/swagger-ui.ts | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/swagger-module.ts b/lib/swagger-module.ts index 5235d913a..68e86376d 100644 --- a/lib/swagger-module.ts +++ b/lib/swagger-module.ts @@ -12,7 +12,7 @@ import { SwaggerScanner } from './swagger-scanner'; import { buildSwaggerHTML, buildSwaggerInitJS, - swaggerAssetsAbsoluteFSPath + getSwaggerAssetsAbsoluteFSPath } from './swagger-ui'; import { assignTwoLevelsDeep } from './utils/assign-two-levels-deep'; import { getGlobalPrefix } from './utils/get-global-prefix'; @@ -44,6 +44,7 @@ export class SwaggerModule { private static serveStatic(finalPath: string, app: INestApplication) { const httpAdapter = app.getHttpAdapter(); + const swaggerAssetsAbsoluteFSPath = getSwaggerAssetsAbsoluteFSPath(); if (httpAdapter && httpAdapter.getType() === 'fastify') { (app as NestFastifyApplication).useStaticAssets({ diff --git a/lib/swagger-ui/swagger-ui.ts b/lib/swagger-ui/swagger-ui.ts index 4c21549b8..aa5e3e0d1 100644 --- a/lib/swagger-ui/swagger-ui.ts +++ b/lib/swagger-ui/swagger-ui.ts @@ -1,4 +1,3 @@ -import * as swaggerUi from 'swagger-ui-dist'; import { favIconHtml, htmlTemplateString, jsTemplateString } from './constants'; import { OpenAPIObject, SwaggerCustomOptions } from '../interfaces'; import { buildJSInitOptions } from './helpers'; @@ -21,10 +20,21 @@ export function buildSwaggerInitJS( return jsTemplateString.replace('<% swaggerOptions %>', jsInitOptions); } +let swaggerAssetsAbsoluteFSPath; + /** - * Stores absolute path to swagger-ui assets + * Returns the absolute path to swagger-ui assets. */ -export const swaggerAssetsAbsoluteFSPath = swaggerUi.getAbsoluteFSPath(); +export function getSwaggerAssetsAbsoluteFSPath() { + if (!swaggerAssetsAbsoluteFSPath) { + // eslint-disable-next-line @typescript-eslint/no-var-requires + const swaggerUi = require('swagger-ui-dist'); + swaggerAssetsAbsoluteFSPath = swaggerUi.getAbsoluteFSPath(); + } + + return swaggerAssetsAbsoluteFSPath; +} + /** * Used to build swagger-ui custom html */ From 857a2d8e85b56d19dd237c400843296aa8809217 Mon Sep 17 00:00:00 2001 From: kaioduarte Date: Wed, 10 Aug 2022 13:14:18 +0100 Subject: [PATCH 2/2] chore: address code review comment --- lib/swagger-ui/swagger-ui.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/swagger-ui/swagger-ui.ts b/lib/swagger-ui/swagger-ui.ts index aa5e3e0d1..812fdf4ba 100644 --- a/lib/swagger-ui/swagger-ui.ts +++ b/lib/swagger-ui/swagger-ui.ts @@ -20,7 +20,7 @@ export function buildSwaggerInitJS( return jsTemplateString.replace('<% swaggerOptions %>', jsInitOptions); } -let swaggerAssetsAbsoluteFSPath; +let swaggerAssetsAbsoluteFSPath: string | undefined; /** * Returns the absolute path to swagger-ui assets.