@koine/i18n 2.0.0-beta.76 → 2.0.0-beta.78

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 (260) hide show
  1. package/README.md +1 -0
  2. package/api.cjs.js +2203 -0
  3. package/api.esm.js +2179 -0
  4. package/compiler-sync.cjs.d.ts +1 -0
  5. package/compiler-sync.cjs.default.js +1 -0
  6. package/compiler-sync.cjs.js +20 -0
  7. package/compiler-sync.cjs.mjs +2 -0
  8. package/compiler-sync.esm.js +16 -0
  9. package/compiler-worker.cjs.d.ts +1 -0
  10. package/compiler-worker.cjs.default.js +1 -0
  11. package/compiler-worker.cjs.js +18 -0
  12. package/compiler-worker.cjs.mjs +2 -0
  13. package/compiler-worker.esm.js +16 -0
  14. package/compiler.cjs.d.ts +1 -0
  15. package/compiler.cjs.default.js +1 -0
  16. package/compiler.cjs.js +21 -0
  17. package/compiler.cjs.mjs +2 -0
  18. package/compiler.esm.js +13 -0
  19. package/formatRoutePathname.cjs.js +12 -0
  20. package/formatRoutePathname.esm.js +10 -0
  21. package/index.cjs.d.ts +1 -0
  22. package/index.cjs.default.js +1 -0
  23. package/index.cjs.js +47 -0
  24. package/index.cjs.mjs +2 -0
  25. package/index.esm.js +42 -0
  26. package/next.cjs.d.ts +1 -0
  27. package/next.cjs.default.js +1 -0
  28. package/next.cjs.js +377 -0
  29. package/next.cjs.mjs +2 -0
  30. package/next.esm.js +353 -0
  31. package/package.json +22 -305
  32. package/actions/helpers-git.js +0 -45
  33. package/actions/helpers-i18n.js +0 -27
  34. package/actions/i18n.js +0 -17
  35. package/adapter-js/code/config.cjs.js +0 -14
  36. package/adapter-js/code/config.js +0 -14
  37. package/adapter-js/code/defaultLocale.js +0 -7
  38. package/adapter-js/code/deriveLocalisedPathnames.js +0 -78
  39. package/adapter-js/code/index.js +0 -123
  40. package/adapter-js/code/isLocale.js +0 -9
  41. package/adapter-js/code/locales.js +0 -8
  42. package/adapter-js/code/pathnameToRouteId.js +0 -20
  43. package/adapter-js/code/routes.js +0 -11
  44. package/adapter-js/code/routesSlim.js +0 -11
  45. package/adapter-js/code/routesSpa.js +0 -11
  46. package/adapter-js/code/tFns.js +0 -34
  47. package/adapter-js/code/tInterpolateParams.js +0 -18
  48. package/adapter-js/code/tPluralise.js +0 -10
  49. package/adapter-js/code/to.js +0 -32
  50. package/adapter-js/code/toFns.js +0 -25
  51. package/adapter-js/code/toFormat.js +0 -56
  52. package/adapter-js/code/toSpa.js +0 -42
  53. package/adapter-js/code/types.js +0 -327
  54. package/adapter-next/code/index.js +0 -54
  55. package/adapter-next/code/next-redirects.js +0 -5
  56. package/adapter-next/code/next-rewrites.js +0 -5
  57. package/adapter-next/code/useCurrentLocalisedPathnames.js +0 -20
  58. package/adapter-next/code/useLocale.js +0 -8
  59. package/adapter-next/code/useRouteId.js +0 -10
  60. package/adapter-next/code/useTo.js +0 -26
  61. package/adapter-next/code/useToSpa.js +0 -34
  62. package/adapter-next/plugin-async.js +0 -11
  63. package/adapter-next/plugin-legacy.js +0 -187
  64. package/adapter-next/plugin-shared.js +0 -39
  65. package/adapter-next/plugin.js +0 -13
  66. package/adapter-next/redirects.js +0 -51
  67. package/adapter-next/rewrites.js +0 -50
  68. package/adapter-next/transformPathname.js +0 -3
  69. package/adapter-next/webpackPluginI18n.js +0 -11
  70. package/adapter-next-translate/code/DynamicNamespaces.js +0 -9
  71. package/adapter-next-translate/code/T.js +0 -31
  72. package/adapter-next-translate/code/TransText.js +0 -9
  73. package/adapter-next-translate/code/getT.js +0 -15
  74. package/adapter-next-translate/code/index.js +0 -49
  75. package/adapter-next-translate/code/nextTranslateI18n.js +0 -19
  76. package/adapter-next-translate/code/useT.js +0 -43
  77. package/cjs/adapter-js/code/config.cjs.d.ts +0 -3
  78. package/cjs/adapter-js/code/config.d.ts +0 -3
  79. package/cjs/adapter-js/code/defaultLocale.d.ts +0 -3
  80. package/cjs/adapter-js/code/deriveLocalisedPathnames.d.ts +0 -2
  81. package/cjs/adapter-js/code/index.d.ts +0 -3
  82. package/cjs/adapter-js/code/isLocale.d.ts +0 -2
  83. package/cjs/adapter-js/code/locales.d.ts +0 -3
  84. package/cjs/adapter-js/code/pathnameToRouteId.d.ts +0 -3
  85. package/cjs/adapter-js/code/routes.d.ts +0 -3
  86. package/cjs/adapter-js/code/routesSlim.d.ts +0 -3
  87. package/cjs/adapter-js/code/routesSpa.d.ts +0 -3
  88. package/cjs/adapter-js/code/tFns.d.ts +0 -3
  89. package/cjs/adapter-js/code/tInterpolateParams.d.ts +0 -3
  90. package/cjs/adapter-js/code/tPluralise.d.ts +0 -2
  91. package/cjs/adapter-js/code/to.d.ts +0 -3
  92. package/cjs/adapter-js/code/toFns.d.ts +0 -3
  93. package/cjs/adapter-js/code/toFormat.d.ts +0 -3
  94. package/cjs/adapter-js/code/toSpa.d.ts +0 -3
  95. package/cjs/adapter-js/code/types.d.ts +0 -3
  96. package/cjs/adapter-next/code/index.d.ts +0 -3
  97. package/cjs/adapter-next/code/next-redirects.d.ts +0 -3
  98. package/cjs/adapter-next/code/next-rewrites.d.ts +0 -3
  99. package/cjs/adapter-next/code/useCurrentLocalisedPathnames.d.ts +0 -2
  100. package/cjs/adapter-next/code/useLocale.d.ts +0 -3
  101. package/cjs/adapter-next/code/useRouteId.d.ts +0 -2
  102. package/cjs/adapter-next/code/useTo.d.ts +0 -2
  103. package/cjs/adapter-next/code/useToSpa.d.ts +0 -2
  104. package/cjs/adapter-next/plugin-async.d.ts +0 -9
  105. package/cjs/adapter-next/plugin-legacy.d.ts +0 -22
  106. package/cjs/adapter-next/plugin-shared.d.ts +0 -13
  107. package/cjs/adapter-next/plugin.d.ts +0 -7
  108. package/cjs/adapter-next/redirects.d.ts +0 -4
  109. package/cjs/adapter-next/rewrites.d.ts +0 -4
  110. package/cjs/adapter-next/transformPathname.d.ts +0 -1
  111. package/cjs/adapter-next/webpackPluginI18n.d.ts +0 -7
  112. package/cjs/adapter-next-translate/code/DynamicNamespaces.d.ts +0 -2
  113. package/cjs/adapter-next-translate/code/T.d.ts +0 -2
  114. package/cjs/adapter-next-translate/code/TransText.d.ts +0 -2
  115. package/cjs/adapter-next-translate/code/getT.d.ts +0 -2
  116. package/cjs/adapter-next-translate/code/index.d.ts +0 -3
  117. package/cjs/adapter-next-translate/code/nextTranslateI18n.d.ts +0 -3
  118. package/cjs/adapter-next-translate/code/useT.d.ts +0 -2
  119. package/cjs/client/formatRoutePathname.d.ts +0 -4
  120. package/cjs/client/index.d.ts +0 -3
  121. package/cjs/client/interpolateTo.d.ts +0 -4
  122. package/cjs/client/routeHasDynamicPortion.d.ts +0 -2
  123. package/cjs/compiler/api.d.ts +0 -30
  124. package/cjs/compiler/code/data-routes.d.ts +0 -19
  125. package/cjs/compiler/code/data-translations.d.ts +0 -13
  126. package/cjs/compiler/code/data.d.ts +0 -33
  127. package/cjs/compiler/code/generate.d.ts +0 -14
  128. package/cjs/compiler/code/index.d.ts +0 -3
  129. package/cjs/compiler/code/tsCompile.d.ts +0 -2
  130. package/cjs/compiler/code/write.d.ts +0 -11
  131. package/cjs/compiler/config.d.ts +0 -12
  132. package/cjs/compiler/helpers.d.ts +0 -2
  133. package/cjs/compiler/input/data-local.d.ts +0 -4
  134. package/cjs/compiler/input/data-remote.d.ts +0 -7
  135. package/cjs/compiler/input/data.d.ts +0 -4
  136. package/cjs/compiler/input/index.d.ts +0 -4
  137. package/cjs/compiler/input/types.d.ts +0 -14
  138. package/cjs/compiler/input/write.d.ts +0 -8
  139. package/cjs/compiler/pluralisation.d.ts +0 -37
  140. package/cjs/compiler/summary/data.d.ts +0 -6
  141. package/cjs/compiler/summary/generate.d.ts +0 -4
  142. package/cjs/compiler/summary/index.d.ts +0 -3
  143. package/cjs/compiler/summary/write.d.ts +0 -10
  144. package/cjs/compiler/types.d.ts +0 -82
  145. package/cjs/compiler-sync.d.ts +0 -4
  146. package/cjs/compiler-worker.d.ts +0 -1
  147. package/cjs/compiler.d.ts +0 -2
  148. package/cjs/i18n/actions/helpers-git.js +0 -45
  149. package/cjs/i18n/actions/helpers-i18n.js +0 -27
  150. package/cjs/i18n/actions/i18n.js +0 -17
  151. package/cjs/i18n/adapter-js/code/config.cjs.js +0 -14
  152. package/cjs/i18n/adapter-js/code/config.js +0 -14
  153. package/cjs/i18n/adapter-js/code/defaultLocale.js +0 -7
  154. package/cjs/i18n/adapter-js/code/deriveLocalisedPathnames.js +0 -78
  155. package/cjs/i18n/adapter-js/code/index.js +0 -123
  156. package/cjs/i18n/adapter-js/code/isLocale.js +0 -9
  157. package/cjs/i18n/adapter-js/code/locales.js +0 -8
  158. package/cjs/i18n/adapter-js/code/pathnameToRouteId.js +0 -20
  159. package/cjs/i18n/adapter-js/code/routes.js +0 -11
  160. package/cjs/i18n/adapter-js/code/routesSlim.js +0 -11
  161. package/cjs/i18n/adapter-js/code/routesSpa.js +0 -11
  162. package/cjs/i18n/adapter-js/code/tFns.js +0 -34
  163. package/cjs/i18n/adapter-js/code/tInterpolateParams.js +0 -18
  164. package/cjs/i18n/adapter-js/code/tPluralise.js +0 -10
  165. package/cjs/i18n/adapter-js/code/to.js +0 -32
  166. package/cjs/i18n/adapter-js/code/toFns.js +0 -25
  167. package/cjs/i18n/adapter-js/code/toFormat.js +0 -56
  168. package/cjs/i18n/adapter-js/code/toSpa.js +0 -42
  169. package/cjs/i18n/adapter-js/code/types.js +0 -327
  170. package/cjs/i18n/adapter-next/code/index.js +0 -54
  171. package/cjs/i18n/adapter-next/code/next-redirects.js +0 -5
  172. package/cjs/i18n/adapter-next/code/next-rewrites.js +0 -5
  173. package/cjs/i18n/adapter-next/code/useCurrentLocalisedPathnames.js +0 -20
  174. package/cjs/i18n/adapter-next/code/useLocale.js +0 -8
  175. package/cjs/i18n/adapter-next/code/useRouteId.js +0 -10
  176. package/cjs/i18n/adapter-next/code/useTo.js +0 -26
  177. package/cjs/i18n/adapter-next/code/useToSpa.js +0 -34
  178. package/cjs/i18n/adapter-next/plugin-async.js +0 -11
  179. package/cjs/i18n/adapter-next/plugin-legacy.js +0 -187
  180. package/cjs/i18n/adapter-next/plugin-shared.js +0 -39
  181. package/cjs/i18n/adapter-next/plugin.js +0 -13
  182. package/cjs/i18n/adapter-next/redirects.js +0 -51
  183. package/cjs/i18n/adapter-next/rewrites.js +0 -50
  184. package/cjs/i18n/adapter-next/transformPathname.js +0 -3
  185. package/cjs/i18n/adapter-next/webpackPluginI18n.js +0 -11
  186. package/cjs/i18n/adapter-next-translate/code/DynamicNamespaces.js +0 -9
  187. package/cjs/i18n/adapter-next-translate/code/T.js +0 -31
  188. package/cjs/i18n/adapter-next-translate/code/TransText.js +0 -9
  189. package/cjs/i18n/adapter-next-translate/code/getT.js +0 -15
  190. package/cjs/i18n/adapter-next-translate/code/index.js +0 -49
  191. package/cjs/i18n/adapter-next-translate/code/nextTranslateI18n.js +0 -19
  192. package/cjs/i18n/adapter-next-translate/code/useT.js +0 -43
  193. package/cjs/i18n/client/formatRoutePathname.js +0 -5
  194. package/cjs/i18n/client/index.js +0 -3
  195. package/cjs/i18n/client/interpolateTo.js +0 -14
  196. package/cjs/i18n/client/routeHasDynamicPortion.js +0 -2
  197. package/cjs/i18n/compiler/api.js +0 -18
  198. package/cjs/i18n/compiler/code/data-routes.js +0 -99
  199. package/cjs/i18n/compiler/code/data-translations.js +0 -86
  200. package/cjs/i18n/compiler/code/data.js +0 -20
  201. package/cjs/i18n/compiler/code/generate.js +0 -46
  202. package/cjs/i18n/compiler/code/index.js +0 -2
  203. package/cjs/i18n/compiler/code/tsCompile.js +0 -27
  204. package/cjs/i18n/compiler/code/write.js +0 -39
  205. package/cjs/i18n/compiler/config.js +0 -13
  206. package/cjs/i18n/compiler/helpers.js +0 -14
  207. package/cjs/i18n/compiler/input/data-local.js +0 -60
  208. package/cjs/i18n/compiler/input/data-remote.js +0 -41
  209. package/cjs/i18n/compiler/input/data.js +0 -11
  210. package/cjs/i18n/compiler/input/index.js +0 -3
  211. package/cjs/i18n/compiler/input/types.js +0 -1
  212. package/cjs/i18n/compiler/input/write.js +0 -15
  213. package/cjs/i18n/compiler/pluralisation.js +0 -59
  214. package/cjs/i18n/compiler/summary/data.js +0 -28
  215. package/cjs/i18n/compiler/summary/generate.js +0 -27
  216. package/cjs/i18n/compiler/summary/index.js +0 -2
  217. package/cjs/i18n/compiler/summary/write.js +0 -18
  218. package/cjs/i18n/compiler/types.js +0 -1
  219. package/cjs/i18n/compiler-sync.js +0 -2
  220. package/cjs/i18n/compiler-worker.js +0 -3
  221. package/cjs/i18n/compiler.js +0 -1
  222. package/cjs/i18n/index.js +0 -2
  223. package/cjs/i18n/next.js +0 -3
  224. package/cjs/i18n/types.js +0 -1
  225. package/cjs/index.d.ts +0 -2
  226. package/cjs/next.d.ts +0 -3
  227. package/cjs/package.json +0 -34
  228. package/cjs/types.d.ts +0 -48
  229. package/client/formatRoutePathname.js +0 -5
  230. package/client/index.js +0 -3
  231. package/client/interpolateTo.js +0 -14
  232. package/client/routeHasDynamicPortion.js +0 -2
  233. package/compiler/api.js +0 -18
  234. package/compiler/code/data-routes.js +0 -99
  235. package/compiler/code/data-translations.js +0 -86
  236. package/compiler/code/data.js +0 -20
  237. package/compiler/code/generate.js +0 -46
  238. package/compiler/code/index.js +0 -2
  239. package/compiler/code/tsCompile.js +0 -27
  240. package/compiler/code/write.js +0 -39
  241. package/compiler/config.js +0 -13
  242. package/compiler/helpers.js +0 -14
  243. package/compiler/input/data-local.js +0 -60
  244. package/compiler/input/data-remote.js +0 -41
  245. package/compiler/input/data.js +0 -11
  246. package/compiler/input/index.js +0 -3
  247. package/compiler/input/types.js +0 -1
  248. package/compiler/input/write.js +0 -15
  249. package/compiler/pluralisation.js +0 -59
  250. package/compiler/summary/data.js +0 -28
  251. package/compiler/summary/generate.js +0 -27
  252. package/compiler/summary/index.js +0 -2
  253. package/compiler/summary/write.js +0 -18
  254. package/compiler/types.js +0 -1
  255. package/compiler-sync.js +0 -2
  256. package/compiler-worker.js +0 -3
  257. package/compiler.js +0 -1
  258. package/index.js +0 -2
  259. package/next.js +0 -3
  260. package/types.js +0 -1
package/next.esm.js ADDED
@@ -0,0 +1,353 @@
1
+ import { arrayUniqueByProperties, normaliseUrlPathname } from '@koine/utils';
2
+ import { ContextReplacementPlugin } from 'webpack';
3
+ import { g as generateRedirects$1, a as generateRewrites$1, i as i18nCompiler } from './api.esm.js';
4
+ import { createRequire } from 'node:module';
5
+ import { i18nCompilerSync } from './compiler-sync.esm.js';
6
+ import 'node:fs';
7
+ import 'node:path';
8
+ import '@koine/node';
9
+ import './formatRoutePathname.esm.js';
10
+ import 'typescript';
11
+ import 'minimatch';
12
+ import 'next/dist/shared/lib/utils';
13
+ import 'node:fs/promises';
14
+ import 'glob';
15
+ import 'node:child_process';
16
+ import 'node:https';
17
+ import 'synckit';
18
+
19
+ function orderRoutes(routes, defaultLocale) {
20
+ const { [defaultLocale]: routesForDefaultLocale, ...restRoutes } = routes;
21
+ return {
22
+ [defaultLocale]: routesForDefaultLocale,
23
+ ...restRoutes,
24
+ };
25
+ }
26
+ function transformRoute(route) {
27
+ const { pathname: rawPathname, template: rawTemplate } = route;
28
+ const pathnameParts = rawPathname.split("/").filter((part) => !!part);
29
+ const templateParts = rawTemplate.split("/").filter((part) => !!part);
30
+ const mapPartsByIdx = {};
31
+ const pathname = pathnameParts
32
+ .map((part) => {
33
+ const hasWildcard = part.endsWith("*");
34
+ part = part.replace("*", "");
35
+ const isDynamic = part.startsWith("{{") && part.endsWith("}}");
36
+ const asValue = isDynamic
37
+ ? part.match(/{{(.+)}}/)?.[1].trim() ?? ""
38
+ : part.trim();
39
+ const asPath = encodeURIComponent(asValue) + (hasWildcard ? "*" : "");
40
+ mapPartsByIdx[asValue] = {
41
+ isDynamic,
42
+ hasWildcard,
43
+ };
44
+ return isDynamic ? `:${asPath}` : asPath;
45
+ })
46
+ .join("/");
47
+ const template = templateParts
48
+ .map((part) => {
49
+ const isDynamic = part.startsWith("[") && part.endsWith("]");
50
+ const asValue = isDynamic
51
+ ? part.match(/\[(.+)\]/)?.[1].trim() ?? ""
52
+ : part.trim();
53
+ const hasWildcard = mapPartsByIdx[asValue]?.hasWildcard;
54
+ const asPath = encodeURIComponent(asValue) + (hasWildcard ? "*" : "");
55
+ return isDynamic ? `:${asPath}` : asPath;
56
+ })
57
+ .join("/");
58
+ return { pathname, template };
59
+ }
60
+ function getRoutesMap(map = {}, routesByLocale, pathnameBuffer = "", templateBuffer = "") {
61
+ const isSpaRoute = Object.keys(routesByLocale).includes("[spa]");
62
+ for (const key in routesByLocale) {
63
+ const pathOrNestedRoutes = routesByLocale[key];
64
+ const template = `${templateBuffer}/${key}`;
65
+ if (typeof pathOrNestedRoutes === "string") {
66
+ if (!isSpaRoute || (isSpaRoute && ["[spa]", "index"].includes(key))) {
67
+ map[template] = {
68
+ template,
69
+ pathname: pathOrNestedRoutes,
70
+ wildcard: pathOrNestedRoutes.includes("*"),
71
+ };
72
+ }
73
+ }
74
+ else {
75
+ if (!isSpaRoute) {
76
+ getRoutesMap(map, pathOrNestedRoutes, pathnameBuffer, template);
77
+ }
78
+ }
79
+ }
80
+ return map;
81
+ }
82
+ function getWithoutIndex(template) {
83
+ return template.replace(/\/index$/, "");
84
+ }
85
+ function getPathRedirect(arg) {
86
+ const { localeSource, localeDestination, route, usePathnameAsSource, permanent, } = arg;
87
+ const { template: routeTemplate, pathname } = transformRoute(route);
88
+ const template = usePathnameAsSource ? pathname : routeTemplate;
89
+ const sourcePrefix = localeSource ? `${localeSource}/` : "";
90
+ const source = getWithoutIndex(`/${normaliseUrlPathname(sourcePrefix + template)}`);
91
+ const destinationPrefix = localeDestination ? `${localeDestination}/` : "";
92
+ const destination = `/${normaliseUrlPathname(destinationPrefix + pathname)}`;
93
+ if (source === destination)
94
+ return;
95
+ const redirect = {
96
+ source,
97
+ destination,
98
+ permanent: Boolean(permanent),
99
+ };
100
+ return redirect;
101
+ }
102
+ function generateRedirects(arg) {
103
+ const { routes, defaultLocale, hideDefaultLocaleInUrl, localeParam, permanent, debug, } = arg;
104
+ const orderedRoutes = orderRoutes(routes, defaultLocale);
105
+ const redirects = [];
106
+ for (const locale in orderedRoutes) {
107
+ const routesByLocale = routes[locale];
108
+ const routesMap = getRoutesMap({}, routesByLocale);
109
+ for (const template in routesMap) {
110
+ const route = routesMap[template];
111
+ if (route.pathname !== getWithoutIndex(template)) {
112
+ const isVisibleDefaultLocale = locale === defaultLocale && !hideDefaultLocaleInUrl;
113
+ const isHiddenDefaultLocale = locale === defaultLocale && hideDefaultLocaleInUrl;
114
+ if (localeParam) {
115
+ if (isVisibleDefaultLocale) {
116
+ redirects.push(getPathRedirect({ localeDestination: locale, route, permanent }));
117
+ }
118
+ else if (isHiddenDefaultLocale) {
119
+ redirects.push(getPathRedirect({ localeSource: locale, route, permanent }));
120
+ }
121
+ else if (locale !== defaultLocale) {
122
+ redirects.push(getPathRedirect({
123
+ localeSource: locale,
124
+ localeDestination: locale,
125
+ route,
126
+ permanent,
127
+ }));
128
+ }
129
+ else {
130
+ redirects.push(getPathRedirect({ route, permanent }));
131
+ }
132
+ }
133
+ else {
134
+ if (isVisibleDefaultLocale) {
135
+ redirects.push(getPathRedirect({ localeDestination: locale, route, permanent }));
136
+ }
137
+ else if (locale !== defaultLocale) {
138
+ redirects.push(getPathRedirect({
139
+ localeSource: locale,
140
+ localeDestination: locale,
141
+ route,
142
+ permanent,
143
+ }));
144
+ }
145
+ else {
146
+ redirects.push(getPathRedirect({ route, permanent }));
147
+ }
148
+ }
149
+ }
150
+ }
151
+ }
152
+ const cleaned = arrayUniqueByProperties(redirects.filter(Boolean), ["source", "destination"]).map((rewrite) => localeParam ? rewrite : { ...rewrite, locale: false });
153
+ if (debug)
154
+ console.info("[@koine/next/plugin-legacy:generateRedirects]", cleaned);
155
+ return cleaned;
156
+ }
157
+ function getPathRewrite(arg) {
158
+ const { localeSource, localeDestination, localeParam, route } = arg;
159
+ const { pathname, template } = transformRoute(route);
160
+ let sourcePrefix = "";
161
+ if (localeSource)
162
+ sourcePrefix = `${localeSource}/`;
163
+ else if (localeParam)
164
+ sourcePrefix = `:${localeParam}/`;
165
+ const source = `/${normaliseUrlPathname(sourcePrefix + pathname)}`;
166
+ let destinationPrefix = "";
167
+ if (localeDestination)
168
+ destinationPrefix = `${localeDestination}/`;
169
+ else if (localeParam)
170
+ destinationPrefix = `:${localeParam}/`;
171
+ const destination = getWithoutIndex(`/${normaliseUrlPathname(destinationPrefix + template)}`);
172
+ if (source === destination)
173
+ return;
174
+ return {
175
+ source,
176
+ destination,
177
+ };
178
+ }
179
+ function generateRewrites(arg) {
180
+ const { routes, defaultLocale, hideDefaultLocaleInUrl, localeParam, debug } = arg;
181
+ const orderedRoutes = orderRoutes(routes, defaultLocale);
182
+ const rewrites = [];
183
+ for (const locale in orderedRoutes) {
184
+ const routesByLocale = routes[locale];
185
+ const routesMap = getRoutesMap({}, routesByLocale);
186
+ for (const template in routesMap) {
187
+ const route = routesMap[template];
188
+ const isVisibleDefaultLocale = locale === defaultLocale && !hideDefaultLocaleInUrl;
189
+ const isHiddenDefaultLocale = locale === defaultLocale && hideDefaultLocaleInUrl;
190
+ if (localeParam) {
191
+ if (isHiddenDefaultLocale) {
192
+ rewrites.push(getPathRewrite({ localeDestination: locale, route }));
193
+ }
194
+ else {
195
+ rewrites.push(getPathRewrite({ localeParam, route }));
196
+ }
197
+ }
198
+ else {
199
+ if (route.pathname !== getWithoutIndex(template)) {
200
+ if (locale !== defaultLocale || isVisibleDefaultLocale) {
201
+ rewrites.push(getPathRewrite({ localeSource: locale, route }));
202
+ }
203
+ else {
204
+ rewrites.push(getPathRewrite({ route }));
205
+ }
206
+ }
207
+ }
208
+ }
209
+ }
210
+ const cleaned = arrayUniqueByProperties(rewrites.filter(Boolean), ["source", "destination"]);
211
+ if (debug)
212
+ console.info("[@koine/next/plugin-legacy:generateRewrites]", cleaned);
213
+ return cleaned;
214
+ }
215
+ let withI18nLegacy = (config) => {
216
+ const { i18nRoutes, ...restNextConfig } = config;
217
+ if (!i18nRoutes)
218
+ return restNextConfig;
219
+ const { locales = ["en"], defaultLocale = "en", ...restI18n } = config.i18n || {};
220
+ restNextConfig.i18n = { ...restI18n, locales, defaultLocale };
221
+ const options = {
222
+ hideDefaultLocaleInUrl: true,
223
+ localeParam: "",
224
+ ...i18nRoutes,
225
+ locales,
226
+ defaultLocale,
227
+ };
228
+ const nextConfig = { ...restNextConfig };
229
+ if (options.routes) {
230
+ const { redirects, rewrites } = nextConfig;
231
+ nextConfig.redirects = async () => {
232
+ const defaults = generateRedirects(options);
233
+ if (redirects) {
234
+ const customs = await redirects();
235
+ return [...defaults, ...customs];
236
+ }
237
+ return defaults;
238
+ };
239
+ nextConfig.rewrites = async () => {
240
+ const defaults = generateRewrites(options);
241
+ if (rewrites) {
242
+ const customs = await rewrites();
243
+ if (Array.isArray(customs)) {
244
+ return {
245
+ beforeFiles: defaults,
246
+ afterFiles: customs,
247
+ fallback: [],
248
+ };
249
+ }
250
+ return {
251
+ ...customs,
252
+ beforeFiles: [...defaults, ...(customs.beforeFiles || [])],
253
+ };
254
+ }
255
+ return {
256
+ beforeFiles: defaults,
257
+ afterFiles: [],
258
+ fallback: [],
259
+ };
260
+ };
261
+ }
262
+ return nextConfig;
263
+ };
264
+
265
+ let tweakNextConfig = (options, nextConfig) => {
266
+ const { defaultLocale, locales, appRouterLocaleParamName } = options;
267
+ if (appRouterLocaleParamName) {
268
+ delete nextConfig.i18n;
269
+ }
270
+ else {
271
+ nextConfig.i18n = nextConfig.i18n || { locales, defaultLocale };
272
+ nextConfig.i18n.locales = locales;
273
+ nextConfig.i18n.defaultLocale = defaultLocale;
274
+ }
275
+ nextConfig.webpack = (webpackConfig) => {
276
+ webpackConfig.plugins.push(new ContextReplacementPlugin(/^date-fns[/\\]locale$/, new RegExp(`\\.[/\\\\](${locales.join("|")})[/\\\\]index\\.js$`)));
277
+ return webpackConfig;
278
+ };
279
+ return nextConfig;
280
+ };
281
+ let getRedirects = async (prevRedirects, { appRouterLocaleParamName, permanentRedirects }, i18nResult) => {
282
+ const defaultRedirects = generateRedirects$1(i18nResult.config, i18nResult.code.routes.byId, i18nResult.code.options.routes, appRouterLocaleParamName, permanentRedirects);
283
+ if (prevRedirects) {
284
+ const custom = await prevRedirects();
285
+ return [...defaultRedirects, ...custom];
286
+ }
287
+ return defaultRedirects;
288
+ };
289
+ let getRewrites = async (prevRewrites, { appRouterLocaleParamName }, i18nResult) => {
290
+ const defaultRewrites = generateRewrites$1(i18nResult.config, i18nResult.code.routes.byId, i18nResult.code.options.routes, appRouterLocaleParamName);
291
+ if (prevRewrites) {
292
+ const custom = await prevRewrites();
293
+ if (Array.isArray(custom)) {
294
+ return {
295
+ beforeFiles: defaultRewrites,
296
+ afterFiles: custom,
297
+ fallback: [],
298
+ };
299
+ }
300
+ return {
301
+ ...custom,
302
+ beforeFiles: [...defaultRewrites, ...(custom.beforeFiles || [])],
303
+ };
304
+ }
305
+ return {
306
+ beforeFiles: defaultRewrites,
307
+ afterFiles: [],
308
+ fallback: [],
309
+ };
310
+ };
311
+
312
+ let withI18nAsync = (config = {}) => async () => {
313
+ const { i18nCompiler: i18nConfig, redirects, rewrites, ...restNextConfig } = config;
314
+ let nextConfig = restNextConfig;
315
+ if (!i18nConfig)
316
+ return nextConfig;
317
+ const i18nResult = await i18nCompiler(i18nConfig);
318
+ nextConfig = tweakNextConfig(i18nResult.config, nextConfig);
319
+ nextConfig.redirects = () => getRedirects(redirects, i18nConfig, i18nResult);
320
+ nextConfig.rewrites = () => getRewrites(rewrites, i18nConfig, i18nResult);
321
+ if (i18nConfig.code.adapter === "next-translate") {
322
+ try {
323
+ const withNextTranslate = await import('next-translate-plugin').then((m) => m.default);
324
+ nextConfig = withNextTranslate(nextConfig);
325
+ }
326
+ catch (e) {
327
+ }
328
+ }
329
+ return nextConfig;
330
+ };
331
+
332
+ const require = createRequire(import.meta.url);
333
+ let withI18n = (config = {}) => {
334
+ const { i18nCompiler: i18nConfig, redirects, rewrites, ...restNextConfig } = config;
335
+ let nextConfig = restNextConfig;
336
+ if (!i18nConfig)
337
+ return nextConfig;
338
+ const i18nResult = i18nCompilerSync(i18nConfig);
339
+ nextConfig = tweakNextConfig(i18nResult.config, nextConfig);
340
+ nextConfig.redirects = () => getRedirects(redirects, i18nConfig, i18nResult);
341
+ nextConfig.rewrites = () => getRewrites(rewrites, i18nConfig, i18nResult);
342
+ if (i18nConfig.code.adapter === "next-translate") {
343
+ try {
344
+ const withNextTranslate = require("next-translate-plugin");
345
+ nextConfig = withNextTranslate(nextConfig);
346
+ }
347
+ catch (e) {
348
+ }
349
+ }
350
+ return nextConfig;
351
+ };
352
+
353
+ export { withI18n, withI18nAsync, withI18nLegacy };