Skip to content

Commit f85dd83

Browse files
authored
Merge pull request #124 from contentstack/fix/DX-1333-duplicate-block
Fix/dx 1333 duplicate block
2 parents b106ee0 + 4c271ae commit f85dd83

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 = {
@@ -256,23 +261,26 @@ export default function (userOptions: TSGenOptions) {
256261

257262
function type_modular_blocks(field: ContentstackTypes.Field): string {
258263
const blockInterfaceName = name_type(field.uid);
259-
const blockInterfaces = field.blocks.map((block) => {
260-
const fieldType = block.reference_to && cachedGlobalFields[name_type(block.reference_to)]
261-
? name_type(block.reference_to)
262-
: visit_fields(block.schema || []);
263-
264-
const schema = block.reference_to ? `${fieldType};` : `{\n ${fieldType} }`;
265-
return `${block.uid}: ${schema}`;
266-
});
267-
268-
const modularInterface = [
269-
`export interface ${blockInterfaceName} {`,
270-
blockInterfaces.join('\n'),
271-
'}',
272-
].join('\n');
273-
274-
// Store or track the generated block interface for later use
275-
modularBlockInterfaces.add(modularInterface);
264+
if(!cachedModularBlocks[blockInterfaceName]){
265+
const blockInterfaces = field.blocks.map((block) => {
266+
const fieldType = block.reference_to && cachedGlobalFields[name_type(block.reference_to)]
267+
? name_type(block.reference_to)
268+
: visit_fields(block.schema || []);
269+
270+
const schema = block.reference_to ? `${fieldType};` : `{\n ${fieldType} }`;
271+
return `${block.uid}: ${schema}`;
272+
});
273+
274+
const modularInterface = [
275+
`export interface ${blockInterfaceName} {`,
276+
blockInterfaces.join('\n'),
277+
'}',
278+
].join('\n');
279+
280+
// Store or track the generated block interface for later use
281+
modularBlockInterfaces.add(modularInterface);
282+
cachedModularBlocks[blockInterfaceName] = blockInterfaceName;
283+
}
276284

277285
return field.multiple ? `${blockInterfaceName}[]` : blockInterfaceName;
278286
}

0 commit comments

Comments
 (0)