[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

@ -1,8 +1,7 @@
import styled from '@emotion/styled';
import { isNonEmptyString } from '@sniptt/guards';
import { getImageAbsoluteURI } from 'twenty-shared';
import { REACT_APP_SERVER_BASE_URL } from '~/config';
import { getImageAbsoluteURI } from 'twenty-shared/utils';
type LogoProps = {
primaryLogo?: string | null;

View File

@ -6,8 +6,8 @@ import { useVerifyLogin } from '@/auth/hooks/useVerifyLogin';
import { AppPath } from '@/types/AppPath';
import { SnackBarVariant } from '@/ui/feedback/snack-bar-manager/components/SnackBar';
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
import { isDefined } from 'twenty-shared';
import { useNavigateApp } from '~/hooks/useNavigateApp';
import { isDefined } from 'twenty-shared/utils';
export const VerifyEffect = () => {
const [searchParams] = useSearchParams();

View File

@ -2,7 +2,6 @@ import { i18n } from '@lingui/core';
import { I18nProvider } from '@lingui/react';
import { renderHook } from '@testing-library/react';
import { RecoilRoot } from 'recoil';
import { SOURCE_LOCALE } from 'twenty-shared';
import { AppPath } from '@/types/AppPath';
import { SnackBarVariant } from '@/ui/feedback/snack-bar-manager/components/SnackBar';
@ -12,6 +11,8 @@ import { useAuth } from '../useAuth';
import { useVerifyLogin } from '../useVerifyLogin';
import { dynamicActivate } from '~/utils/i18n/dynamicActivate';
import { SOURCE_LOCALE } from 'twenty-shared/translations';
jest.mock('../useAuth', () => ({
useAuth: jest.fn(),
}));

View File

@ -20,7 +20,6 @@ import { clientConfigApiStatusState } from '@/client-config/states/clientConfigA
import { isDebugModeState } from '@/client-config/states/isDebugModeState';
import { supportChatState } from '@/client-config/states/supportChatState';
import { ColorScheme } from '@/workspace-member/types/WorkspaceMember';
import { APP_LOCALES, isDefined } from 'twenty-shared';
import { REACT_APP_SERVER_BASE_URL } from '~/config';
import {
useCheckUserExistsLazyQuery,
@ -65,6 +64,8 @@ import { i18n } from '@lingui/core';
import { useSearchParams } from 'react-router-dom';
import { getWorkspaceUrl } from '~/utils/getWorkspaceUrl';
import { dynamicActivate } from '~/utils/i18n/dynamicActivate';
import { APP_LOCALES } from 'twenty-shared/translations';
import { isDefined } from 'twenty-shared/utils';
export const useAuth = () => {
const setTokenPair = useSetRecoilState(tokenPairState);

View File

@ -7,7 +7,6 @@ import {
} from '@apollo/client';
import { loggerLink } from '@/apollo/utils/loggerLink';
import { isDefined } from 'twenty-shared';
import {
AuthTokenPair,
RenewTokenDocument,
@ -15,6 +14,7 @@ import {
RenewTokenMutationVariables,
} from '~/generated/graphql';
import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull';
import { isDefined } from 'twenty-shared/utils';
const logger = loggerLink(() => 'Twenty-Refresh');

View File

@ -3,7 +3,7 @@ import { Controller, useFormContext } from 'react-hook-form';
import styled from '@emotion/styled';
import { motion } from 'framer-motion';
import { Form } from '@/auth/sign-in-up/hooks/useSignInUpForm';
import { isDefined } from 'twenty-shared';
import { isDefined } from 'twenty-shared/utils';
const StyledFullWidthMotionDiv = styled(motion.div)`
width: 100%;

View File

@ -26,8 +26,8 @@ import { authProvidersState } from '@/client-config/states/authProvidersState';
import { useRedirectToWorkspaceDomain } from '@/domain-manager/hooks/useRedirectToWorkspaceDomain';
import { SnackBarVariant } from '@/ui/feedback/snack-bar-manager/components/SnackBar';
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
import { isDefined } from 'twenty-shared';
import { getWorkspaceUrl } from '~/utils/getWorkspaceUrl';
import { isDefined } from 'twenty-shared/utils';
const StyledContentContainer = styled(motion.div)`
margin-bottom: ${({ theme }) => theme.spacing(8)};

View File

@ -7,8 +7,8 @@ import { useRecoilValue } from 'recoil';
import { HorizontalSeparator, MainButton } from 'twenty-ui';
import { workspaceAuthProvidersState } from '@/workspace/states/workspaceAuthProvidersState';
import { isDefined } from 'twenty-shared';
import React from 'react';
import { isDefined } from 'twenty-shared/utils';
const StyledContentContainer = styled.div`
margin-bottom: ${({ theme }) => theme.spacing(8)};

View File

@ -15,8 +15,8 @@ import { useLingui } from '@lingui/react/macro';
import { useMemo, useState } from 'react';
import { useFormContext } from 'react-hook-form';
import { useRecoilState, useRecoilValue } from 'recoil';
import { isDefined } from 'twenty-shared';
import { Loader, MainButton } from 'twenty-ui';
import { isDefined } from 'twenty-shared/utils';
const StyledForm = styled.form`
align-items: center;

View File

@ -7,8 +7,8 @@ import { workspaceAuthProvidersState } from '@/workspace/states/workspaceAuthPro
import { useTheme } from '@emotion/react';
import { useLingui } from '@lingui/react/macro';
import { useRecoilValue, useSetRecoilState } from 'recoil';
import { isDefined } from 'twenty-shared';
import { HorizontalSeparator, IconLock, MainButton } from 'twenty-ui';
import { isDefined } from 'twenty-shared/utils';
export const SignInUpWithSSO = () => {
const theme = useTheme();

View File

@ -10,7 +10,7 @@ import { captchaState } from '@/client-config/states/captchaState';
import { workspaceAuthProvidersState } from '@/workspace/states/workspaceAuthProvidersState';
import { useEffect, useState } from 'react';
import { useRecoilValue, useSetRecoilState } from 'recoil';
import { isDefined } from 'twenty-shared';
import { isDefined } from 'twenty-shared/utils';
const searchParams = new URLSearchParams(window.location.search);
const email = searchParams.get('email');

View File

@ -2,7 +2,6 @@ import { i18n } from '@lingui/core';
import { I18nProvider } from '@lingui/react';
import { act, renderHook } from '@testing-library/react';
import { RecoilRoot } from 'recoil';
import { SOURCE_LOCALE } from 'twenty-shared';
import { useHandleResetPassword } from '@/auth/sign-in-up/hooks/useHandleResetPassword';
import { SnackBarVariant } from '@/ui/feedback/snack-bar-manager/components/SnackBar';
@ -13,6 +12,7 @@ import {
} from '~/generated/graphql';
import { dynamicActivate } from '~/utils/i18n/dynamicActivate';
import { workspacePublicDataState } from '@/auth/states/workspacePublicDataState';
import { SOURCE_LOCALE } from 'twenty-shared/translations';
// Mocks
jest.mock('@/ui/feedback/snack-bar-manager/hooks/useSnackBar');

View File

@ -11,7 +11,7 @@ import {
} from '@/auth/states/signInUpStepState';
import { PASSWORD_REGEX } from '@/auth/utils/passwordRegex';
import { isDeveloperDefaultSignInPrefilledState } from '@/client-config/states/isDeveloperDefaultSignInPrefilledState';
import { isDefined } from 'twenty-shared';
import { isDefined } from 'twenty-shared/utils';
const makeValidationSchema = (signInUpStep: SignInUpStep) =>
z

View File

@ -9,9 +9,9 @@ import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
import { isDefaultLayoutAuthModalVisibleState } from '@/ui/layout/states/isDefaultLayoutAuthModalVisibleState';
import { AppPath } from '@/types/AppPath';
import { isDefined } from 'twenty-shared';
import { useGetWorkspaceFromInviteHashQuery } from '~/generated/graphql';
import { useNavigateApp } from '~/hooks/useNavigateApp';
import { isDefined } from 'twenty-shared/utils';
export const useWorkspaceFromInviteHash = () => {
const { enqueueSnackBar } = useSnackBar();