Skip to content

The open alternative to Salesforce, designed for AI.

Share frontend component build transforms with the server (#28465)

Weiko024e14663d38
diff --git a/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/css-injection-plugin.ts b/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/css-injection-plugin.ts
index 79c3d53ff93..15cefc014a8 100644
--- a/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/css-injection-plugin.ts
+++ b/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/css-injection-plugin.ts
@@ -1,24 +1 @@
-import { readFile } from 'node:fs/promises';
-
-import type * as esbuild from 'esbuild';
-
-const buildStyleInjectionModule = (cssText: string): string =>
- `if (typeof document !== 'undefined') {
- const styleElement = document.createElement('style');
- document.head.appendChild(styleElement);
- styleElement.textContent = ${JSON.stringify(cssText)};
-}`;
-
-export const cssInjectionPlugin: esbuild.Plugin = {
- name: 'css-injection',
- setup: (build) => {
- build.onLoad({ filter: /\.css$/ }, async ({ path }) => {
- const cssText = await readFile(path, 'utf8');
-
- return {
- contents: buildStyleInjectionModule(cssText),
- loader: 'js',
- };
- });
- },
-};
+export { cssInjectionPlugin } from 'twenty-shared/front-component-build';
diff --git a/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/utils/__tests__/unwrap-define-front-component-to-direct-export.spec.ts b/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/utils/__tests__/unwrap-define-front-component-to-direct-export.spec.ts
index 7bd0f2445d7..6406b07cdca 100644
--- a/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/utils/__tests__/unwrap-define-front-component-to-direct-export.spec.ts
+++ b/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/utils/__tests__/unwrap-define-front-component-to-direct-export.spec.ts
@@ -1,3 +1,5 @@
+import { transformSync } from 'esbuild';
+
import { unwrapDefineFrontComponentToDirectExport } from '@/cli/utilities/build/common/front-component-build/utils/unwrap-define-front-component-to-direct-export';
const INLINE_EXPRESSION_COMPONENT_SOURCE = `import { defineFrontComponent } from 'twenty-sdk/define';
@@ -47,6 +49,34 @@ export default defineSettingsFrontComponent({
});
`;
+const loadRenderer = (render: (element: unknown) => void) => {
+ const source = unwrapDefineFrontComponentToDirectExport(`
+ import { defineFrontComponent } from 'twenty-sdk/define';
+ export default defineFrontComponent({ component: () => null });
+ `);
+ const { code } = transformSync(source, { format: 'cjs' });
+ const bundleModule = { exports: {} };
+ const requireModule = (specifier: string) => {
+ if (specifier === 'react-dom/client') {
+ return { createRoot: () => ({ render }) };
+ }
+ if (specifier === 'react/jsx-runtime') {
+ return {
+ jsx: (component: unknown, props: unknown) => ({ component, props }),
+ };
+ }
+ throw new Error(`Unexpected import: ${specifier}`);
+ };
+
+ new Function('module', 'exports', 'require', code)(
+ bundleModule,
+ bundleModule.exports,
+ requireModule,
+ );
+
+ return bundleModule.exports as { default: (container: object) => unknown };
+};
+
describe('unwrapDefineFrontComponentToDirectExport', () => {
it('renders the component when component is an inline factory call', () => {
const output = unwrapDefineFrontComponentToDirectExport(
@@ -109,4 +139,23 @@ export const createSummarizeRecordEffect = () => () => null;
expect(unwrapDefineFrontComponentToDirectExport(source)).toBe(source);
});
+
+ it('keeps the synchronous renderer contract for existing callers', () => {
+ const renderedElements: unknown[] = [];
+ const renderer = loadRenderer((element) => renderedElements.push(element));
+
+ expect(renderer.default({})).toBeUndefined();
+ expect(renderedElements).toEqual([
+ { component: expect.any(Function), props: {} },
+ ]);
+ });
+
+ it('propagates synchronous rendering failures to the caller', () => {
+ const error = new Error('Cannot mount component');
+ const renderer = loadRenderer(() => {
+ throw error;
+ });
+
+ expect(() => renderer.default({})).toThrow(error);
+ });
});
diff --git a/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/utils/unwrap-define-front-component-to-direct-export.ts b/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/utils/unwrap-define-front-component-to-direct-export.ts
index 2225b17b2c3..d8e659341e1 100644
--- a/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/utils/unwrap-define-front-component-to-direct-export.ts
+++ b/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/utils/unwrap-define-front-component-to-direct-export.ts
@@ -1,48 +1 @@
-const DEFINE_FRONT_COMPONENT_IMPORT_PATTERN =
- /import\s*\{\s*define(?:Settings)?FrontComponent\s*\}\s*from\s*['"][^'"]+['"];?\n?/g;
-
-const DEFINE_FRONT_COMPONENT_EXPORT_OPENING_PATTERN =
- /export\s+default\s+define(?:Settings)?FrontComponent\s*\(/;
-
-const IDENTIFIER_COMPONENT_VALUE_PATTERN =
- /component\s*:\s*([A-Za-z_$][\w$]*)\s*[,}]/;
-
-const FRONT_COMPONENT_DEFINITION_NAME = '__frontComponentDefinition';
-
-export const unwrapDefineFrontComponentToDirectExport = (
- sourceCode: string,
-): string => {
- let transformedSource = sourceCode.replace(
- DEFINE_FRONT_COMPONENT_IMPORT_PATTERN,
- '',
- );
-
- if (!DEFINE_FRONT_COMPONENT_EXPORT_OPENING_PATTERN.test(transformedSource)) {
- return transformedSource;
- }
-
- transformedSource = transformedSource.replace(
- DEFINE_FRONT_COMPONENT_EXPORT_OPENING_PATTERN,
- `const ${FRONT_COMPONENT_DEFINITION_NAME} = (`,
- );
-
- const identifierComponentMatch = transformedSource.match(
- IDENTIFIER_COMPONENT_VALUE_PATTERN,
- );
-
- if (identifierComponentMatch) {
- const wrappedComponentName = identifierComponentMatch[1];
-
- transformedSource = transformedSource.replace(
- new RegExp(`export\\s+(const|function)\\s+${wrappedComponentName}\\b`),
- `$1 ${wrappedComponentName}`,
- );
- }
-
- return (
- `import { createRoot as __createRoot } from 'react-dom/client';\n` +
- `import { jsx as __frontComponentJsx } from 'react/jsx-runtime';\n` +
- transformedSource +
- `\nexport default function __renderFrontComponent(__container) { __createRoot(__container).render(__frontComponentJsx(${FRONT_COMPONENT_DEFINITION_NAME}.component, {})); }\n`
- );
-};
+export { unwrapDefineFrontComponentToDirectExport } from 'twenty-shared/front-component-build';
diff --git a/packages/twenty-shared/src/front-component-build/css-injection-plugin.ts b/packages/twenty-shared/src/front-component-build/css-injection-plugin.ts
new file mode 100644
index 00000000000..79c3d53ff93
--- /dev/null
+++ b/packages/twenty-shared/src/front-component-build/css-injection-plugin.ts
@@ -0,0 +1,24 @@
+import { readFile } from 'node:fs/promises';
+
+import type * as esbuild from 'esbuild';
+
+const buildStyleInjectionModule = (cssText: string): string =>
+ `if (typeof document !== 'undefined') {
+ const styleElement = document.createElement('style');
+ document.head.appendChild(styleElement);
+ styleElement.textContent = ${JSON.stringify(cssText)};
+}`;
+
+export const cssInjectionPlugin: esbuild.Plugin = {
+ name: 'css-injection',
+ setup: (build) => {
+ build.onLoad({ filter: /\.css$/ }, async ({ path }) => {
+ const cssText = await readFile(path, 'utf8');
+
+ return {
+ contents: buildStyleInjectionModule(cssText),
+ loader: 'js',
+ };
+ });
+ },
+};
diff --git a/packages/twenty-shared/src/front-component-build/index.ts b/packages/twenty-shared/src/front-component-build/index.ts
index e2b8b637a2b..653c5f8af47 100644
--- a/packages/twenty-shared/src/front-component-build/index.ts
+++ b/packages/twenty-shared/src/front-component-build/index.ts
@@ -7,4 +7,6 @@
* |___/
*/
+export { cssInjectionPlugin } from './css-injection-plugin';
export { createJsxRuntimeRemoteWrapperPlugin } from './jsx-runtime-remote-wrapper-plugin';
+export { unwrapDefineFrontComponentToDirectExport } from './unwrap-define-front-component-to-direct-export';
diff --git a/packages/twenty-shared/src/front-component-build/unwrap-define-front-component-to-direct-export.ts b/packages/twenty-shared/src/front-component-build/unwrap-define-front-component-to-direct-export.ts
new file mode 100644
index 00000000000..9668b7da5f2
--- /dev/null
+++ b/packages/twenty-shared/src/front-component-build/unwrap-define-front-component-to-direct-export.ts
@@ -0,0 +1,50 @@
+import { isDefined } from '@/utils/validation/isDefined';
+
+const DEFINE_FRONT_COMPONENT_IMPORT_PATTERN =
+ /import\s*\{\s*define(?:Settings)?FrontComponent\s*\}\s*from\s*['"][^'"]+['"];?\n?/g;
+
+const DEFINE_FRONT_COMPONENT_EXPORT_OPENING_PATTERN =
+ /export\s+default\s+define(?:Settings)?FrontComponent\s*\(/;
+
+const IDENTIFIER_COMPONENT_VALUE_PATTERN =
+ /component\s*:\s*([A-Za-z_$][\w$]*)\s*[,}]/;
+
+const FRONT_COMPONENT_DEFINITION_NAME = '__frontComponentDefinition';
+
+export const unwrapDefineFrontComponentToDirectExport = (
+ sourceCode: string,
+): string => {
+ let transformedSource = sourceCode.replace(
+ DEFINE_FRONT_COMPONENT_IMPORT_PATTERN,
+ '',
+ );
+
+ if (!DEFINE_FRONT_COMPONENT_EXPORT_OPENING_PATTERN.test(transformedSource)) {
+ return transformedSource;
+ }
+
+ transformedSource = transformedSource.replace(
+ DEFINE_FRONT_COMPONENT_EXPORT_OPENING_PATTERN,
+ `const ${FRONT_COMPONENT_DEFINITION_NAME} = (`,
+ );
+
+ const identifierComponentMatch = transformedSource.match(
+ IDENTIFIER_COMPONENT_VALUE_PATTERN,
+ );
+
+ if (isDefined(identifierComponentMatch)) {
+ const wrappedComponentName = identifierComponentMatch[1];
+
+ transformedSource = transformedSource.replace(
+ new RegExp(`export\\s+(const|function)\\s+${wrappedComponentName}\\b`),
+ `$1 ${wrappedComponentName}`,
+ );
+ }
+
+ return (
+ `import { createRoot as __createRoot } from 'react-dom/client';\n` +
+ `import { jsx as __frontComponentJsx } from 'react/jsx-runtime';\n` +
+ transformedSource +
+ `\nexport default function __renderFrontComponent(__container) { __createRoot(__container).render(__frontComponentJsx(${FRONT_COMPONENT_DEFINITION_NAME}.component, {})); }\n`
+ );
+};
diff --git a/packages/twenty-shared/vite.config.ts b/packages/twenty-shared/vite.config.ts
index d93460a4089..cd3bfafb0de 100644
--- a/packages/twenty-shared/vite.config.ts
+++ b/packages/twenty-shared/vite.config.ts
@@ -51,6 +51,7 @@ export default defineConfig(() => {
external: [
...Object.keys((packageJson as any).dependencies || {}),
'typescript',
+ 'node:fs/promises',
// TODO: i18n now hashes with @noble/hashes, so this node:crypto external looks removable.
'node:crypto',
],