Files
twenty/packages/twenty-sdk/scripts/remote-dom/generators/remote-components.generator.ts
T
Charles Bochet f17cc4d190 Improve building of twenty-sdk (#17913)
## Split twenty-sdk build into separate Node and browser targets

The SDK was bundling Node.js code (CLI, SDK API) and browser code (UI
components, front-component renderer) through a single Vite config. This
caused incorrect externalization — Node builtins leaked into browser
bundles and browser-specific chunking logic applied to CLI output.

This PR splits the build into `vite.config.node.ts` and
`vite.config.browser.ts` so each target gets the right externals and
output format.

Also includes a few housekeeping renames:
- `front-component` export path → `front-component-renderer` (matches
what it actually is)
- `front-component-common` merged into `front-component-api` (was a
needless extra module)
2026-02-13 15:58:19 +01:00

66 lines
1.7 KiB
TypeScript

import type { Project, SourceFile } from 'ts-morph';
import { EVENT_TO_REACT } from '@/sdk/front-component-api/constants/EventToReact';
import { type ComponentSchema } from './schemas';
import { addExportedConst, addFileHeader } from './utils';
const generateComponentDefinition = (
sourceFile: SourceFile,
component: ComponentSchema,
): void => {
const hasEvents = component.events.length > 0;
const componentExportName = component.tagName;
let initializer: string;
if (hasEvents) {
const eventProps = component.events
.map((event) => {
const propName = EVENT_TO_REACT[event];
return ` ${propName}: { event: '${event}' },`;
})
.join('\n');
initializer = `createRemoteComponent('${component.customElementName}', ${component.name}Element, {
eventProps: {
${eventProps}
},
})`;
} else {
initializer = `createRemoteComponent('${component.customElementName}', ${component.name}Element)`;
}
addExportedConst(sourceFile, componentExportName, initializer);
};
export const generateRemoteComponents = (
project: Project,
components: ComponentSchema[],
): SourceFile => {
const sourceFile = project.createSourceFile('remote-components.ts', '', {
overwrite: true,
});
sourceFile.addImportDeclaration({
moduleSpecifier: '@remote-dom/react',
namedImports: ['createRemoteComponent'],
});
const elementImports = components.map(
(component) => `${component.name}Element`,
);
sourceFile.addImportDeclaration({
moduleSpecifier: './remote-elements',
namedImports: elementImports,
});
for (const component of components) {
generateComponentDefinition(sourceFile, component);
}
addFileHeader(sourceFile);
return sourceFile;
};