From 664821371e7de58c4c903774100163ca274d97d7 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 15 Mar 2022 10:14:08 +0100 Subject: [PATCH] config-loader: lazy load typescript-json-schema Signed-off-by: Patrik Oldsberg --- .changeset/calm-boxes-smell.md | 5 +++++ packages/config-loader/src/lib/schema/collect.test.ts | 3 +++ packages/config-loader/src/lib/schema/collect.ts | 11 ++++++++--- 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 .changeset/calm-boxes-smell.md diff --git a/.changeset/calm-boxes-smell.md b/.changeset/calm-boxes-smell.md new file mode 100644 index 0000000000..55c33997dd --- /dev/null +++ b/.changeset/calm-boxes-smell.md @@ -0,0 +1,5 @@ +--- +'@backstage/config-loader': patch +--- + +The `typescript-json-schema` dependency that is used during schema collection is now lazy loaded, as it eagerly loads in the TypeScript compiler. diff --git a/packages/config-loader/src/lib/schema/collect.test.ts b/packages/config-loader/src/lib/schema/collect.test.ts index 877cbb54e3..2754f788dd 100644 --- a/packages/config-loader/src/lib/schema/collect.test.ts +++ b/packages/config-loader/src/lib/schema/collect.test.ts @@ -28,6 +28,9 @@ const mockSchema = { }, }; +// Gotta make sure this is in the compiler cache before we start mocking the filesystem +require('typescript-json-schema'); + // We need to load in actual TS libraries when using mock-fs. // This lookup is to allow the `typescript` dependency to exist either // at top level or inside node_modules of typescript-json-schema diff --git a/packages/config-loader/src/lib/schema/collect.ts b/packages/config-loader/src/lib/schema/collect.ts index 3ff161cec4..98feffebb2 100644 --- a/packages/config-loader/src/lib/schema/collect.ts +++ b/packages/config-loader/src/lib/schema/collect.ts @@ -22,7 +22,6 @@ import { sep, } from 'path'; import { ConfigSchemaPackageEntry } from './types'; -import { getProgramFromFiles, generateSchema } from 'typescript-json-schema'; import { JsonObject } from '@backstage/types'; import { assertError } from '@backstage/errors'; @@ -149,7 +148,7 @@ export async function collectConfigSchemas( ...packagePaths.map(path => processItem({ name: path, packagePath: path })), ]); - const tsSchemas = compileTsSchemas(tsSchemaPaths); + const tsSchemas = await compileTsSchemas(tsSchemaPaths); return schemas.concat(tsSchemas); } @@ -157,11 +156,17 @@ export async function collectConfigSchemas( // This handles the support of TypeScript .d.ts config schema declarations. // We collect all typescript schema definition and compile them all in one go. // This is much faster than compiling them separately. -function compileTsSchemas(paths: string[]) { +async function compileTsSchemas(paths: string[]) { if (paths.length === 0) { return []; } + // Lazy loaded, because this brings up all of TypeScript and we don't + // want that eagerly loaded in tests + const { getProgramFromFiles, generateSchema } = await import( + 'typescript-json-schema' + ); + const program = getProgramFromFiles(paths, { incremental: false, isolatedModules: true,