[REFACTOR] twenty-shared multi barrel and CJS/ESM build with preconstruct (#11083)

# Introduction

In this PR we've migrated `twenty-shared` from a `vite` app
[libary-mode](https://vite.dev/guide/build#library-mode) to a
[preconstruct](https://preconstruct.tools/) "atomic" application ( in
the future would like to introduce preconstruct to handle of all our
atomic dependencies such as `twenty-emails` `twenty-ui` etc it will be
integrated at the monorepo's root directly, would be to invasive in the
first, starting incremental via `twenty-shared`)

For more information regarding the motivations please refer to nor:
- https://github.com/twentyhq/core-team-issues/issues/587
-
https://github.com/twentyhq/core-team-issues/issues/281#issuecomment-2630949682

close https://github.com/twentyhq/core-team-issues/issues/589
close https://github.com/twentyhq/core-team-issues/issues/590

## How to test
In order to ease the review this PR will ship all the codegen at the
very end, the actual meaning full diff is `+2,411 −114`
In order to migrate existing dependent packages to `twenty-shared` multi
barrel new arch you need to run in local:
```sh
yarn tsx packages/twenty-shared/scripts/migrateFromSingleToMultiBarrelImport.ts && \
npx nx run-many -t lint --fix -p twenty-front twenty-ui twenty-server twenty-emails twenty-shared twenty-zapier
```
Note that `migrateFromSingleToMultiBarrelImport` is idempotent, it's atm
included in the PR but should not be merged. ( such as codegen will be
added before merging this script will be removed )

## Misc
- related opened issue preconstruct
https://github.com/preconstruct/preconstruct/issues/617

## Closed related PR
- https://github.com/twentyhq/twenty/pull/11028
- https://github.com/twentyhq/twenty/pull/10993
- https://github.com/twentyhq/twenty/pull/10960

## Upcoming enhancement: ( in others dedicated PRs )
- 1/ refactor generate barrel to export atomic module instead of `*`
- 2/ generate barrel own package with several files and tests
- 3/ Migration twenty-ui the same way
- 4/ Use `preconstruct` at monorepo global level

## Conclusion
As always any suggestions are welcomed !
This commit is contained in:
Paul Rastoin
2025-03-22 19:16:06 +01:00
committed by GitHub
parent 8a21c19f03
commit 9ad8287dbc
1091 changed files with 3611 additions and 1297 deletions

View File

@ -2,14 +2,13 @@ import { createReactBlockSpec } from '@blocknote/react';
import styled from '@emotion/styled';
import { isNonEmptyString } from '@sniptt/guards';
import { ChangeEvent, useRef } from 'react';
import { isDefined } from 'twenty-shared';
import { Button } from 'twenty-ui';
import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull';
import { AttachmentIcon } from '../../files/components/AttachmentIcon';
import { AttachmentType } from '../../files/types/Attachment';
import { getFileType } from '../../files/utils/getFileType';
import { isDefined } from 'twenty-shared/utils';
const StyledFileInput = styled.input`
display: none;

View File

@ -11,7 +11,6 @@ import { getCalendarEventStartDate } from '@/activities/calendar/utils/getCalend
import { hasCalendarEventEnded } from '@/activities/calendar/utils/hasCalendarEventEnded';
import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
import { useOpenCalendarEventInCommandMenu } from '@/command-menu/hooks/useOpenCalendarEventInCommandMenu';
import { isDefined } from 'twenty-shared';
import {
Avatar,
AvatarGroup,
@ -24,6 +23,7 @@ import {
CalendarChannelVisibility,
TimelineCalendarEvent,
} from '~/generated-metadata/graphql';
import { isDefined } from 'twenty-shared/utils';
type CalendarEventRowProps = {
calendarEvent: TimelineCalendarEvent;

View File

@ -3,10 +3,10 @@ import { useMemo, useState } from 'react';
import { findUpcomingCalendarEvent } from '@/activities/calendar/utils/findUpcomingCalendarEvent';
import { getCalendarEventStartDate } from '@/activities/calendar/utils/getCalendarEventStartDate';
import { isDefined } from 'twenty-shared';
import { TimelineCalendarEvent } from '~/generated/graphql';
import { groupArrayItemsBy } from '~/utils/array/groupArrayItemsBy';
import { sortDesc } from '~/utils/sort';
import { isDefined } from 'twenty-shared/utils';
export const useCalendarEvents = (calendarEvents: TimelineCalendarEvent[]) => {
const calendarEventsByDayTime = groupArrayItemsBy(

View File

@ -21,7 +21,6 @@ import { AppHotkeyScope } from '@/ui/utilities/hotkey/types/AppHotkeyScope';
import { isNonTextWritingKey } from '@/ui/utilities/hotkey/utils/isNonTextWritingKey';
import { useRecoilComponentValueV2 } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValueV2';
import { Key } from 'ts-key-enum';
import { isDefined } from 'twenty-shared';
import { useDebouncedCallback } from 'use-debounce';
import { ActivityRichTextEditorChangeOnActivityIdEffect } from '@/activities/components/ActivityRichTextEditorChangeOnActivityIdEffect';
@ -34,6 +33,7 @@ import '@blocknote/mantine/style.css';
import { useCreateBlockNote } from '@blocknote/react';
import '@blocknote/react/style.css';
import { isArray, isNonEmptyString } from '@sniptt/guards';
import { isDefined } from 'twenty-shared/utils';
type ActivityRichTextEditorProps = {
activityId: string;

View File

@ -1,7 +1,7 @@
import { isNonEmptyString } from '@sniptt/guards';
import { EmailThreadMessageParticipant } from '@/activities/emails/types/EmailThreadMessageParticipant';
import { isDefined } from 'twenty-shared';
import { isDefined } from 'twenty-shared/utils';
export const getDisplayNameFromParticipant = ({
participant,

View File

@ -15,7 +15,7 @@ import { TextInput } from '@/ui/input/components/TextInput';
import { useTheme } from '@emotion/react';
import styled from '@emotion/styled';
import { useState } from 'react';
import { isDefined } from 'twenty-shared';
import { isDefined } from 'twenty-shared/utils';
import {
IconCalendar,
OverflowingTextWithTooltip,

View File

@ -18,7 +18,7 @@ import { useAttachments } from '@/activities/files/hooks/useAttachments';
import { useUploadAttachmentFile } from '@/activities/files/hooks/useUploadAttachmentFile';
import { ActivityTargetableObject } from '@/activities/types/ActivityTargetableEntity';
import { useHasObjectReadOnlyPermission } from '@/settings/roles/hooks/useHasObjectReadOnlyPermission';
import { isDefined } from 'twenty-shared';
import { isDefined } from 'twenty-shared/utils';
const StyledAttachmentsContainer = styled.div`
display: flex;

View File

@ -9,7 +9,7 @@ import { TaskTarget } from '@/activities/types/TaskTarget';
import { objectMetadataItemsState } from '@/object-metadata/states/objectMetadataItemsState';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { recordStoreFamilyState } from '@/object-record/record-store/states/recordStoreFamilyState';
import { isDefined } from 'twenty-shared';
import { isDefined } from 'twenty-shared/utils';
export const useActivityTargetObjectRecords = (
activityRecordId?: string,

View File

@ -16,7 +16,7 @@ import { NoteTarget } from '@/activities/types/NoteTarget';
import { TaskTarget } from '@/activities/types/TaskTarget';
import { getJoinObjectNameSingular } from '@/activities/utils/getJoinObjectNameSingular';
import { useRecoilCallback } from 'recoil';
import { capitalize } from 'twenty-shared';
import { capitalize } from 'twenty-shared/utils';
export const useCreateActivityInDB = ({
activityObjectNameSingular,

View File

@ -13,8 +13,8 @@ import { useGetRecordFromCache } from '@/object-record/cache/hooks/useGetRecordF
import { useUpsertFindManyRecordsQueryInCache } from '@/object-record/cache/hooks/useUpsertFindManyRecordsQueryInCache';
import { getRecordFromCache } from '@/object-record/cache/utils/getRecordFromCache';
import { ObjectRecord } from '@/object-record/types/ObjectRecord';
import { isDefined } from 'twenty-shared';
import { sortByAscString } from '~/utils/array/sortByAscString';
import { isDefined } from 'twenty-shared/utils';
export const usePrepareFindManyActivitiesQuery = ({
activityObjectNameSingular,

View File

@ -3,7 +3,7 @@ import { useRecoilValue } from 'recoil';
import { usePrepareFindManyActivitiesQuery } from '@/activities/hooks/usePrepareFindManyActivitiesQuery';
import { objectShowPageTargetableObjectState } from '@/activities/timeline-activities/states/objectShowPageTargetableObjectIdState';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { isDefined } from 'twenty-shared';
import { isDefined } from 'twenty-shared/utils';
// This hook should only be executed if the normalized cache is up-to-date
// It will take a targetableObject and prepare the queries for the activities

View File

@ -2,8 +2,8 @@ import { BLOCK_SCHEMA } from '@/activities/blocks/constants/Schema';
import { recordStoreFamilyState } from '@/object-record/record-store/states/recordStoreFamilyState';
import { isNonEmptyString } from '@sniptt/guards';
import { useRecoilCallback } from 'recoil';
import { isDefined } from 'twenty-shared';
import { isDeeplyEqual } from '~/utils/isDeeplyEqual';
import { isDefined } from 'twenty-shared/utils';
export const useReplaceActivityBlockEditorContent = (
editor: typeof BLOCK_SCHEMA.BlockNoteEditor,

View File

@ -9,7 +9,7 @@ import { Note } from '@/activities/types/Note';
import { Task } from '@/activities/types/Task';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { useUpdateOneRecord } from '@/object-record/hooks/useUpdateOneRecord';
import { isDefined } from 'twenty-shared';
import { isDefined } from 'twenty-shared/utils';
export const useUpsertActivity = ({
activityObjectNameSingular,

View File

@ -11,8 +11,8 @@ import { RecordPickerPickableMorphItem } from '@/object-record/record-picker/typ
import { recordStoreFamilyState } from '@/object-record/record-store/states/recordStoreFamilyState';
import { isNull } from '@sniptt/guards';
import { useRecoilCallback, useSetRecoilState } from 'recoil';
import { isDefined } from 'twenty-shared';
import { v4 } from 'uuid';
import { isDefined } from 'twenty-shared/utils';
type UpdateActivityTargetFromInlineCellProps = {
recordPickerInstanceId: string;

View File

@ -7,13 +7,13 @@ import { useFindOneRecord } from '@/object-record/hooks/useFindOneRecord';
import { useUpsertRecordsInStore } from '@/object-record/record-store/hooks/useUpsertRecordsInStore';
import { UserContext } from '@/users/contexts/UserContext';
import { useContext } from 'react';
import { isDefined } from 'twenty-shared';
import {
formatToHumanReadableDay,
formatToHumanReadableMonth,
formatToHumanReadableTime,
} from '~/utils/format/formatDate';
import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull';
import { isDefined } from 'twenty-shared/utils';
const StyledEventCardCalendarEventContainer = styled.div`
cursor: pointer;

View File

@ -5,7 +5,7 @@ import { FieldMetadataItem } from '@/object-metadata/types/FieldMetadataItem';
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
import { useSetRecordValue } from '@/object-record/record-store/contexts/RecordFieldValueSelectorContext';
import { recordStoreFamilyState } from '@/object-record/record-store/states/recordStoreFamilyState';
import { isDefined } from 'twenty-shared';
import { isDefined } from 'twenty-shared/utils';
export const EventFieldDiffValueEffect = ({
diffArtificialRecordStoreId,

View File

@ -7,7 +7,7 @@ import { EventCardMessageNotShared } from '@/activities/timeline-activities/rows
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { useFindOneRecord } from '@/object-record/hooks/useFindOneRecord';
import { useUpsertRecordsInStore } from '@/object-record/record-store/hooks/useUpsertRecordsInStore';
import { isDefined } from 'twenty-shared';
import { isDefined } from 'twenty-shared/utils';
const StyledEventCardMessageContainer = styled.div`
display: flex;

View File

@ -1,6 +1,6 @@
import { TimelineActivity } from '@/activities/timeline-activities/types/TimelineActivity';
import { CurrentWorkspaceMember } from '@/auth/states/currentWorkspaceMemberState';
import { isDefined } from 'twenty-shared';
import { isDefined } from 'twenty-shared/utils';
export const getTimelineActivityAuthorFullName = (
event: TimelineActivity,

View File

@ -1,5 +1,5 @@
import { TimelineActivity } from '@/activities/timeline-activities/types/TimelineActivity';
import { isDefined } from 'twenty-shared';
import { isDefined } from 'twenty-shared/utils';
export type EventGroup = {
month: number;