twenty/packages/twenty-apps/fixtures/rich-app/src/logic-functions/test-function-2.function.ts

13 lines
355 B
TypeScript
Raw Normal View History

perf(sdk): split twenty-sdk barrel into per-purpose subpaths to cut logic-function bundle ~700x (#19834) ## Summary Logic-function bundles produced by the twenty-sdk CLI were ~1.18 MB even for a one-line handler. Root cause: the SDK shipped as a single bundled barrel (`twenty-sdk` → `dist/index.mjs`) that co-mingled server-side definition factories with the front-component runtime, validation (zod), and React. With no `\"sideEffects\"` declaration on the SDK package, esbuild had to assume every module-level statement could have side effects and refused to drop unused code. This PR restructures the SDK so consumers' bundlers can tree-shake at the leaf level: - **Reorganized SDK source.** All server-side definition factories now live under `src/sdk/define/` (agents, application, fields, logic-functions, objects, page-layouts, roles, skills, views, navigation-menu-items, etc.). All front-component runtime (components, hooks, host APIs, command primitives) lives under `src/sdk/front-component/`. The legacy bare `src/sdk/index.ts` is removed; the bare `twenty-sdk` entry no longer exists. - **Split the build configs by purpose / runtime env.** Replaced `vite.config.sdk.ts` with two purpose-specific configs: - `vite.config.define.ts` — node target, externals from package `dependencies`, emits to `dist/define/**` - `vite.config.front-component.ts` — browser/React target, emits to `dist/front-component/**` Both use `preserveModules: true` so each leaf ships as its own `.mjs`. - **\`\"sideEffects\": false\`** on `twenty-sdk` so esbuild can drop unreferenced re-exports. - **\`package.json\` exports + \`typesVersions\`** updated: dropped the bare \`.\` entry, added \`./front-component\`, and pointed \`./define\` at the new per-module dist layout. - **Migrated every internal/example/community app** to the new subpath imports (`twenty-sdk/define`, `twenty-sdk/front-component`, `twenty-sdk/ui`). - **Added \`bundle-investigation\` internal app** that reproduces the bundle bloat and demonstrates the fix. - Cleaned up dead \`twenty-sdk/dist/sdk/...\` references in the front-component story builder, the call-recording app, and the SDK tsconfig. ## Bundle size impact Measured with esbuild using the same options as the SDK CLI (\`packages/twenty-apps/internal/bundle-investigation\`): | Variant | Imports | Before | After | | ----------------------- | ------------------------------------------------------- | ---------- | --------- | | \`01-bare\` | \`defineLogicFunction\` from \`twenty-sdk/define\` | 1177 KB | **1.6 KB** | | \`02-with-sdk-client\` | + \`CoreApiClient\` from \`twenty-client-sdk/core\` | 1177 KB | **1.9 KB** | | \`03-fetch-issues\` | + GitHub GraphQL fetch + JWT signing + 2 mutations | 1181 KB | **5.8 KB** | | \`05-via-define-subpath\` | same as \`01\`, via the public subpath | 1177 KB | **1.7 KB** | That's a ~735× reduction on the bare baseline. Knock-on benefits for Lambda warm + cold starts, S3 upload size, and \`/tmp\` disk usage in warm containers. ## Test plan - [x] \`npx nx run twenty-sdk:build\` succeeds - [x] \`npx nx run twenty-sdk:typecheck\` passes - [x] \`npx nx run twenty-sdk:test:unit\` passes (31 files / 257 tests) - [x] \`npx nx run-many -t typecheck --projects=twenty-front,twenty-server,twenty-front-component-renderer,twenty-sdk,twenty-shared,bundle-investigation\` passes - [x] \`node packages/twenty-apps/internal/bundle-investigation/scripts/build-variants.mjs\` produces the sizes above - [ ] CI green Made with [Cursor](https://cursor.com)
2026-04-18 17:38:34 +00:00
import { defineLogicFunction } from 'twenty-sdk/define';
Improve application ast (#17016) # Summary - Introduces a new, flexible folder structure for Twenty SDK applications using file suffix-based entity detection - Adds defineApp, defineFunction, defineObject, and defineRole helper functions with built-in validation - Refactors manifest loading to use jiti runtime evaluation for TypeScript config files - Separates validation logic into dedicated module with comprehensive error reporting # New Application Folder Structure Applications now use a convention-over-configuration approach where entities are detected by their file suffix, allowing flexible organization within the src/app/ folder. # Required Structure my-app/ ├── package.json ├── yarn.lock └── src/ ├── app/ │ └── application.config.ts # Required - main application configuration └── utils/ # Optional - handler implementations & utilities # Entity Detection by File Suffix - *.object.ts - Custom object definitions - *.function.ts - Serverless function definitions - *.role.ts - Role definitions # Supported Folder Organizations ## Traditional (by type): src/app/ ├── application.config.ts ├── objects/ │ └── postCard.object.ts ├── functions/ │ └── createPostCard.function.ts └── roles/ └── admin.role.ts ## Feature-based: src/app/ ├── application.config.ts └── post-card/ ├── postCard.object.ts ├── createPostCard.function.ts └── postCardAdmin.role.ts ## Flat: src/app/ ├── application.config.ts ├── postCard.object.ts ├── createPostCard.function.ts └── admin.role.ts # New Helper Functions ## defineApp(config) import { defineApp } from 'twenty-sdk'; export default defineApp({ universalIdentifier: '4ec0391d-...', displayName: 'My App', description: 'App description', icon: 'IconWorld', }); ## defineObject(config) import { defineObject, FieldType } from 'twenty-sdk'; export default defineObject({ universalIdentifier: '54b589ca-...', nameSingular: 'postCard', namePlural: 'postCards', labelSingular: 'Post Card', labelPlural: 'Post Cards', icon: 'IconMail', fields: [ { universalIdentifier: '58a0a314-...', type: FieldType.TEXT, name: 'content', label: 'Content', }, ], }); ## defineFunction(config) import { defineFunction } from 'twenty-sdk'; import { myHandler } from '../utils/my-handler'; export default defineFunction({ universalIdentifier: 'e56d363b-...', name: 'My Function', handler: myHandler, triggers: [ { universalIdentifier: 'c9f84c8d-...', type: 'route', path: '/my-route', httpMethod: 'POST', }, ], }); ## defineRole(config) import { defineRole, PermissionFlag } from 'twenty-sdk'; export default defineRole({ universalIdentifier: 'b648f87b-...', label: 'App User', objectPermissions: [ { objectNameSingular: 'postCard', canReadObjectRecords: true, }, ], permissionFlags: [PermissionFlag.UPLOAD_FILE], }); # Test plan - Verify npx twenty app sync works with new folder structure - Verify npx twenty app dev works with new folder structure - Verify validation errors display correctly for invalid configs - Verify all three folder organization styles work (traditional, feature-based, flat) - Run existing E2E tests to ensure backward compatibility
2026-01-09 13:06:30 +00:00
import { testFunction2 } from '../utils/test-function-2.util';
export default defineLogicFunction({
Improve application ast (#17016) # Summary - Introduces a new, flexible folder structure for Twenty SDK applications using file suffix-based entity detection - Adds defineApp, defineFunction, defineObject, and defineRole helper functions with built-in validation - Refactors manifest loading to use jiti runtime evaluation for TypeScript config files - Separates validation logic into dedicated module with comprehensive error reporting # New Application Folder Structure Applications now use a convention-over-configuration approach where entities are detected by their file suffix, allowing flexible organization within the src/app/ folder. # Required Structure my-app/ ├── package.json ├── yarn.lock └── src/ ├── app/ │ └── application.config.ts # Required - main application configuration └── utils/ # Optional - handler implementations & utilities # Entity Detection by File Suffix - *.object.ts - Custom object definitions - *.function.ts - Serverless function definitions - *.role.ts - Role definitions # Supported Folder Organizations ## Traditional (by type): src/app/ ├── application.config.ts ├── objects/ │ └── postCard.object.ts ├── functions/ │ └── createPostCard.function.ts └── roles/ └── admin.role.ts ## Feature-based: src/app/ ├── application.config.ts └── post-card/ ├── postCard.object.ts ├── createPostCard.function.ts └── postCardAdmin.role.ts ## Flat: src/app/ ├── application.config.ts ├── postCard.object.ts ├── createPostCard.function.ts └── admin.role.ts # New Helper Functions ## defineApp(config) import { defineApp } from 'twenty-sdk'; export default defineApp({ universalIdentifier: '4ec0391d-...', displayName: 'My App', description: 'App description', icon: 'IconWorld', }); ## defineObject(config) import { defineObject, FieldType } from 'twenty-sdk'; export default defineObject({ universalIdentifier: '54b589ca-...', nameSingular: 'postCard', namePlural: 'postCards', labelSingular: 'Post Card', labelPlural: 'Post Cards', icon: 'IconMail', fields: [ { universalIdentifier: '58a0a314-...', type: FieldType.TEXT, name: 'content', label: 'Content', }, ], }); ## defineFunction(config) import { defineFunction } from 'twenty-sdk'; import { myHandler } from '../utils/my-handler'; export default defineFunction({ universalIdentifier: 'e56d363b-...', name: 'My Function', handler: myHandler, triggers: [ { universalIdentifier: 'c9f84c8d-...', type: 'route', path: '/my-route', httpMethod: 'POST', }, ], }); ## defineRole(config) import { defineRole, PermissionFlag } from 'twenty-sdk'; export default defineRole({ universalIdentifier: 'b648f87b-...', label: 'App User', objectPermissions: [ { objectNameSingular: 'postCard', canReadObjectRecords: true, }, ], permissionFlags: [PermissionFlag.UPLOAD_FILE], }); # Test plan - Verify npx twenty app sync works with new folder structure - Verify npx twenty app dev works with new folder structure - Verify validation errors display correctly for invalid configs - Verify all three folder organization styles work (traditional, feature-based, flat) - Run existing E2E tests to ensure backward compatibility
2026-01-09 13:06:30 +00:00
universalIdentifier: 'eb3ffc98-88ec-45d4-9b4a-56833b219ccb',
name: 'test-function-2',
timeoutSeconds: 2,
handler: testFunction2,
cronTriggerSettings: {
pattern: '0 0 1 1 *',
},
Improve application ast (#17016) # Summary - Introduces a new, flexible folder structure for Twenty SDK applications using file suffix-based entity detection - Adds defineApp, defineFunction, defineObject, and defineRole helper functions with built-in validation - Refactors manifest loading to use jiti runtime evaluation for TypeScript config files - Separates validation logic into dedicated module with comprehensive error reporting # New Application Folder Structure Applications now use a convention-over-configuration approach where entities are detected by their file suffix, allowing flexible organization within the src/app/ folder. # Required Structure my-app/ ├── package.json ├── yarn.lock └── src/ ├── app/ │ └── application.config.ts # Required - main application configuration └── utils/ # Optional - handler implementations & utilities # Entity Detection by File Suffix - *.object.ts - Custom object definitions - *.function.ts - Serverless function definitions - *.role.ts - Role definitions # Supported Folder Organizations ## Traditional (by type): src/app/ ├── application.config.ts ├── objects/ │ └── postCard.object.ts ├── functions/ │ └── createPostCard.function.ts └── roles/ └── admin.role.ts ## Feature-based: src/app/ ├── application.config.ts └── post-card/ ├── postCard.object.ts ├── createPostCard.function.ts └── postCardAdmin.role.ts ## Flat: src/app/ ├── application.config.ts ├── postCard.object.ts ├── createPostCard.function.ts └── admin.role.ts # New Helper Functions ## defineApp(config) import { defineApp } from 'twenty-sdk'; export default defineApp({ universalIdentifier: '4ec0391d-...', displayName: 'My App', description: 'App description', icon: 'IconWorld', }); ## defineObject(config) import { defineObject, FieldType } from 'twenty-sdk'; export default defineObject({ universalIdentifier: '54b589ca-...', nameSingular: 'postCard', namePlural: 'postCards', labelSingular: 'Post Card', labelPlural: 'Post Cards', icon: 'IconMail', fields: [ { universalIdentifier: '58a0a314-...', type: FieldType.TEXT, name: 'content', label: 'Content', }, ], }); ## defineFunction(config) import { defineFunction } from 'twenty-sdk'; import { myHandler } from '../utils/my-handler'; export default defineFunction({ universalIdentifier: 'e56d363b-...', name: 'My Function', handler: myHandler, triggers: [ { universalIdentifier: 'c9f84c8d-...', type: 'route', path: '/my-route', httpMethod: 'POST', }, ], }); ## defineRole(config) import { defineRole, PermissionFlag } from 'twenty-sdk'; export default defineRole({ universalIdentifier: 'b648f87b-...', label: 'App User', objectPermissions: [ { objectNameSingular: 'postCard', canReadObjectRecords: true, }, ], permissionFlags: [PermissionFlag.UPLOAD_FILE], }); # Test plan - Verify npx twenty app sync works with new folder structure - Verify npx twenty app dev works with new folder structure - Verify validation errors display correctly for invalid configs - Verify all three folder organization styles work (traditional, feature-based, flat) - Run existing E2E tests to ensure backward compatibility
2026-01-09 13:06:30 +00:00
});