config-loader: lazy load typescript-json-schema

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2022-03-15 10:14:08 +01:00
parent 14806c1211
commit 664821371e
3 changed files with 16 additions and 3 deletions
+5
View File
@@ -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.
@@ -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
@@ -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,