The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Unify CLI and SDK frontend component build transforms (#28541)
Weiko8ab601fa721d
diff --git a/packages/twenty-cli/src/app/bundles/front-component-build/__tests__/css-injection-plugin.spec.ts b/packages/twenty-cli/src/app/bundles/front-component-build/__tests__/css-injection-plugin.spec.tsindex 82757e3f2e8..4fbf0c90a15 100644--- a/packages/twenty-cli/src/app/bundles/front-component-build/__tests__/css-injection-plugin.spec.ts+++ b/packages/twenty-cli/src/app/bundles/front-component-build/__tests__/css-injection-plugin.spec.ts@@ -1,12 +1,15 @@import { describe, expect, it } from 'vitest';-import { mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';+import { mkdtemp, readFile, realpath, rm, writeFile } from 'node:fs/promises';import { tmpdir } from 'node:os';import { join } from 'path';import * as esbuild from 'esbuild';import { MINIMAL_APP_PATH } from '@/app/__tests__/utils/fixture-paths';+import { cssInjectionPlugin } from '@/app/bundles/front-component-build/css-injection-plugin';+import { collectWatchInputs } from '@/app/dev/collect-watch-inputs';+import { readWatchInputStamp } from '@/app/dev/read-watch-input-stamp';import { getBaseFrontComponentBuildOptions } from '@/app/bundles/front-component-build/utils/get-base-front-component-build-options';describe('front-component build CSS injection', () => {@@ -82,4 +85,49 @@ describe('front-component build CSS injection', () => {await rm(outputDir, { recursive: true, force: true });}}, 30000);++ it('tracks nested stylesheets and assets for each build', async () => {+ const directory = await realpath(+ await mkdtemp(join(tmpdir(), 'component-css-watch-')),+ );++ try {+ const stylesheet = join(directory, 'style.css');+ const nestedStylesheet = join(directory, 'nested.css');+ const asset = join(directory, 'icon.svg');++ await writeFile(+ stylesheet,+ '@import "./nested.css"; .image { background: url("./icon.svg"); }',+ );+ await writeFile(nestedStylesheet, '.nested { color: purple; }');+ await writeFile(asset, '<svg xmlns="http://www.w3.org/2000/svg"/>');++ for (let buildIndex = 0; buildIndex < 2; buildIndex++) {+ const { watchInputs } = await collectWatchInputs(() =>+ esbuild.build({+ entryPoints: [stylesheet],+ bundle: true,+ write: false,+ plugins: [cssInjectionPlugin],+ }),+ );+ const files = watchInputs.filter((input) => input.kind === 'file');++ expect(files.map((input) => input.path)).toEqual(+ expect.arrayContaining([stylesheet, nestedStylesheet, asset]),+ );+ await writeFile(+ nestedStylesheet,+ `.nested { z-index: ${buildIndex}; }`,+ );+ await writeFile(asset, `<svg data-build="${buildIndex}"/>`);+ for (const file of files.filter((input) => input.path !== stylesheet)) {+ expect(readWatchInputStamp(file)).not.toBe(file.stamp);+ }+ }+ } finally {+ await rm(directory, { recursive: true, force: true });+ }+ });});diff --git a/packages/twenty-cli/src/app/bundles/front-component-build/css-injection-plugin.ts b/packages/twenty-cli/src/app/bundles/front-component-build/css-injection-plugin.tsindex f00f226efea..2b8f8f960b8 100644--- a/packages/twenty-cli/src/app/bundles/front-component-build/css-injection-plugin.ts+++ b/packages/twenty-cli/src/app/bundles/front-component-build/css-injection-plugin.ts@@ -1,48 +1,7 @@-import * as esbuild from 'esbuild';-import { getWatchInputPlugins } from '@/app/dev/collect-watch-inputs';--const buildStyleInjectionModule = (cssText: string): string =>- `if (typeof document !== 'undefined') {- const styleElement = document.createElement('style');- document.head.appendChild(styleElement);- styleElement.textContent = ${JSON.stringify(cssText)};-}`;+import { createCssInjectionPlugin } from 'twenty-shared/front-component-build';-export const cssInjectionPlugin: esbuild.Plugin = {- name: 'css-injection',- setup: (build) => {- build.onLoad({ filter: /\.css$/, namespace: 'file' }, async ({ path }) => {- const result = await esbuild.build({- entryPoints: [path],- bundle: true,- write: false,- outfile: 'injected.css',- logLevel: 'silent',- plugins: getWatchInputPlugins(),- loader: Object.fromEntries(- [- '.png',- '.jpg',- '.jpeg',- '.gif',- '.svg',- '.webp',- '.avif',- '.ico',- '.woff',- '.woff2',- '.ttf',- '.eot',- '.otf',- ].map((extension) => [extension, 'dataurl']),- ),- });- const cssText = result.outputFiles[0].text;+import { getWatchInputPlugins } from '@/app/dev/collect-watch-inputs';- return {- contents: buildStyleInjectionModule(cssText),- loader: 'js',- };- });- },-};+export const cssInjectionPlugin = createCssInjectionPlugin({+ getBuildPlugins: getWatchInputPlugins,+});diff --git a/packages/twenty-cli/src/app/bundles/front-component-build/utils/unwrap-define-front-component-to-direct-export.ts b/packages/twenty-cli/src/app/bundles/front-component-build/utils/unwrap-define-front-component-to-direct-export.tsindex 78a18af46ba..d8e659341e1 100644--- a/packages/twenty-cli/src/app/bundles/front-component-build/utils/unwrap-define-front-component-to-direct-export.ts+++ b/packages/twenty-cli/src/app/bundles/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*\}\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 => {- if (!DEFINE_FRONT_COMPONENT_EXPORT_OPENING_PATTERN.test(sourceCode)) {- return sourceCode;- }-- let transformedSource = sourceCode.replace(- DEFINE_FRONT_COMPONENT_IMPORT_PATTERN,- '',- );-- 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-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.tsindex 6406b07cdca..ffdc473bbe3 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@@ -158,4 +158,34 @@ export const createSummarizeRecordEffect = () => () => null;expect(() => renderer.default({})).toThrow(error);});++ it('keeps imports used by a non-default definition', () => {+ const source =+ "import { defineFrontComponent } from 'twenty-sdk/define';\nexport const definition = defineFrontComponent({ component: () => null });";+ expect(unwrapDefineFrontComponentToDirectExport(source)).toBe(source);+ });++ it('unwraps a multiline import with a trailing comma', () => {+ const source = IDENTIFIER_COMPONENT_SOURCE.replace(+ 'import { defineFrontComponent }',+ 'import {\n defineFrontComponent,\n}',+ );+ const output = unwrapDefineFrontComponentToDirectExport(source);++ expect(output).not.toContain('defineFrontComponent');+ expect(output).toContain('__frontComponentDefinition.component');+ });++ it('handles long whitespace after the definition import without backtracking', () => {+ const source = IDENTIFIER_COMPONENT_SOURCE.replace(+ "import { defineFrontComponent } from 'twenty-sdk/define';",+ `import { defineFrontComponent${' '.repeat(50_000)}! } from 'twenty-sdk/define';`,+ );++ const startedAt = performance.now();+ const output = unwrapDefineFrontComponentToDirectExport(source);++ expect(output).toContain('__frontComponentDefinition.component');+ expect(performance.now() - startedAt).toBeLessThan(1000);+ });});diff --git a/packages/twenty-shared/src/front-component-build/__tests__/css-injection-plugin.test.ts b/packages/twenty-shared/src/front-component-build/__tests__/css-injection-plugin.test.tsnew file mode 100644index 00000000000..0bb9869c03a--- /dev/null+++ b/packages/twenty-shared/src/front-component-build/__tests__/css-injection-plugin.test.ts@@ -0,0 +1,136 @@+/** @jest-environment node */++import { mkdtemp, rm, writeFile } from 'node:fs/promises';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { runInNewContext } from 'node:vm';++import { build, context } from 'esbuild';++import { cssInjectionPlugin } from '../css-injection-plugin';++describe('cssInjectionPlugin', () => {+ let directory: string;++ beforeEach(async () => {+ directory = await mkdtemp(join(tmpdir(), 'component-css-'));+ });++ afterEach(async () => {+ await rm(directory, { recursive: true, force: true });+ });++ it('injects bundled CSS with relative imports and embedded assets', async () => {+ await writeFile(join(directory, 'entry.js'), "import './style.css';");+ await writeFile(+ join(directory, 'style.css'),+ '@import "./nested.css"; .image { background: url("./icon.svg"); }',+ );+ await writeFile(+ join(directory, 'nested.css'),+ '.nested { color: purple; }',+ );+ await writeFile(+ join(directory, 'icon.svg'),+ '<svg xmlns="http://www.w3.org/2000/svg"/>',+ );++ const result = await build({+ entryPoints: [join(directory, 'entry.js')],+ bundle: true,+ write: false,+ plugins: [cssInjectionPlugin],+ });+ const source = result.outputFiles.map((file) => file.text).join('\n');+ const styles: { textContent?: string }[] = [];++ runInNewContext(source, {+ document: {+ createElement: () => ({}),+ head: {+ appendChild: (style: { textContent?: string }) => styles.push(style),+ },+ },+ });++ expect(styles).toHaveLength(1);+ expect(styles[0]?.textContent).toContain('.nested');+ expect(styles[0]?.textContent).toContain('data:image/svg+xml');+ expect(styles[0]?.textContent).not.toContain('@import');+ expect(styles[0]?.textContent).not.toContain('url("./icon.svg")');+ expect(() => runInNewContext(source)).not.toThrow();+ });++ it('fails the build when an imported stylesheet is missing', async () => {+ await writeFile(join(directory, 'style.css'), '@import "./missing.css";');++ await expect(+ build({+ entryPoints: [join(directory, 'style.css')],+ bundle: true,+ write: false,+ logLevel: 'silent',+ plugins: [cssInjectionPlugin],+ }),+ ).rejects.toThrow('missing.css');+ });++ it('rebuilds when nested CSS and assets change in native watch mode', async () => {+ const nestedStylesheet = join(directory, 'nested.css');+ const asset = join(directory, 'icon.svg');+ await writeFile(+ join(directory, 'style.css'),+ '@import "./nested.css"; .image { background: url("./icon.svg"); }',+ );+ await writeFile(nestedStylesheet, '.nested { color: purple; }');+ await writeFile(asset, '<svg xmlns="http://www.w3.org/2000/svg"/>');+ let finishBuild = (_source: string) => {};+ const nextBuild = () =>+ new Promise<string>((resolveBuild, rejectBuild) => {+ const timeout = setTimeout(+ () => rejectBuild(new Error('CSS rebuild did not finish')),+ 2000,+ );+ finishBuild = (source) => {+ clearTimeout(timeout);+ resolveBuild(source);+ };+ });+ let result = nextBuild();+ const buildContext = await context({+ absWorkingDir: directory,+ entryPoints: ['style.css'],+ bundle: true,+ write: false,+ plugins: [+ cssInjectionPlugin,+ {+ name: 'observe-build',+ setup: (build) => {+ build.onEnd((result) => {+ finishBuild(+ result.outputFiles?.map((file) => file.text).join('') ?? '',+ );+ });+ },+ },+ ],+ });++ try {+ await buildContext.watch();+ expect(await result).toContain('purple');+ result = nextBuild();+ await writeFile(nestedStylesheet, '.nested { color: green; }');+ expect(await result).toContain('green');+ result = nextBuild();+ await writeFile(+ asset,+ '<svg xmlns="http://www.w3.org/2000/svg">changed</svg>',+ );+ expect(await result).toContain('changed');+ } finally {+ await buildContext.dispose();+ }+ });+});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.tsindex 79c3d53ff93..20b22b8fcda 100644--- a/packages/twenty-shared/src/front-component-build/css-injection-plugin.ts+++ b/packages/twenty-shared/src/front-component-build/css-injection-plugin.ts@@ -1,7 +1,10 @@-import { readFile } from 'node:fs/promises';+import { resolve } from 'node:path';import type * as esbuild from 'esbuild';+import { CustomError } from '@/utils/errors/CustomError';+import { isDefined } from '@/utils/validation/isDefined';+const buildStyleInjectionModule = (cssText: string): string =>`if (typeof document !== 'undefined') {const styleElement = document.createElement('style');@@ -9,16 +12,65 @@ const buildStyleInjectionModule = (cssText: string): string =>styleElement.textContent = ${JSON.stringify(cssText)};}`;-export const cssInjectionPlugin: esbuild.Plugin = {+type CssInjectionPluginOptions = {+ getBuildPlugins?: () => esbuild.Plugin[];+};++export const createCssInjectionPlugin = (+ options?: CssInjectionPluginOptions,+): esbuild.Plugin => ({name: 'css-injection',setup: (build) => {- build.onLoad({ filter: /\.css$/ }, async ({ path }) => {- const cssText = await readFile(path, 'utf8');+ const plugins = options?.getBuildPlugins?.() ?? [];++ build.onLoad({ filter: /\.css$/, namespace: 'file' }, async ({ path }) => {+ const result = await build.esbuild.build({+ absWorkingDir: build.initialOptions.absWorkingDir,+ entryPoints: [path],+ bundle: true,+ write: false,+ outfile: 'injected.css',+ metafile: true,+ logLevel: 'silent',+ plugins,+ loader: Object.fromEntries(+ [+ '.png',+ '.jpg',+ '.jpeg',+ '.gif',+ '.svg',+ '.webp',+ '.avif',+ '.ico',+ '.woff',+ '.woff2',+ '.ttf',+ '.eot',+ '.otf',+ ].map((extension) => [extension, 'dataurl']),+ ),+ });+ const [outputFile] = result.outputFiles;++ if (!isDefined(outputFile)) {+ throw new CustomError(+ `CSS build produced no output for ${path}`,+ 'CSS_BUILD_EMPTY_OUTPUT',+ );+ }++ const cssText = outputFile.text;return {contents: buildStyleInjectionModule(cssText),loader: 'js',+ watchFiles: Object.keys(result.metafile.inputs).map((input) =>+ resolve(build.initialOptions.absWorkingDir ?? process.cwd(), input),+ ),};});},-};+});++export const cssInjectionPlugin = createCssInjectionPlugin();diff --git a/packages/twenty-shared/src/front-component-build/index.ts b/packages/twenty-shared/src/front-component-build/index.tsindex 653c5f8af47..6c43ad3051c 100644--- a/packages/twenty-shared/src/front-component-build/index.ts+++ b/packages/twenty-shared/src/front-component-build/index.ts@@ -7,6 +7,9 @@* |___/*/-export { cssInjectionPlugin } from './css-injection-plugin';+export {+ createCssInjectionPlugin,+ 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.tsindex 9668b7da5f2..e0559199091 100644--- 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@@ -1,7 +1,7 @@import { isDefined } from '@/utils/validation/isDefined';const DEFINE_FRONT_COMPONENT_IMPORT_PATTERN =- /import\s*\{\s*define(?:Settings)?FrontComponent\s*\}\s*from\s*['"][^'"]+['"];?\n?/g;+ /import\s*\{\s*define(?:Settings)?FrontComponent\s*(?:,\s*)?\}\s*from\s*['"][^'"]+['"];?\n?/g;const DEFINE_FRONT_COMPONENT_EXPORT_OPENING_PATTERN =/export\s+default\s+define(?:Settings)?FrontComponent\s*\(/;@@ -14,15 +14,15 @@ const FRONT_COMPONENT_DEFINITION_NAME = '__frontComponentDefinition';export const unwrapDefineFrontComponentToDirectExport = (sourceCode: string,): string => {+ if (!DEFINE_FRONT_COMPONENT_EXPORT_OPENING_PATTERN.test(sourceCode)) {+ return sourceCode;+ }+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} = (`,diff --git a/packages/twenty-shared/vite.config.ts b/packages/twenty-shared/vite.config.tsindex cd3bfafb0de..c52cc22111a 100644--- a/packages/twenty-shared/vite.config.ts+++ b/packages/twenty-shared/vite.config.ts@@ -52,6 +52,7 @@ export default defineConfig(() => {...Object.keys((packageJson as any).dependencies || {}),'typescript','node:fs/promises',+ 'node:path',// TODO: i18n now hashes with @noble/hashes, so this node:crypto external looks removable.'node:crypto',],