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 f38a9bf7c..9a9b2435f 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: string | undefined; + /** - * 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 */