Skip to content

Commit fc92169

Browse files
committed
fix: duplicate modular block
1 parent 999ad27 commit fc92169

1 file changed

Lines changed: 25 additions & 17 deletions

File tree

src/lib/tsgen/factory.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ type GlobalFieldCache = {
3131
[prop: string]: { definition: string };
3232
};
3333

34+
type ModularBlockCache = {
35+
[prop: string]: string;
36+
};
37+
3438
enum TypeFlags {
3539
BuiltinJS = 1 << 0,
3640
BuiltinCS = 1 << 1,
@@ -65,6 +69,7 @@ export default function (userOptions: TSGenOptions) {
6569
const visitedGlobalFields = new Set<string>()
6670
const visitedContentTypes = new Set<string>()
6771
const cachedGlobalFields: GlobalFieldCache = {}
72+
const cachedModularBlocks: ModularBlockCache = {}
6873
const modularBlockInterfaces = new Set<string>()
6974

7075
const typeMap: TypeMap = {
@@ -267,23 +272,26 @@ export default function (userOptions: TSGenOptions) {
267272

268273
function type_modular_blocks(field: ContentstackTypes.Field): string {
269274
const blockInterfaceName = name_type(field.uid);
270-
const blockInterfaces = field.blocks.map((block) => {
271-
const fieldType = block.reference_to && cachedGlobalFields[name_type(block.reference_to)]
272-
? name_type(block.reference_to)
273-
: visit_fields(block.schema || []);
274-
275-
const schema = block.reference_to ? `${fieldType};` : `{\n ${fieldType} }`;
276-
return `${block.uid}: ${schema}`;
277-
});
278-
279-
const modularInterface = [
280-
`export interface ${blockInterfaceName} {`,
281-
blockInterfaces.join('\n'),
282-
'}',
283-
].join('\n');
284-
285-
// Store or track the generated block interface for later use
286-
modularBlockInterfaces.add(modularInterface);
275+
if(!cachedModularBlocks[blockInterfaceName]){
276+
const blockInterfaces = field.blocks.map((block) => {
277+
const fieldType = block.reference_to && cachedGlobalFields[name_type(block.reference_to)]
278+
? name_type(block.reference_to)
279+
: visit_fields(block.schema || []);
280+
281+
const schema = block.reference_to ? `${fieldType};` : `{\n ${fieldType} }`;
282+
return `${block.uid}: ${schema}`;
283+
});
284+
285+
const modularInterface = [
286+
`export interface ${blockInterfaceName} {`,
287+
blockInterfaces.join('\n'),
288+
'}',
289+
].join('\n');
290+
291+
// Store or track the generated block interface for later use
292+
modularBlockInterfaces.add(modularInterface);
293+
cachedModularBlocks[blockInterfaceName] = blockInterfaceName;
294+
}
287295

288296
return field.multiple ? `${blockInterfaceName}[]` : blockInterfaceName;
289297
}

0 commit comments

Comments
 (0)