react-intlayer 2.0.0 → 2.0.1

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/dist/cjs/client/ContentEditionLayout.cjs +6 -2
  2. package/dist/cjs/client/ContentEditionLayout.cjs.map +1 -1
  3. package/dist/cjs/client/{IntlayerClientProvider.cjs → IntlayerProvider.cjs} +11 -9
  4. package/dist/cjs/client/IntlayerProvider.cjs.map +1 -0
  5. package/dist/cjs/client/{IntlayerClientProvider.d.ts → IntlayerProvider.d.ts} +4 -3
  6. package/dist/cjs/client/index.cjs +6 -6
  7. package/dist/cjs/client/index.cjs.map +1 -1
  8. package/dist/cjs/client/index.d.ts +2 -1
  9. package/dist/cjs/client/useIntlayer.cjs +2 -2
  10. package/dist/cjs/client/useIntlayer.cjs.map +1 -1
  11. package/dist/cjs/client/useIntlayer.d.ts +2 -1
  12. package/dist/cjs/client/useLocale.cjs +2 -2
  13. package/dist/cjs/client/useLocale.cjs.map +1 -1
  14. package/dist/cjs/client/useLocaleBase.cjs +2 -2
  15. package/dist/cjs/client/useLocaleBase.cjs.map +1 -1
  16. package/dist/cjs/client/useTraduction.cjs +2 -2
  17. package/dist/cjs/client/useTraduction.cjs.map +1 -1
  18. package/dist/cjs/index.cjs +7 -4
  19. package/dist/cjs/index.cjs.map +1 -1
  20. package/dist/cjs/index.d.ts +3 -2
  21. package/dist/cjs/processDictionary/contentDictionary.cjs.map +1 -1
  22. package/dist/cjs/processDictionary/contentDictionary.d.ts +9 -9
  23. package/dist/cjs/processDictionary/index.cjs +67 -51
  24. package/dist/cjs/processDictionary/index.cjs.map +1 -1
  25. package/dist/cjs/processDictionary/index.d.ts +4 -4
  26. package/dist/cjs/server/IntlayerServerProvider.cjs +3 -4
  27. package/dist/cjs/server/IntlayerServerProvider.cjs.map +1 -1
  28. package/dist/cjs/server/IntlayerServerProvider.d.ts +1 -1
  29. package/dist/cjs/server/index.d.ts +1 -0
  30. package/dist/cjs/server/useIntlayer.d.ts +2 -1
  31. package/dist/cjs/useIntlayerBase.cjs +45 -1
  32. package/dist/cjs/useIntlayerBase.cjs.map +1 -1
  33. package/dist/cjs/useIntlayerBase.d.ts +51 -4
  34. package/dist/esm/client/ContentEditionLayout.mjs +5 -1
  35. package/dist/esm/client/ContentEditionLayout.mjs.map +1 -1
  36. package/dist/esm/client/{IntlayerClientProvider.d.mts → IntlayerProvider.d.mts} +4 -3
  37. package/dist/esm/client/{IntlayerClientProvider.mjs → IntlayerProvider.mjs} +7 -5
  38. package/dist/esm/client/IntlayerProvider.mjs.map +1 -0
  39. package/dist/esm/client/index.d.mts +2 -1
  40. package/dist/esm/client/index.mjs +4 -4
  41. package/dist/esm/client/index.mjs.map +1 -1
  42. package/dist/esm/client/useIntlayer.d.mts +2 -1
  43. package/dist/esm/client/useIntlayer.mjs +1 -1
  44. package/dist/esm/client/useIntlayer.mjs.map +1 -1
  45. package/dist/esm/client/useLocale.mjs +1 -1
  46. package/dist/esm/client/useLocale.mjs.map +1 -1
  47. package/dist/esm/client/useLocaleBase.mjs +1 -1
  48. package/dist/esm/client/useLocaleBase.mjs.map +1 -1
  49. package/dist/esm/client/useTraduction.mjs +1 -1
  50. package/dist/esm/client/useTraduction.mjs.map +1 -1
  51. package/dist/esm/index.d.mts +3 -2
  52. package/dist/esm/index.mjs +6 -4
  53. package/dist/esm/index.mjs.map +1 -1
  54. package/dist/esm/processDictionary/contentDictionary.d.mts +9 -9
  55. package/dist/esm/processDictionary/index.d.mts +4 -4
  56. package/dist/esm/processDictionary/index.mjs +67 -51
  57. package/dist/esm/processDictionary/index.mjs.map +1 -1
  58. package/dist/esm/server/IntlayerServerProvider.d.mts +1 -1
  59. package/dist/esm/server/IntlayerServerProvider.mjs +3 -4
  60. package/dist/esm/server/IntlayerServerProvider.mjs.map +1 -1
  61. package/dist/esm/server/index.d.mts +1 -0
  62. package/dist/esm/server/useIntlayer.d.mts +2 -1
  63. package/dist/esm/useIntlayerBase.d.mts +51 -4
  64. package/dist/esm/useIntlayerBase.mjs +43 -1
  65. package/dist/esm/useIntlayerBase.mjs.map +1 -1
  66. package/package.json +8 -9
  67. package/src/index.ts +4 -3
  68. package/dist/cjs/IntlayerProvider.cjs +0 -37
  69. package/dist/cjs/IntlayerProvider.cjs.map +0 -1
  70. package/dist/cjs/IntlayerProvider.d.ts +0 -9
  71. package/dist/cjs/client/IntlayerClientProvider.cjs.map +0 -1
  72. package/dist/esm/IntlayerProvider.d.mts +0 -9
  73. package/dist/esm/IntlayerProvider.mjs +0 -18
  74. package/dist/esm/IntlayerProvider.mjs.map +0 -1
  75. package/dist/esm/client/IntlayerClientProvider.mjs.map +0 -1
  76. package/src/IntlayerProvider.tsx +0 -24
  77. package/src/cli/react-intlayer.ts +0 -60
  78. package/src/client/ContentEditionLayout.tsx +0 -15
  79. package/src/client/IntlayerClientProvider.tsx +0 -83
  80. package/src/client/getBrowserLocale.tsx +0 -192
  81. package/src/client/index.ts +0 -16
  82. package/src/client/useContent.ts +0 -20
  83. package/src/client/useIntlayer.ts +0 -25
  84. package/src/client/useLocale.ts +0 -23
  85. package/src/client/useLocaleBase.ts +0 -24
  86. package/src/client/useLocaleCookie.ts +0 -32
  87. package/src/client/useTraduction.ts +0 -33
  88. package/src/craco/craco.config.ts +0 -11
  89. package/src/craco/intlayerCracoPlugin.ts +0 -89
  90. package/src/getEnumeration.ts +0 -41
  91. package/src/getTranslation.ts +0 -37
  92. package/src/processDictionary/contentDictionary.ts +0 -28
  93. package/src/processDictionary/index.ts +0 -231
  94. package/src/server/IntlayerServerProvider.tsx +0 -36
  95. package/src/server/getLocaleTranslation.ts +0 -22
  96. package/src/server/index.ts +0 -8
  97. package/src/server/serverContext.ts +0 -85
  98. package/src/server/useIntlayer.ts +0 -23
  99. package/src/server/useTraduction.ts +0 -34
  100. package/src/useIntlayerBase.ts +0 -38
  101. package/src/vite/intlayerPlugin.ts +0 -59
@@ -1,37 +0,0 @@
1
- import { type Locales, getConfiguration } from '@intlayer/config/client';
2
- import { type LanguageContent, getTranslationContent } from '@intlayer/core';
3
-
4
- /**
5
- *
6
- * Allow to pick a content based on a locale.
7
- * If not locale found, it will return the content related to the default locale.
8
- *
9
- * Return either the content editor, or the content itself depending on the configuration.
10
- *
11
- * Usage:
12
- *
13
- * ```ts
14
- * const content = getTranslation<string>({
15
- * en: 'Hello',
16
- * fr: 'Bonjour',
17
- * }, 'fr');
18
- * // 'Bonjour'
19
- * ```
20
- *
21
- * Using TypeScript:
22
- * - this function will require each locale to be defined if defined in the project configuration.
23
- * - If a locale is missing, it will make each existing locale optional and raise an error if the locale is not found.
24
- */
25
- export const getTranslation = <Content = string>(
26
- languageContent: LanguageContent<Content>,
27
- locale?: Locales
28
- ): Content => {
29
- const { defaultLocale } = getConfiguration().internationalization;
30
-
31
- const result: Content = getTranslationContent<Content>(
32
- languageContent,
33
- locale ?? defaultLocale
34
- );
35
-
36
- return result;
37
- };
@@ -1,28 +0,0 @@
1
- import type { TranslationContent } from '@intlayer/core';
2
- import type { ReactNode } from 'react';
3
-
4
- export type ContentValue =
5
- | string
6
- | {
7
- [key: string]: ContentValue;
8
- }
9
- | TranslationContent<unknown>;
10
-
11
- export type Content = Record<string, ContentValue | undefined>;
12
-
13
- export type TransformedContentValue =
14
- | string
15
- | ReactNode
16
- | {
17
- [key: string]: TransformedContentValue;
18
- }
19
- | undefined
20
- | ((quantity: number) => TransformedContentValue);
21
-
22
- export type TransformedContent =
23
- | Record<string, TransformedContentValue | undefined>
24
- | ReactNode;
25
-
26
- export type ContentDictionary = Content & {
27
- id: string;
28
- };
@@ -1,231 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { type Locales, getConfiguration } from '@intlayer/config/client';
3
- import {
4
- NodeType,
5
- type QuantityContent,
6
- type LanguageContent,
7
- findMatchingCondition,
8
- type KeyPath,
9
- } from '@intlayer/core';
10
- import { renderContentEditor } from 'intlayer-editor/client';
11
- import { type ReactElement, createElement, type ReactNode } from 'react';
12
- import { getEnumeration } from '../getEnumeration';
13
- import { getTranslation } from '../getTranslation';
14
- import type {
15
- Content,
16
- ContentValue,
17
- TransformedContent,
18
- TransformedContentValue,
19
- } from './contentDictionary';
20
-
21
- const {
22
- internationalization: { defaultLocale },
23
- } = getConfiguration();
24
-
25
- const processTranslation = (
26
- languageContent: LanguageContent<ContentValue>,
27
- locale: Locales,
28
- dictionaryId: string,
29
- dictionaryPath: string,
30
- keyPath: KeyPath[] = []
31
- ): TransformedContent => {
32
- const translationResult: ContentValue = getTranslation<ContentValue>(
33
- languageContent,
34
- locale
35
- );
36
-
37
- const resultKeyPath: KeyPath[] = [
38
- ...keyPath,
39
- { type: NodeType.Translation, key: locale },
40
- ];
41
-
42
- return processDictionary(
43
- translationResult as Content,
44
- dictionaryId,
45
- dictionaryPath,
46
- resultKeyPath,
47
- locale
48
- );
49
- };
50
-
51
- const processEnumeration = (
52
- enumerationContent: QuantityContent<ContentValue>,
53
- locale: Locales,
54
- dictionaryId: string,
55
- dictionaryPath: string,
56
- keyPath: KeyPath[] = []
57
- ): TransformedContentValue => {
58
- return (quantity: number): TransformedContentValue => {
59
- const enumerationResult: ContentValue = getEnumeration<ContentValue>(
60
- enumerationContent,
61
- quantity
62
- );
63
-
64
- const matchingCondition = findMatchingCondition(
65
- enumerationContent,
66
- quantity
67
- );
68
-
69
- const resultKeyPath: KeyPath[] = [
70
- ...keyPath,
71
- { type: NodeType.Enumeration, key: matchingCondition.toString() },
72
- ];
73
-
74
- //
75
- return processDictionary(
76
- enumerationResult as Content,
77
- dictionaryId,
78
- dictionaryPath,
79
- resultKeyPath,
80
- locale
81
- );
82
- };
83
- };
84
-
85
- const isReactNode = (node: Record<string, unknown>): boolean =>
86
- typeof node?.key !== 'undefined' && typeof node?.props !== 'undefined';
87
-
88
- export const processNode = (
89
- field: ContentValue | undefined,
90
- locale: Locales,
91
- dictionaryId: string,
92
- dictionaryPath: string,
93
- keyPath: KeyPath[] = []
94
- ): TransformedContentValue => {
95
- if (typeof field === 'object') {
96
- if (field.nodeType === NodeType.Translation) {
97
- return processTranslation(
98
- field as LanguageContent<ContentValue>,
99
- locale,
100
- dictionaryId,
101
- dictionaryPath,
102
- keyPath
103
- );
104
- }
105
-
106
- if (field.nodeType === NodeType.Enumeration) {
107
- return processEnumeration(
108
- field satisfies QuantityContent<ContentValue>,
109
- locale,
110
- dictionaryId,
111
- dictionaryPath,
112
- keyPath
113
- );
114
- }
115
- }
116
-
117
- return processDictionary(
118
- field as Content,
119
- dictionaryId,
120
- dictionaryPath,
121
- keyPath,
122
- locale
123
- );
124
- };
125
-
126
- // This function recursively creates React elements from a given JSON-like structure
127
- const createReactElement = (element: ReactElement) => {
128
- if (typeof element === 'string') {
129
- // If it's a string, simply return it (used for text content)
130
- return element;
131
- }
132
-
133
- // Destructure the component properties
134
-
135
- const convertChildrenAsArray = (element: ReactElement): ReactElement => {
136
- if (element?.props && typeof element.props.children === 'object') {
137
- const childrenResult: ReactNode[] = [];
138
- const { children } = element.props;
139
-
140
- // Create the children elements recursively, if any
141
- Object.keys(children).forEach((key) => {
142
- childrenResult.push(createReactElement(children[key]));
143
- });
144
-
145
- return {
146
- ...element,
147
- props: { ...element.props, children: childrenResult },
148
- };
149
- }
150
-
151
- return {
152
- ...element,
153
- props: { ...element.props, children: element.props.children },
154
- };
155
- };
156
-
157
- const fixedElement = convertChildrenAsArray(element);
158
-
159
- const { type, props } = fixedElement;
160
-
161
- // Create and return the React element
162
- return createElement(type ?? 'div', props, ...props.children);
163
- };
164
-
165
- const traceKeys: string[] = ['filePath', 'id', 'nodeType'];
166
-
167
- /**
168
- * Function that process a dictionary and return the result to be used in the application.
169
- */
170
- export const processDictionary = (
171
- content: Content,
172
- dictionaryId: string,
173
- dictionaryPath: string,
174
- keyPath: KeyPath[] = [],
175
- locale: Locales = defaultLocale
176
- ): TransformedContent => {
177
- // If it's a React element, render it
178
- if (isReactNode(content)) {
179
- return createReactElement(
180
- content as unknown as ReactElement
181
- ) as unknown as TransformedContent;
182
- }
183
-
184
- if (content && typeof content === 'object') {
185
- const result: TransformedContent = {};
186
-
187
- // List each key in the content and process it
188
- for (const key of Object.keys(content)) {
189
- const field = content[key];
190
-
191
- if (traceKeys.includes(key)) {
192
- result[key] = field as TransformedContentValue;
193
- continue;
194
- }
195
-
196
- const resultKeyPath: KeyPath[] = [
197
- ...keyPath,
198
- { type: 'ObjectExpression', key },
199
- ];
200
-
201
- result[key] = processNode(
202
- field,
203
- locale,
204
- dictionaryId,
205
- dictionaryPath,
206
- resultKeyPath
207
- );
208
- }
209
-
210
- return result;
211
- }
212
-
213
- if (typeof content === 'string') {
214
- try {
215
- // renderContentEditor come from intlayer-editor, which is an optional dependency.
216
- // intlayer-editor should be installed in the project to use the content editor.
217
- return renderContentEditor(
218
- content,
219
- dictionaryId,
220
- dictionaryPath,
221
- keyPath
222
- );
223
- } catch (e) {
224
- // If renderContentEditor not available, it will return the content as is.
225
- return content;
226
- }
227
- }
228
-
229
- // If it's a string, number, or function, return it
230
- return content;
231
- };
@@ -1,36 +0,0 @@
1
- import { type Locales, getConfiguration } from '@intlayer/config/client';
2
- import type { FC, PropsWithChildren } from 'react';
3
- import { createServerContext, getServerContext } from './serverContext';
4
-
5
- /**
6
- * Context that store the current locale on the server side
7
- */
8
- export const IntlayerServerContext = createServerContext<Locales>(
9
- getConfiguration().internationalization.defaultLocale
10
- );
11
-
12
- /**
13
- * Hook that provides the current locale
14
- */
15
- export const useIntlayer = () => getServerContext(IntlayerServerContext);
16
-
17
- /**
18
- * Get the current locale
19
- */
20
- export const locale = getServerContext(IntlayerServerContext);
21
-
22
- export type IntlayerServerProviderProps = PropsWithChildren & {
23
- locale: Locales;
24
- };
25
-
26
- /**
27
- * Provider that store the current locale on the server side
28
- */
29
- export const IntlayerServerProvider: FC<IntlayerServerProviderProps> = ({
30
- children,
31
- locale,
32
- }) => (
33
- <IntlayerServerContext.Provider value={locale}>
34
- {children}
35
- </IntlayerServerContext.Provider>
36
- );
@@ -1,22 +0,0 @@
1
- import { getConfiguration } from '@intlayer/config/client';
2
- import { type LanguageContent, getTranslationContent } from '@intlayer/core';
3
- import { IntlayerServerContext } from './IntlayerServerProvider';
4
- import { getServerContext } from './serverContext';
5
-
6
- /**
7
- * On the server side, get the translation content based on the locale
8
- */
9
- export const getLocaleTranslation = <Content>(
10
- languageContent: LanguageContent<Content>
11
- ) => {
12
- const locale = getServerContext(IntlayerServerContext);
13
- const content = getTranslationContent<Content>(
14
- languageContent,
15
- locale ?? getConfiguration().internationalization.defaultLocale
16
- );
17
-
18
- return {
19
- locale,
20
- content,
21
- };
22
- };
@@ -1,8 +0,0 @@
1
- export { getLocaleTranslation as getLocaleContent } from './getLocaleTranslation';
2
- export { useTraduction } from './useTraduction';
3
- export {
4
- IntlayerServerContext as IntlayerServer,
5
- locale,
6
- IntlayerServerProvider,
7
- } from './IntlayerServerProvider';
8
- export { useIntlayer } from './useIntlayer';
@@ -1,85 +0,0 @@
1
- /**
2
- * Creates a new datastore for a given server context.
3
- * Attempts to closely mimic the `createContext` API.
4
- *
5
- * @example
6
- * const IntlayerServer = createServerContext<string | null>(null);
7
- *
8
- * <IntlayerServer.Provider value={locale}>
9
- * {children}
10
- * </IntlayerServer.Provider>
11
- */
12
-
13
- import react from 'react';
14
-
15
- type CacheType<T> = () => { value: T | undefined };
16
-
17
- const cacheFallback = () => () => ({ value: undefined });
18
-
19
- export const createServerContext = <T>(defaultValue?: T): ServerContext<T> => {
20
- throwInClient();
21
-
22
- const cache = react.cache<CacheType<T>> ?? cacheFallback;
23
-
24
- const getCache = cache(() => ({
25
- value: undefined,
26
- }));
27
-
28
- return {
29
- Provider: ({ children, value }) => {
30
- getCache().value = value;
31
- return children;
32
- },
33
- Consumer: ({ children }) => {
34
- const store = getCache();
35
- return children(store ? store.value : defaultValue);
36
- },
37
- _storage: getCache,
38
- _defaultValue: defaultValue,
39
- };
40
- };
41
-
42
- /**
43
- * Fetches a value present in a given server context.
44
- * Attempts to closely mimic the `useContext` API.
45
- *
46
- * @example
47
- * getServerContext(IntlayerServer);
48
- */
49
- export const getServerContext = <T>({
50
- _storage,
51
- _defaultValue,
52
- }: ServerContext<T>) => {
53
- // throwInClient();
54
- const store = _storage();
55
- if (!store) return _defaultValue;
56
- return store.value;
57
- };
58
-
59
- type ServerContext<T> = {
60
- Provider: ({
61
- children,
62
- value,
63
- }: {
64
- children: React.ReactNode;
65
- value: T;
66
- }) => React.ReactNode;
67
- Consumer: ({
68
- children,
69
- }: {
70
- children: (context: T | undefined) => React.ReactNode;
71
- }) => React.ReactNode;
72
- _storage: () => { value: T | undefined };
73
- _defaultValue: T | undefined;
74
- };
75
-
76
- /**
77
- * Throws if called within a client component environment.
78
- * Useful to help prevent mistakes.
79
- */
80
- const throwInClient = (): void | never => {
81
- // If window.document is defined we're in a client component
82
- if (typeof window !== 'undefined') {
83
- throw new Error(`createServerContext only works in Server Components`);
84
- }
85
- };
@@ -1,23 +0,0 @@
1
- import type { Locales } from '@intlayer/config/client';
2
- import {
3
- type DictionaryKeys,
4
- useIntlayerBase,
5
- type UseIntlayer,
6
- } from '../useIntlayerBase';
7
- import { IntlayerServerContext } from './IntlayerServerProvider';
8
- import { getServerContext } from './serverContext';
9
-
10
- /**
11
- * On the server side, Hook that picking one dictionary by its id and return the content
12
- *
13
- * If the locale is not provided, it will use the locale from the server context
14
- */
15
- export const useIntlayer: UseIntlayer = <T extends DictionaryKeys>(
16
- id: T,
17
- locale?: Locales
18
- ) => {
19
- const localeTarget =
20
- locale ?? getServerContext<Locales>(IntlayerServerContext);
21
-
22
- return useIntlayerBase(id, localeTarget);
23
- };
@@ -1,34 +0,0 @@
1
- import type { Locales } from '@intlayer/config/client';
2
- import type { LanguageContent } from '@intlayer/core';
3
- import { getTranslation } from '../getTranslation';
4
- import { IntlayerServerContext } from './IntlayerServerProvider';
5
- import { getServerContext } from './serverContext';
6
-
7
- /**
8
- * On the server side, hook to get the translation content based on the locale
9
- *
10
- * If not locale found, it will return the content related to the default locale.
11
- *
12
- * Return either the content editor, or the content itself depending on the configuration.
13
- *
14
- * Usage:
15
- *
16
- * ```ts
17
- * const content = useTraduction<string>({
18
- * en: 'Hello',
19
- * fr: 'Bonjour',
20
- * }, 'fr');
21
- * // 'Bonjour'
22
- * ```
23
- *
24
- * Using TypeScript:
25
- * - this function will require each locale to be defined if defined in the project configuration.
26
- * - If a locale is missing, it will make each existing locale optional and raise an error if the locale is not found.
27
- */
28
- export const useTraduction = <Content = string>(
29
- languageContent: LanguageContent<Content>
30
- ) => {
31
- const locale = getServerContext<Locales>(IntlayerServerContext);
32
-
33
- return getTranslation(languageContent, locale);
34
- };
@@ -1,38 +0,0 @@
1
- import type { Locales } from '@intlayer/config/client';
2
- /**
3
- * @intlayer/dictionaries-entry is a package that only returns the dictionary entry path.
4
- * Using an external package allow to alias it in the bundle configuration (such as webpack).
5
- * The alias allow hot reload the app (such as nextjs) on any dictionary change.
6
- */
7
- import dictionaries from '@intlayer/dictionaries-entry';
8
- import type { IntlayerDictionaryTypesConnector } from 'intlayer';
9
- import type { Content } from './processDictionary/contentDictionary';
10
- import { processDictionary } from './processDictionary/index';
11
-
12
- export type StringFallback<T> = T extends never ? string : T; // If no keys are found, return string to disable error, and accept any string as dictionary key
13
- export type DictionaryKeys = StringFallback<
14
- keyof IntlayerDictionaryTypesConnector
15
- >;
16
-
17
- export type UseIntlayer = <T extends DictionaryKeys>(
18
- id: T,
19
- locale?: Locales
20
- ) => IntlayerDictionaryTypesConnector[T];
21
-
22
- /**
23
- * Hook that picking one dictionary by its id and return the content.
24
- */
25
- export const useIntlayerBase: UseIntlayer = <T extends DictionaryKeys>(
26
- id: T,
27
- locale?: Locales
28
- ) => {
29
- const dictionary: Content = dictionaries[id];
30
-
31
- return processDictionary(
32
- dictionary,
33
- id,
34
- dictionary.filePath as string,
35
- [],
36
- locale
37
- ) as IntlayerDictionaryTypesConnector[T];
38
- };
@@ -1,59 +0,0 @@
1
- import { join, relative, resolve } from 'path';
2
- import { watch } from '@intlayer/chokidar';
3
- import { getConfiguration, formatEnvVariable } from '@intlayer/config';
4
- import { loadEnv, type Plugin } from 'vite';
5
-
6
- // Plugin options type definition
7
- type PluginOptions = {
8
- // Custom options for your plugin, if any
9
- };
10
-
11
- /**
12
- *
13
- * A Vite plugin that integrates IntLayer configuration into the build process
14
- *
15
- * ```ts
16
- * // Example usage of the plugin in a Vite configuration
17
- * export default defineConfig({
18
- * plugins: [ intLayerPlugin() ],
19
- * });
20
- * ```
21
- * */
22
- export const intLayerPlugin = (_pluginOptions: PluginOptions = {}): Plugin => ({
23
- name: 'vite-intlayer-plugin',
24
-
25
- config: (config, { mode }) => {
26
- const intlayerConfig = getConfiguration();
27
-
28
- // Set all configuration values as environment variables
29
- const env = formatEnvVariable('vite');
30
-
31
- process.env = { ...process.env, ...loadEnv(mode, process.cwd()), ...env };
32
-
33
- const { mainDir, baseDir } = intlayerConfig.content;
34
- const dictionariesPath = join(mainDir, 'dictionaries.mjs');
35
- const relativeDictionariesPath = relative(baseDir, dictionariesPath);
36
-
37
- // Update Vite's resolve alias
38
- config.resolve = {
39
- ...config.resolve,
40
- alias: {
41
- ...config.resolve?.alias,
42
- '@intlayer/dictionaries-entry': resolve(relativeDictionariesPath),
43
- },
44
- };
45
-
46
- return config;
47
- },
48
-
49
- buildStart: () => {
50
- // Code to run when Vite build starts
51
- watch({
52
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
53
- persistent: (import.meta as any).env === 'development',
54
- });
55
- },
56
- configureServer: () => {
57
- // Custom server configuration, if needed
58
- },
59
- });