@kirill.konshin/utils 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. package/.ctirc +10 -0
  2. package/.storybook/main.ts +21 -0
  3. package/.storybook/preview.ts +17 -0
  4. package/.turbo/turbo-build.log +67 -0
  5. package/.turbo/turbo-test.log +118 -0
  6. package/CHANGELOG.md +12 -0
  7. package/README.md +59 -0
  8. package/builders/.swcrc +18 -0
  9. package/builders/build.config.ts +11 -0
  10. package/builders/bun.build.js +23 -0
  11. package/builders/package.json +209 -0
  12. package/builders/perf.mjs +63 -0
  13. package/builders/rollup.config.mjs +50 -0
  14. package/builders/rslib.config.ts +26 -0
  15. package/builders/tsconfig.json +21 -0
  16. package/builders/tsup.config.ts +20 -0
  17. package/builders/turbo.json +24 -0
  18. package/builders/vite.config.ts +37 -0
  19. package/demo/cache-demo.ts +2 -1
  20. package/package.json +166 -21
  21. package/src/bootstrap/adaptiveContainer.tsx +12 -0
  22. package/src/bootstrap/controls.stories.tsx +62 -0
  23. package/src/bootstrap/controls.tsx +119 -0
  24. package/src/bootstrap/error.stories.tsx +34 -0
  25. package/src/bootstrap/error.tsx +40 -0
  26. package/src/bootstrap/field.stories.tsx +42 -0
  27. package/src/bootstrap/field.tsx +35 -0
  28. package/src/bootstrap/footer.tsx +49 -0
  29. package/src/bootstrap/globalLoading.stories.tsx +25 -0
  30. package/src/bootstrap/globalLoading.tsx +17 -0
  31. package/src/bootstrap/index.ts +12 -0
  32. package/src/bootstrap/loading.stories.tsx +35 -0
  33. package/src/bootstrap/loading.tsx +22 -0
  34. package/src/bootstrap/main.scss +86 -0
  35. package/src/bootstrap/responsiveHelper.tsx +50 -0
  36. package/src/bootstrap/screen.stories.tsx +160 -0
  37. package/src/bootstrap/screen.tsx +144 -0
  38. package/src/bootstrap/toaster.stories.tsx +32 -0
  39. package/src/bootstrap/toaster.tsx +38 -0
  40. package/src/bootstrap/useModal.stories.tsx +51 -0
  41. package/src/bootstrap/useModal.tsx +88 -0
  42. package/src/bootstrap/useWrappedForm.ts +40 -0
  43. package/src/core/cache.test.ts +121 -0
  44. package/src/{cache.ts → core/cache.ts} +29 -10
  45. package/src/electron/README.md +13 -0
  46. package/src/electron/createWindow.ts +234 -0
  47. package/src/electron/index.ts +2 -0
  48. package/src/electron/updater.ts +54 -0
  49. package/src/electron-builder/builder.ts +150 -0
  50. package/src/electron-builder/images.ts +33 -0
  51. package/src/electron-builder/index.ts +2 -0
  52. package/src/mui/README.md +3 -0
  53. package/src/mui/formControlFieldset.stories.tsx +71 -0
  54. package/src/mui/formControlFieldset.tsx +26 -0
  55. package/src/mui/formLabelLegend.tsx +11 -0
  56. package/src/mui/genericControl.tsx +20 -0
  57. package/src/mui/index.ts +4 -0
  58. package/src/mui/readOnly.tsx +6 -0
  59. package/src/next/appLink.tsx +39 -0
  60. package/src/next/index.ts +5 -0
  61. package/src/next/measure.ts +7 -0
  62. package/src/next/noSSR.tsx +17 -0
  63. package/src/next/redirect.tsx +13 -0
  64. package/src/next/useIsInner.ts +13 -0
  65. package/src/react/apiCall.ts +25 -0
  66. package/src/react/form/client.tsx +62 -0
  67. package/src/react/form/form.tsx +109 -0
  68. package/src/react/form/index.ts +2 -0
  69. package/src/react/index.ts +4 -0
  70. package/src/react/useFetch.ts +29 -0
  71. package/src/react/useFetcher.ts +49 -0
  72. package/src/react-native/index.ts +3 -0
  73. package/src/react-native/share.ts +31 -0
  74. package/src/react-native/update.tsx +40 -0
  75. package/src/react-native/useAppState.ts +18 -0
  76. package/src/tailwind/fullpage.tsx +11 -0
  77. package/src/tailwind/index.ts +2 -0
  78. package/src/tailwind/responsiveHelper.tsx +16 -0
  79. package/src-todo/auth0.tsx +177 -0
  80. package/tsconfig.json +15 -18
  81. package/turbo.json +2 -11
  82. package/vite.config.ts +47 -0
  83. package/vite.exports.ts +121 -0
  84. package/build/cache.d.ts +0 -119
  85. package/build/cache.js +0 -176
  86. package/build/errors.d.ts +0 -1
  87. package/build/errors.js +0 -14
  88. package/build/index.d.ts +0 -5
  89. package/build/index.js +0 -5
  90. package/build/measure.d.ts +0 -34
  91. package/build/measure.js +0 -48
  92. package/build/mutex.d.ts +0 -5
  93. package/build/mutex.js +0 -23
  94. package/build/worker.d.ts +0 -83
  95. package/build/worker.js +0 -250
  96. package/tsconfig.tsbuildinfo +0 -1
  97. package/src/{errors.ts → core/errors.ts} +0 -0
  98. package/src/{index.ts → core/index.ts} +1 -1
  99. /package/src/{measure.ts → core/measure.ts} +0 -0
  100. /package/src/{mutex.ts → core/mutex.ts} +0 -0
  101. /package/src/{worker.ts → core/worker.ts} +0 -0
@@ -0,0 +1,109 @@
1
+ import { typeToFlattenedError, z, SafeParseReturnType, TypeOf } from 'zod';
2
+
3
+ const nonEmpty = 'This field cannot be empty';
4
+
5
+ export const stringRequired = () => z.string({ required_error: nonEmpty }).min(1, nonEmpty);
6
+ export const maxLength = (schema: z.ZodString): number => schema.maxLength || 0;
7
+ export const minLength = (schema: z.ZodString): number => schema.minLength || 0;
8
+ export const isRequired = (schema: z.ZodString): boolean => minLength(schema) > 0;
9
+
10
+ export type ZodObject = z.ZodObject<any> | z.ZodEffects<z.ZodObject<any>>; // z.ZodType<any, any, any>
11
+ export type MaybeTypeOf<S extends ZodObject> = Partial<TypeOf<S>>;
12
+ export type SafeTypeOf<S extends ZodObject> = SafeParseReturnType<TypeOf<S>, TypeOf<S>>['data'];
13
+ export type Errors<S extends ZodObject> = typeToFlattenedError<TypeOf<S>>['fieldErrors'];
14
+ export type Validation<S extends ZodObject> =
15
+ | {
16
+ success: true; // this is true only if form was validated successfully
17
+ data: SafeTypeOf<S>;
18
+ errors?: never;
19
+ }
20
+ | {
21
+ success: false;
22
+ data?: MaybeTypeOf<S>;
23
+ errors?: Errors<S>;
24
+ };
25
+
26
+ export function create<S extends ZodObject>(schema: S) {
27
+ const getShape = () =>
28
+ (schema as z.ZodObject<any>).shape || (schema as z.ZodEffects<z.ZodObject<any>>).sourceType().shape;
29
+
30
+ if (!getShape()) {
31
+ throw new Error('Invalid schema: only z.object() or z.object().refine() are supported');
32
+ }
33
+
34
+ function register(name: keyof TypeOf<S>, data?: MaybeTypeOf<S>, errors?: Errors<S>, mui: boolean = false) {
35
+ const field = getShape()[name];
36
+ return {
37
+ name,
38
+ id: name,
39
+ required: isRequired(field),
40
+ maxLength: maxLength(field),
41
+ type: field.isEmail ? 'email' : (name as string).includes('password') ? 'password' : 'text',
42
+ defaultValue: data?.[name],
43
+ ...(mui
44
+ ? {
45
+ label: field.description,
46
+ helperText: errors?.[name]?.join(', '),
47
+ error: !!errors?.[name]?.length,
48
+ }
49
+ : {}),
50
+ };
51
+ }
52
+
53
+ function validationError(data: MaybeTypeOf<S>, errors: Errors<S>): Validation<S> {
54
+ return {
55
+ success: false,
56
+ data, // data is undefined if there are errors
57
+ errors, // Next.js will butcher error object, so we provide something more primitive
58
+ };
59
+ }
60
+
61
+ function validate(formData: FormData): Validation<S> {
62
+ const rawData = Object.fromEntries(formData) as TypeOf<S>;
63
+ const { error, data } = schema.safeParse(rawData);
64
+
65
+ // console.log('Validate result', { error, data, rawData });
66
+
67
+ if (error) {
68
+ // data is undefined if there are errors
69
+ // Next.js will butcher error object, so we provide something more primitive
70
+ return validationError(rawData, error.flatten().fieldErrors as any);
71
+ }
72
+
73
+ return { success: true, data };
74
+ }
75
+
76
+ //FIXME Context?
77
+ function Field({
78
+ children,
79
+ name,
80
+ errors,
81
+ hint,
82
+ }: {
83
+ children?: any;
84
+ name: string;
85
+ errors?: Validation<S>['errors'];
86
+ hint?: string;
87
+ }) {
88
+ const { description } = getShape()[name];
89
+
90
+ return (
91
+ <div className="form-row">
92
+ {description && <label htmlFor={name}>{description}</label>}
93
+ {children}
94
+ {hint && <Hint>{hint}</Hint>}
95
+ {errors?.[name]?.map((e: string) => (
96
+ <Hint error key={e}>
97
+ {e}
98
+ </Hint>
99
+ ))}
100
+ </div>
101
+ );
102
+ }
103
+
104
+ return { register, validate, Field, validationError };
105
+ }
106
+
107
+ export function Hint({ children, error }: { children: any; error?: boolean }) {
108
+ return <div className={`hint ${error ? 'hint-error' : ''}`}>{children}</div>;
109
+ }
@@ -0,0 +1,2 @@
1
+ export * from './client';
2
+ export * from './form';
@@ -0,0 +1,4 @@
1
+ export * from './form';
2
+ export * from './apiCall';
3
+ export * from './useFetch';
4
+ export * from './useFetcher';
@@ -0,0 +1,29 @@
1
+ 'use client';
2
+
3
+ import { useCallback, useState, useTransition } from 'react';
4
+
5
+ //TODO useFetch https://use-http.com
6
+ //TODO SWR?
7
+ //TODO Tanstack Query?
8
+ export function useFetch<R>(
9
+ fn: (...args: any[]) => Promise<R>,
10
+ defaultValue: R | null = null,
11
+ ): [R | null, typeof fn, boolean, Error | undefined] {
12
+ // An async function was passed to useActionState, but it was dispatched outside of an action context.
13
+ // This is likely not what you intended. Either pass the dispatch function to an `action` prop, or dispatch manually inside `startTransition`
14
+ const [isPending, startTransition] = useTransition();
15
+ const [data, setData] = useState<R | null>(defaultValue);
16
+ const [error, setError] = useState<Error>();
17
+
18
+ const actionFn = useCallback(
19
+ (...args: Parameters<typeof fn>) => {
20
+ const promise = fn(...args);
21
+ // https://react.dev/reference/react/useTransition#react-doesnt-treat-my-state-update-after-await-as-a-transition
22
+ startTransition(() => promise.then(setData).catch(setError)); //FIXME sub-chain...
23
+ return promise;
24
+ },
25
+ [fn],
26
+ );
27
+
28
+ return [data, actionFn, isPending, error];
29
+ }
@@ -0,0 +1,49 @@
1
+ import { useCallback, useEffect, useRef, useState } from 'react';
2
+
3
+ export function useFetcher<T = any>(
4
+ cb,
5
+
6
+ { fetchOnMount = false, onError }: { fetchOnMount?: boolean; onError?: (e: Error) => void } = {},
7
+ ) {
8
+ const [loading, setLoading] = useState(fetchOnMount);
9
+ const [error, setError] = useState<Error | null>(null);
10
+ const [data, setData] = useState<T | null>(null);
11
+
12
+ const isMounted = useRef(false);
13
+
14
+ const trigger = useCallback(
15
+ async (...args) => {
16
+ try {
17
+ setLoading(true);
18
+ setError(null);
19
+ const res = await cb(args);
20
+ if (!isMounted.current) return;
21
+ setData(res);
22
+ return res;
23
+ } catch (e) {
24
+ console.error('Fetch failed', e);
25
+ if (!isMounted.current) return;
26
+ setError(e);
27
+ (onError as any)?.(e);
28
+ } finally {
29
+ if (isMounted.current) setLoading(false);
30
+ }
31
+ },
32
+ [cb, onError],
33
+ );
34
+
35
+ useEffect(() => {
36
+ if (fetchOnMount) {
37
+ trigger().catch((e) => console.error('Fetch on mount failed', e)); // catch actually will never happen
38
+ }
39
+ }, [fetchOnMount, cb, trigger]);
40
+
41
+ useEffect(() => {
42
+ isMounted.current = true;
43
+ return () => {
44
+ isMounted.current = false;
45
+ };
46
+ });
47
+
48
+ return { loading, error, data, trigger };
49
+ }
@@ -0,0 +1,3 @@
1
+ export * from './share';
2
+ export * from './update';
3
+ export * from './useAppState';
@@ -0,0 +1,31 @@
1
+ import * as FileSystem from 'expo-file-system';
2
+ import * as Sharing from 'expo-sharing';
3
+
4
+ /**
5
+ * data:text/plain;base64,W29iamV
6
+ * @see https://stackoverflow.com/questions/69738812/problem-to-generate-pdf-from-a-blob-in-an-expo-app-using-filesystem
7
+ * @param base64
8
+ * @param fileName
9
+ * @returns {Promise<void>}
10
+ */
11
+ export const shareDialog = async (base64, fileName) => {
12
+ const path = `${FileSystem.documentDirectory}/${fileName}`;
13
+
14
+ try {
15
+ if (!(await Sharing.isAvailableAsync())) throw new Error('Sharing is not available');
16
+
17
+ const [header, buffer] = base64.split(',');
18
+ const [, mimeTypeStr] = header.split(':');
19
+ const [mimeType] = mimeTypeStr.split(';');
20
+
21
+ await FileSystem.writeAsStringAsync(`${path}`, buffer, {
22
+ encoding: FileSystem.EncodingType.Base64,
23
+ });
24
+
25
+ await Sharing.shareAsync(path, { mimeType, UTI: fileName, dialogTitle: 'Save or share document' });
26
+ } catch (error) {
27
+ alert('Sharing failed: ' + error.message);
28
+ } finally {
29
+ await FileSystem.deleteAsync(path);
30
+ }
31
+ };
@@ -0,0 +1,40 @@
1
+ import * as Updates from 'expo-updates';
2
+ import { useEffect, useState } from 'react';
3
+ import { Text } from 'react-native';
4
+
5
+ export function Update({ children }) {
6
+ //FIXME useUpdates https://blog.expo.dev/feature-preview-updates-js-api-for-expo-updates-3b92beb40dab
7
+ const [updating, setUpdating] = useState(true);
8
+
9
+ useEffect(() => {
10
+ (async () => {
11
+ try {
12
+ if (__DEV__) {
13
+ setUpdating(false);
14
+ return;
15
+ }
16
+
17
+ const update = await Updates.checkForUpdateAsync();
18
+
19
+ if (!update.isAvailable) {
20
+ setUpdating(false);
21
+ return;
22
+ }
23
+
24
+ await Updates.fetchUpdateAsync();
25
+ alert('Update is available. App will reload.');
26
+ await Updates.reloadAsync();
27
+ } catch (error) {
28
+ // You can also add an alert() to see the error message in case of an error when fetching updates.
29
+ alert(`Error fetching latest Expo update: ${error}`);
30
+ setUpdating(false);
31
+ }
32
+ })();
33
+ });
34
+
35
+ if (updating) {
36
+ return <Text>Checking for updates ({Updates.channel})...</Text>;
37
+ }
38
+
39
+ return children;
40
+ }
@@ -0,0 +1,18 @@
1
+ import { useEffect, useState } from 'react';
2
+ import { AppState } from 'react-native';
3
+
4
+ export function useAppState() {
5
+ const [appState, setAppState] = useState(AppState.currentState);
6
+
7
+ useEffect(() => {
8
+ const subscription = AppState.addEventListener('change', (nextAppState) => {
9
+ setAppState(nextAppState);
10
+ });
11
+
12
+ return () => {
13
+ subscription.remove();
14
+ };
15
+ }, []);
16
+
17
+ return appState;
18
+ }
@@ -0,0 +1,11 @@
1
+ import React from 'react';
2
+
3
+ export const Fullpage = ({ divRef = null, className = '', children, ...props }) => (
4
+ <div
5
+ ref={divRef}
6
+ {...props}
7
+ className={`container h-screen flex flex-col justify-center items-center ${className}`}
8
+ >
9
+ {children}
10
+ </div>
11
+ );
@@ -0,0 +1,2 @@
1
+ export * from './fullpage';
2
+ export * from './responsiveHelper';
@@ -0,0 +1,16 @@
1
+ 'use client';
2
+
3
+ import React from 'react';
4
+
5
+ export function ResponsiveHelperTW() {
6
+ return (
7
+ <span className="fixed bottom-0 right-2 z-30 leading-none text-xs">
8
+ <small className="hidden xs:max-sm:block">XS</small>
9
+ <small className="hidden sm:max-md:block">SM</small>
10
+ <small className="hidden md:max-lg:block">MD</small>
11
+ <small className="hidden lg:max-xl:block">LG</small>
12
+ <small className="hidden xl:max-2xl:block">XL</small>
13
+ <small className="hidden 2xl:block">XXL</small>
14
+ </span>
15
+ );
16
+ }
@@ -0,0 +1,177 @@
1
+ // 'use client';
2
+ //
3
+ // import { Auth0Provider, useAuth0 } from '@auth0/auth0-react';
4
+ // import { Button, ButtonProps } from 'react-bootstrap';
5
+ // import { useRouter } from 'next/navigation';
6
+ // import { useEffect } from 'react';
7
+ // import { emitter } from '../api/apiClient';
8
+ // import RestResponse from '../api/restResponse';
9
+ // import { useToaster } from '../redux/toasterSlice';
10
+ // import { end_session } from '../api/miscCommands';
11
+ // import { createAuth0Client, Auth0Client } from '@auth0/auth0-spa-js';
12
+ //
13
+ // const isSSR = typeof window === 'undefined';
14
+ //
15
+ // const origin = !isSSR ? window.location.origin : '';
16
+ //
17
+ // // @ts-ignore
18
+ // export const isReactNative = () => !isSSR && window.ReactNativeWebView?.postMessage;
19
+ //
20
+ // // @ts-ignore
21
+ // export const postMessage = (data: any = null) => window.ReactNativeWebView?.postMessage(JSON.stringify(data));
22
+ //
23
+ // if (isReactNative()) {
24
+ // const originalConsole = { ...console };
25
+ //
26
+ // const makeLogger =
27
+ // (type) =>
28
+ // (...args) => {
29
+ // postMessage(
30
+ // JSON.stringify({
31
+ // type,
32
+ // data: args,
33
+ // })
34
+ // );
35
+ // originalConsole[type].apply(console, args);
36
+ // };
37
+ //
38
+ // console.log = makeLogger('log');
39
+ // console.log = makeLogger('error');
40
+ // console.log = makeLogger('info');
41
+ // console.log = makeLogger('warn');
42
+ // }
43
+ //
44
+ // export const externalAuth =
45
+ // !isSSR && window['auth0data']
46
+ // ? ({
47
+ // user: window['auth0data'].user,
48
+ // getAccessTokenSilently: async () => window['auth0data'].token.accessToken,
49
+ // isLoading: !window['auth0data'].user,
50
+ // isAuthenticated: !!window['auth0data'].user,
51
+ // loginWithRedirect: () => {},
52
+ // logout: () => postMessage({ type: 'logout' }),
53
+ // } as any)
54
+ // : null;
55
+ //
56
+ // export const useAuth = (): ReturnType<typeof useAuth0> => {
57
+ // const auth0 = useAuth0();
58
+ // return externalAuth || auth0;
59
+ // };
60
+ //
61
+ // // To support passing parameters and hints to Auth0 we need to use lower level API
62
+ // const auth0Config = {
63
+ // domain: process.env.NEXT_PUBLIC_AUTH0_DOMAIN as string,
64
+ // clientId: process.env.NEXT_PUBLIC_AUTH0_CLIENT_ID as string,
65
+ // redirectUri: origin,
66
+ // audience: process.env.NEXT_PUBLIC_AUTH0_AUDIENCE,
67
+ // };
68
+ //
69
+ // let auth0ClientInstance: Auth0Client | null = null;
70
+ //
71
+ // const getAuth0Client = async () => {
72
+ // if (!auth0ClientInstance) {
73
+ // auth0ClientInstance = await createAuth0Client(auth0Config);
74
+ // }
75
+ // return auth0ClientInstance;
76
+ // };
77
+ //
78
+ // export const useExtendedAuth = () => {
79
+ // const auth = useAuth0();
80
+ //
81
+ // const extendedLoginWithRedirect = async (options: any) => {
82
+ // if (options.login_hint || options.screen_hint) {
83
+ // const auth0Client = await getAuth0Client();
84
+ // return auth0Client.loginWithRedirect(options);
85
+ // }
86
+ //
87
+ // return auth.loginWithRedirect(options);
88
+ // };
89
+ //
90
+ // return {
91
+ // ...auth,
92
+ // loginWithRedirect: extendedLoginWithRedirect,
93
+ // };
94
+ // };
95
+ //
96
+ // export const Provider = ({ children, authParams = {} }) => {
97
+ // return (
98
+ // <Auth0Provider
99
+ // domain={process.env.NEXT_PUBLIC_AUTH0_DOMAIN as string}
100
+ // clientId={process.env.NEXT_PUBLIC_AUTH0_CLIENT_ID as string}
101
+ // authorizationParams={{
102
+ // ...authParams,
103
+ // redirect_uri: origin,
104
+ // audience: process.env.NEXT_PUBLIC_AUTH0_AUDIENCE,
105
+ // }}
106
+ // >
107
+ // {children}
108
+ // </Auth0Provider>
109
+ // );
110
+ // };
111
+ //
112
+ // export const LoginButton = (props: ButtonProps) => {
113
+ // const { loginWithRedirect } = useAuth(); //FIXME add state
114
+ // return (
115
+ // <Button {...props} onClick={() => loginWithRedirect()}>
116
+ // Log In
117
+ // </Button>
118
+ // );
119
+ // };
120
+ //
121
+ // export const LogoutButton = ({ as: Cmp = Button, children, ...props }: any) => {
122
+ // const { logout, isAuthenticated, isLoading } = useAuth();
123
+ // if (!isAuthenticated || isLoading) return null;
124
+ //
125
+ // const handleLogout = async () => {
126
+ // try {
127
+ // await end_session();
128
+ // } catch (e) {
129
+ // console.error('Cannot end session', e); // will get here if in Native
130
+ // }
131
+ // logout({ logoutParams: { returnTo: origin } });
132
+ // };
133
+ //
134
+ // return (
135
+ // <Cmp {...props} onClick={handleLogout} role="button">
136
+ // {children || 'Log Out'}
137
+ // </Cmp>
138
+ // );
139
+ // };
140
+ //
141
+ // export const useLogoutTracker = () => {
142
+ // const router = useRouter();
143
+ // const { logout } = useAuth();
144
+ // const { setToast } = useToaster();
145
+ //
146
+ // useEffect(() => {
147
+ // let onError;
148
+ //
149
+ // emitter.on(
150
+ // 'error',
151
+ // (onError = (restResponse: RestResponse) => {
152
+ // if (restResponse.code === 401 || restResponse.message.includes('(Unauthorized)')) {
153
+ // if (restResponse.message.includes('OpenAI API Key')) {
154
+ // setToast('Your account is not provisioned yet. Please check your email for instructions.');
155
+ // return;
156
+ // }
157
+ //
158
+ // if (externalAuth) {
159
+ // externalAuth.logout();
160
+ // return;
161
+ // }
162
+ //
163
+ // logout({
164
+ // async openUrl() {
165
+ // router.replace('/' as any); //FIXME Return to the same page
166
+ // setToast('You have been logged out');
167
+ // },
168
+ // });
169
+ // }
170
+ // })
171
+ // );
172
+ //
173
+ // return () => {
174
+ // emitter.off('error', onError);
175
+ // };
176
+ // }, [logout, router, setToast]);
177
+ // };
package/tsconfig.json CHANGED
@@ -1,24 +1,21 @@
1
1
  {
2
+ "extends": "../../tsconfig.json",
2
3
  "compilerOptions": {
4
+ "rootDir": "src",
5
+ "outDir": "dist",
6
+ "declaration": true,
7
+ "declarationMap": true,
8
+ "declarationDir": "dist",
9
+ "incremental": false,
3
10
  "allowJs": true,
4
- "allowSyntheticDefaultImports": true,
5
- "esModuleInterop": true,
6
- "forceConsistentCasingInFileNames": true,
11
+ /* Build-specific options */
12
+ "moduleResolution": "bundler",
13
+ "allowImportingTsExtensions": true,
7
14
  "isolatedModules": true,
8
- "jsx": "preserve",
9
- "target": "es2018",
10
- "lib": ["dom", "dom.iterable", "esnext", "webworker"],
11
- "module": "esnext",
12
- "moduleResolution": "node",
13
- "resolveJsonModule": true,
14
- "skipLibCheck": true,
15
- "strict": false,
16
- "strictNullChecks": true,
17
- "incremental": true,
18
- "outDir": "./build",
19
- "declaration": true,
20
- "declarationDir": "./build",
21
- "rootDir": "src"
15
+ "moduleDetection": "force",
16
+ "noEmit": true,
17
+ "jsx": "react-jsx"
22
18
  },
23
- "include": ["src"]
19
+ "include": ["src"],
20
+ "exclude": ["**/*.test.ts", "**/*.stories.ts", "**/*.stories.tsx"]
24
21
  }
package/turbo.json CHANGED
@@ -2,18 +2,9 @@
2
2
  "$schema": "https://turbo.build/schema.json",
3
3
  "extends": ["//"],
4
4
  "tasks": {
5
- "clean": {
6
- "cache": false
7
- },
8
5
  "build": {
9
- "outputs": ["build/**/*"]
10
- },
11
- "start": {
12
- "persistent": true,
13
- "cache": false
14
- },
15
- "wait": {
16
- "cache": false
6
+ "dependsOn": ["^build"],
7
+ "outputs": ["src/**/*/index.ts", "package.json", "dist/**/*"]
17
8
  }
18
9
  }
19
10
  }
package/vite.config.ts ADDED
@@ -0,0 +1,47 @@
1
+ import { defineConfig } from 'vitest/config';
2
+ import react from '@vitejs/plugin-react';
3
+ import dts from 'vite-plugin-dts';
4
+ import { fixExports, formats, entry, external, distDir, generateIndex } from './vite.exports';
5
+
6
+ // https://rbardini.com/how-to-build-ts-library-with-vite/
7
+ // https://dev.to/receter/how-to-create-a-react-component-library-using-vites-library-mode-4lma
8
+
9
+ export default defineConfig({
10
+ build: {
11
+ ssr: true,
12
+ sourcemap: true,
13
+ outDir: distDir,
14
+ emptyOutDir: true,
15
+ // target: 'esnext',
16
+ lib: {
17
+ entry,
18
+ formats: Object.keys(formats) as any,
19
+ },
20
+ rollupOptions: {
21
+ external,
22
+ output: {
23
+ preserveModules: true,
24
+ preserveModulesRoot: 'src',
25
+ },
26
+ },
27
+ },
28
+ test: {
29
+ coverage: {
30
+ reporter: ['text', 'html', 'cobertura'],
31
+ } as any,
32
+ },
33
+ resolve: { extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'] }, // https://vite.dev/guide/performance#reduce-resolve-operations
34
+ plugins: [
35
+ react(),
36
+ dts(), //TODO Check https://github.com/alloc/vite-dts
37
+ {
38
+ name: 'Generate Index & Exports',
39
+ async buildStart() {
40
+ await generateIndex();
41
+ },
42
+ async closeBundle() {
43
+ await fixExports();
44
+ },
45
+ },
46
+ ],
47
+ });