Skip to content

Commit f2f5182

Browse files
committed
chore: reduce bundle size
1 parent d8b30e6 commit f2f5182

File tree

7 files changed

+78
-32
lines changed

7 files changed

+78
-32
lines changed

packages/cli/src/commands/stats.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ export async function handleStats({ argv, config, collectSpecData }: CommandArgs
9898
const { bundle: document } = await bundle({ config, ref: path });
9999
collectSpecData?.(document.parsed);
100100
const specVersion = detectSpec(document.parsed);
101-
const types = normalizeTypes(config.extendTypes(getTypes(specVersion), specVersion), config);
101+
const types = normalizeTypes(
102+
config.extendTypes(await getTypes(specVersion), specVersion),
103+
config
104+
);
102105

103106
const startedAt = performance.now();
104107
const ctx: WalkContext = {

packages/core/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
"require": "./lib/index.js",
1111
"types": "./lib/index.d.ts"
1212
},
13+
"./resolve": {
14+
"import": "./lib/resolve.js",
15+
"require": "./lib/resolve.js",
16+
"types": "./lib/resolve.d.ts"
17+
},
1318
"./lib/config/config": {
1419
"import": "./lib/config/index.js",
1520
"require": "./lib/config/index.js",

packages/core/src/__tests__/lint.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@ describe('lint', () => {
11601160
it('lintConfig should alternate its behavior when supplied externalConfigTypes', async () => {
11611161
const config = await createConfig(testPortalConfigContent);
11621162
const results = await lintConfig({
1163-
externalConfigTypes: createConfigTypes(
1163+
externalConfigTypes: await createConfigTypes(
11641164
{
11651165
type: 'object',
11661166
properties: {

packages/core/src/bundle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export async function bundleDocument(opts: {
167167
const specMajorVersion = getMajorSpecVersion(specVersion);
168168
const rules = config.getRulesForSpecVersion(specMajorVersion);
169169
const types = normalizeTypes(
170-
config.extendTypes(customTypes ?? getTypes(specVersion), specVersion),
170+
config.extendTypes(customTypes ?? (await getTypes(specVersion)), specVersion),
171171
config
172172
);
173173

packages/core/src/lint.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export async function lintDocument(opts: {
7676
const specMajorVersion = getMajorSpecVersion(specVersion);
7777
const rules = config.getRulesForSpecVersion(specMajorVersion);
7878
const types = normalizeTypes(
79-
config.extendTypes(customTypes ?? getTypes(specVersion), specVersion),
79+
config.extendTypes(customTypes ?? (await getTypes(specVersion)), specVersion),
8080
config
8181
);
8282

@@ -143,7 +143,7 @@ export async function lintConfig(opts: {
143143
};
144144

145145
const types = normalizeTypes(
146-
opts.externalConfigTypes || createConfigTypes(rootRedoclyConfigSchema, config)
146+
opts.externalConfigTypes || (await createConfigTypes(rootRedoclyConfigSchema, config))
147147
);
148148

149149
const rules: (RuleInstanceConfig & {

packages/core/src/oas-types.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
import { Oas2Types } from './types/oas2.js';
2-
import { Oas3Types } from './types/oas3.js';
3-
import { Oas3_1Types } from './types/oas3_1.js';
4-
import { Oas3_2Types } from './types/oas3_2.js';
5-
import { AsyncApi2Types } from './types/asyncapi2.js';
6-
import { AsyncApi3Types } from './types/asyncapi3.js';
7-
import { Arazzo1Types } from './types/arazzo.js';
8-
import { Overlay1Types } from './types/overlay.js';
91
import { isPlainObject } from './utils.js';
102
import { VERSION_PATTERN } from './typings/arazzo.js';
113

@@ -47,17 +39,6 @@ export type SpecVersion = typeof specVersions[number];
4739

4840
export type SpecMajorVersion = 'oas2' | 'oas3' | 'async2' | 'async3' | 'arazzo1' | 'overlay1';
4941

50-
const typesMap = {
51-
oas2: Oas2Types,
52-
oas3_0: Oas3Types,
53-
oas3_1: Oas3_1Types,
54-
oas3_2: Oas3_2Types,
55-
async2: AsyncApi2Types,
56-
async3: AsyncApi3Types,
57-
arazzo1: Arazzo1Types,
58-
overlay1: Overlay1Types,
59-
};
60-
6142
export type RuleMap<Key extends string, RuleConfig, T> = Record<
6243
T extends 'built-in' ? Key : string,
6344
RuleConfig
@@ -175,6 +156,25 @@ export function getMajorSpecVersion(version: SpecVersion): SpecMajorVersion {
175156
}
176157
}
177158

178-
export function getTypes(spec: SpecVersion) {
179-
return typesMap[spec];
159+
export async function getTypes(spec: SpecVersion) {
160+
switch (spec) {
161+
case 'oas2':
162+
return (await import('./types/oas2.js')).Oas2Types;
163+
case 'oas3_0':
164+
return (await import('./types/oas3.js')).Oas3Types;
165+
case 'oas3_1':
166+
return (await import('./types/oas3_1.js')).Oas3_1Types;
167+
case 'oas3_2':
168+
return (await import('./types/oas3_2.js')).Oas3_2Types;
169+
case 'async2':
170+
return (await import('./types/asyncapi2.js')).AsyncApi2Types;
171+
case 'async3':
172+
return (await import('./types/asyncapi3.js')).AsyncApi3Types;
173+
case 'arazzo1':
174+
return (await import('./types/arazzo.js')).Arazzo1Types;
175+
case 'overlay1':
176+
return (await import('./types/overlay.js')).Overlay1Types;
177+
default:
178+
throw new Error(`Unsupported specification: ${spec}`);
179+
}
180180
}

packages/core/src/types/redocly-yaml.ts

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -426,11 +426,17 @@ const Assert: NodeType = {
426426
required: ['subject', 'assertions'],
427427
};
428428

429-
export function createConfigTypes(extraSchemas: JSONSchema, config?: Config) {
430-
const nodeNames = specVersions.flatMap((version) => {
431-
const types = config ? config.extendTypes(getTypes(version), version) : getTypes(version);
432-
return Object.keys(types);
433-
});
429+
export async function createConfigTypes(extraSchemas: JSONSchema, config?: Config) {
430+
const nodeNames = (
431+
await Promise.all(
432+
specVersions.map(async (version) => {
433+
const types = config
434+
? config.extendTypes(await getTypes(version), version)
435+
: await getTypes(version);
436+
return Object.keys(types);
437+
})
438+
)
439+
).flat();
434440
// Create types based on external schemas
435441
const nodeTypes = getNodeTypesFromJSONSchema('rootRedoclyConfigSchema', extraSchemas);
436442

@@ -460,5 +466,37 @@ const CoreConfigTypes: Record<string, NodeType> = {
460466
// @ts-ignore FIXME: remove this once we remove `theme` from the schema
461467
delete rootRedoclyConfigSchema.properties.theme;
462468

463-
export const ConfigTypes: Record<string, NodeType> = createConfigTypes(rootRedoclyConfigSchema);
469+
// Lazy-loaded config types to support dynamic imports
470+
let _configTypesPromise: Promise<Record<string, NodeType>> | null = null;
471+
let _normalizedConfigTypes: ReturnType<typeof normalizeTypes> | null = null;
472+
473+
export function getConfigTypes(): Promise<Record<string, NodeType>> {
474+
if (!_configTypesPromise) {
475+
_configTypesPromise = createConfigTypes(rootRedoclyConfigSchema);
476+
}
477+
return _configTypesPromise;
478+
}
479+
480+
export async function getNormalizedConfigTypes() {
481+
if (!_normalizedConfigTypes) {
482+
const configTypes = await getConfigTypes();
483+
_normalizedConfigTypes = normalizeTypes(configTypes);
484+
}
485+
return _normalizedConfigTypes;
486+
}
487+
488+
// Synchronous exports for backwards compatibility (minimal set for sync initialization)
489+
// Note: This only includes core config types without spec-specific node names
490+
// For full type support with all specs, use getConfigTypes() or getNormalizedConfigTypes()
491+
const nodeTypes = getNodeTypesFromJSONSchema('rootRedoclyConfigSchema', rootRedoclyConfigSchema);
492+
493+
export const ConfigTypes: Record<string, NodeType> = {
494+
...CoreConfigTypes,
495+
ConfigRoot: createConfigRoot(nodeTypes),
496+
ConfigApisProperties: createConfigApisProperties(nodeTypes),
497+
AssertionDefinitionSubject: createAssertionDefinitionSubject([]), // Empty for sync version
498+
...nodeTypes,
499+
'rootRedoclyConfigSchema.scorecard.levels_items': createScorecardLevelsItems(nodeTypes),
500+
};
501+
464502
export const NormalizedConfigTypes = normalizeTypes(ConfigTypes);

0 commit comments

Comments
 (0)