@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.
- package/README.md +1 -0
- package/api.cjs.js +2203 -0
- package/api.esm.js +2179 -0
- package/compiler-sync.cjs.d.ts +1 -0
- package/compiler-sync.cjs.default.js +1 -0
- package/compiler-sync.cjs.js +20 -0
- package/compiler-sync.cjs.mjs +2 -0
- package/compiler-sync.esm.js +16 -0
- package/compiler-worker.cjs.d.ts +1 -0
- package/compiler-worker.cjs.default.js +1 -0
- package/compiler-worker.cjs.js +18 -0
- package/compiler-worker.cjs.mjs +2 -0
- package/compiler-worker.esm.js +16 -0
- package/compiler.cjs.d.ts +1 -0
- package/compiler.cjs.default.js +1 -0
- package/compiler.cjs.js +21 -0
- package/compiler.cjs.mjs +2 -0
- package/compiler.esm.js +13 -0
- package/formatRoutePathname.cjs.js +12 -0
- package/formatRoutePathname.esm.js +10 -0
- package/index.cjs.d.ts +1 -0
- package/index.cjs.default.js +1 -0
- package/index.cjs.js +47 -0
- package/index.cjs.mjs +2 -0
- package/index.esm.js +42 -0
- package/next.cjs.d.ts +1 -0
- package/next.cjs.default.js +1 -0
- package/next.cjs.js +377 -0
- package/next.cjs.mjs +2 -0
- package/next.esm.js +353 -0
- package/package.json +22 -305
- package/actions/helpers-git.js +0 -45
- package/actions/helpers-i18n.js +0 -27
- package/actions/i18n.js +0 -17
- package/adapter-js/code/config.cjs.js +0 -14
- package/adapter-js/code/config.js +0 -14
- package/adapter-js/code/defaultLocale.js +0 -7
- package/adapter-js/code/deriveLocalisedPathnames.js +0 -78
- package/adapter-js/code/index.js +0 -123
- package/adapter-js/code/isLocale.js +0 -9
- package/adapter-js/code/locales.js +0 -8
- package/adapter-js/code/pathnameToRouteId.js +0 -20
- package/adapter-js/code/routes.js +0 -11
- package/adapter-js/code/routesSlim.js +0 -11
- package/adapter-js/code/routesSpa.js +0 -11
- package/adapter-js/code/tFns.js +0 -34
- package/adapter-js/code/tInterpolateParams.js +0 -18
- package/adapter-js/code/tPluralise.js +0 -10
- package/adapter-js/code/to.js +0 -32
- package/adapter-js/code/toFns.js +0 -25
- package/adapter-js/code/toFormat.js +0 -56
- package/adapter-js/code/toSpa.js +0 -42
- package/adapter-js/code/types.js +0 -327
- package/adapter-next/code/index.js +0 -54
- package/adapter-next/code/next-redirects.js +0 -5
- package/adapter-next/code/next-rewrites.js +0 -5
- package/adapter-next/code/useCurrentLocalisedPathnames.js +0 -20
- package/adapter-next/code/useLocale.js +0 -8
- package/adapter-next/code/useRouteId.js +0 -10
- package/adapter-next/code/useTo.js +0 -26
- package/adapter-next/code/useToSpa.js +0 -34
- package/adapter-next/plugin-async.js +0 -11
- package/adapter-next/plugin-legacy.js +0 -187
- package/adapter-next/plugin-shared.js +0 -39
- package/adapter-next/plugin.js +0 -13
- package/adapter-next/redirects.js +0 -51
- package/adapter-next/rewrites.js +0 -50
- package/adapter-next/transformPathname.js +0 -3
- package/adapter-next/webpackPluginI18n.js +0 -11
- package/adapter-next-translate/code/DynamicNamespaces.js +0 -9
- package/adapter-next-translate/code/T.js +0 -31
- package/adapter-next-translate/code/TransText.js +0 -9
- package/adapter-next-translate/code/getT.js +0 -15
- package/adapter-next-translate/code/index.js +0 -49
- package/adapter-next-translate/code/nextTranslateI18n.js +0 -19
- package/adapter-next-translate/code/useT.js +0 -43
- package/cjs/adapter-js/code/config.cjs.d.ts +0 -3
- package/cjs/adapter-js/code/config.d.ts +0 -3
- package/cjs/adapter-js/code/defaultLocale.d.ts +0 -3
- package/cjs/adapter-js/code/deriveLocalisedPathnames.d.ts +0 -2
- package/cjs/adapter-js/code/index.d.ts +0 -3
- package/cjs/adapter-js/code/isLocale.d.ts +0 -2
- package/cjs/adapter-js/code/locales.d.ts +0 -3
- package/cjs/adapter-js/code/pathnameToRouteId.d.ts +0 -3
- package/cjs/adapter-js/code/routes.d.ts +0 -3
- package/cjs/adapter-js/code/routesSlim.d.ts +0 -3
- package/cjs/adapter-js/code/routesSpa.d.ts +0 -3
- package/cjs/adapter-js/code/tFns.d.ts +0 -3
- package/cjs/adapter-js/code/tInterpolateParams.d.ts +0 -3
- package/cjs/adapter-js/code/tPluralise.d.ts +0 -2
- package/cjs/adapter-js/code/to.d.ts +0 -3
- package/cjs/adapter-js/code/toFns.d.ts +0 -3
- package/cjs/adapter-js/code/toFormat.d.ts +0 -3
- package/cjs/adapter-js/code/toSpa.d.ts +0 -3
- package/cjs/adapter-js/code/types.d.ts +0 -3
- package/cjs/adapter-next/code/index.d.ts +0 -3
- package/cjs/adapter-next/code/next-redirects.d.ts +0 -3
- package/cjs/adapter-next/code/next-rewrites.d.ts +0 -3
- package/cjs/adapter-next/code/useCurrentLocalisedPathnames.d.ts +0 -2
- package/cjs/adapter-next/code/useLocale.d.ts +0 -3
- package/cjs/adapter-next/code/useRouteId.d.ts +0 -2
- package/cjs/adapter-next/code/useTo.d.ts +0 -2
- package/cjs/adapter-next/code/useToSpa.d.ts +0 -2
- package/cjs/adapter-next/plugin-async.d.ts +0 -9
- package/cjs/adapter-next/plugin-legacy.d.ts +0 -22
- package/cjs/adapter-next/plugin-shared.d.ts +0 -13
- package/cjs/adapter-next/plugin.d.ts +0 -7
- package/cjs/adapter-next/redirects.d.ts +0 -4
- package/cjs/adapter-next/rewrites.d.ts +0 -4
- package/cjs/adapter-next/transformPathname.d.ts +0 -1
- package/cjs/adapter-next/webpackPluginI18n.d.ts +0 -7
- package/cjs/adapter-next-translate/code/DynamicNamespaces.d.ts +0 -2
- package/cjs/adapter-next-translate/code/T.d.ts +0 -2
- package/cjs/adapter-next-translate/code/TransText.d.ts +0 -2
- package/cjs/adapter-next-translate/code/getT.d.ts +0 -2
- package/cjs/adapter-next-translate/code/index.d.ts +0 -3
- package/cjs/adapter-next-translate/code/nextTranslateI18n.d.ts +0 -3
- package/cjs/adapter-next-translate/code/useT.d.ts +0 -2
- package/cjs/client/formatRoutePathname.d.ts +0 -4
- package/cjs/client/index.d.ts +0 -3
- package/cjs/client/interpolateTo.d.ts +0 -4
- package/cjs/client/routeHasDynamicPortion.d.ts +0 -2
- package/cjs/compiler/api.d.ts +0 -30
- package/cjs/compiler/code/data-routes.d.ts +0 -19
- package/cjs/compiler/code/data-translations.d.ts +0 -13
- package/cjs/compiler/code/data.d.ts +0 -33
- package/cjs/compiler/code/generate.d.ts +0 -14
- package/cjs/compiler/code/index.d.ts +0 -3
- package/cjs/compiler/code/tsCompile.d.ts +0 -2
- package/cjs/compiler/code/write.d.ts +0 -11
- package/cjs/compiler/config.d.ts +0 -12
- package/cjs/compiler/helpers.d.ts +0 -2
- package/cjs/compiler/input/data-local.d.ts +0 -4
- package/cjs/compiler/input/data-remote.d.ts +0 -7
- package/cjs/compiler/input/data.d.ts +0 -4
- package/cjs/compiler/input/index.d.ts +0 -4
- package/cjs/compiler/input/types.d.ts +0 -14
- package/cjs/compiler/input/write.d.ts +0 -8
- package/cjs/compiler/pluralisation.d.ts +0 -37
- package/cjs/compiler/summary/data.d.ts +0 -6
- package/cjs/compiler/summary/generate.d.ts +0 -4
- package/cjs/compiler/summary/index.d.ts +0 -3
- package/cjs/compiler/summary/write.d.ts +0 -10
- package/cjs/compiler/types.d.ts +0 -82
- package/cjs/compiler-sync.d.ts +0 -4
- package/cjs/compiler-worker.d.ts +0 -1
- package/cjs/compiler.d.ts +0 -2
- package/cjs/i18n/actions/helpers-git.js +0 -45
- package/cjs/i18n/actions/helpers-i18n.js +0 -27
- package/cjs/i18n/actions/i18n.js +0 -17
- package/cjs/i18n/adapter-js/code/config.cjs.js +0 -14
- package/cjs/i18n/adapter-js/code/config.js +0 -14
- package/cjs/i18n/adapter-js/code/defaultLocale.js +0 -7
- package/cjs/i18n/adapter-js/code/deriveLocalisedPathnames.js +0 -78
- package/cjs/i18n/adapter-js/code/index.js +0 -123
- package/cjs/i18n/adapter-js/code/isLocale.js +0 -9
- package/cjs/i18n/adapter-js/code/locales.js +0 -8
- package/cjs/i18n/adapter-js/code/pathnameToRouteId.js +0 -20
- package/cjs/i18n/adapter-js/code/routes.js +0 -11
- package/cjs/i18n/adapter-js/code/routesSlim.js +0 -11
- package/cjs/i18n/adapter-js/code/routesSpa.js +0 -11
- package/cjs/i18n/adapter-js/code/tFns.js +0 -34
- package/cjs/i18n/adapter-js/code/tInterpolateParams.js +0 -18
- package/cjs/i18n/adapter-js/code/tPluralise.js +0 -10
- package/cjs/i18n/adapter-js/code/to.js +0 -32
- package/cjs/i18n/adapter-js/code/toFns.js +0 -25
- package/cjs/i18n/adapter-js/code/toFormat.js +0 -56
- package/cjs/i18n/adapter-js/code/toSpa.js +0 -42
- package/cjs/i18n/adapter-js/code/types.js +0 -327
- package/cjs/i18n/adapter-next/code/index.js +0 -54
- package/cjs/i18n/adapter-next/code/next-redirects.js +0 -5
- package/cjs/i18n/adapter-next/code/next-rewrites.js +0 -5
- package/cjs/i18n/adapter-next/code/useCurrentLocalisedPathnames.js +0 -20
- package/cjs/i18n/adapter-next/code/useLocale.js +0 -8
- package/cjs/i18n/adapter-next/code/useRouteId.js +0 -10
- package/cjs/i18n/adapter-next/code/useTo.js +0 -26
- package/cjs/i18n/adapter-next/code/useToSpa.js +0 -34
- package/cjs/i18n/adapter-next/plugin-async.js +0 -11
- package/cjs/i18n/adapter-next/plugin-legacy.js +0 -187
- package/cjs/i18n/adapter-next/plugin-shared.js +0 -39
- package/cjs/i18n/adapter-next/plugin.js +0 -13
- package/cjs/i18n/adapter-next/redirects.js +0 -51
- package/cjs/i18n/adapter-next/rewrites.js +0 -50
- package/cjs/i18n/adapter-next/transformPathname.js +0 -3
- package/cjs/i18n/adapter-next/webpackPluginI18n.js +0 -11
- package/cjs/i18n/adapter-next-translate/code/DynamicNamespaces.js +0 -9
- package/cjs/i18n/adapter-next-translate/code/T.js +0 -31
- package/cjs/i18n/adapter-next-translate/code/TransText.js +0 -9
- package/cjs/i18n/adapter-next-translate/code/getT.js +0 -15
- package/cjs/i18n/adapter-next-translate/code/index.js +0 -49
- package/cjs/i18n/adapter-next-translate/code/nextTranslateI18n.js +0 -19
- package/cjs/i18n/adapter-next-translate/code/useT.js +0 -43
- package/cjs/i18n/client/formatRoutePathname.js +0 -5
- package/cjs/i18n/client/index.js +0 -3
- package/cjs/i18n/client/interpolateTo.js +0 -14
- package/cjs/i18n/client/routeHasDynamicPortion.js +0 -2
- package/cjs/i18n/compiler/api.js +0 -18
- package/cjs/i18n/compiler/code/data-routes.js +0 -99
- package/cjs/i18n/compiler/code/data-translations.js +0 -86
- package/cjs/i18n/compiler/code/data.js +0 -20
- package/cjs/i18n/compiler/code/generate.js +0 -46
- package/cjs/i18n/compiler/code/index.js +0 -2
- package/cjs/i18n/compiler/code/tsCompile.js +0 -27
- package/cjs/i18n/compiler/code/write.js +0 -39
- package/cjs/i18n/compiler/config.js +0 -13
- package/cjs/i18n/compiler/helpers.js +0 -14
- package/cjs/i18n/compiler/input/data-local.js +0 -60
- package/cjs/i18n/compiler/input/data-remote.js +0 -41
- package/cjs/i18n/compiler/input/data.js +0 -11
- package/cjs/i18n/compiler/input/index.js +0 -3
- package/cjs/i18n/compiler/input/types.js +0 -1
- package/cjs/i18n/compiler/input/write.js +0 -15
- package/cjs/i18n/compiler/pluralisation.js +0 -59
- package/cjs/i18n/compiler/summary/data.js +0 -28
- package/cjs/i18n/compiler/summary/generate.js +0 -27
- package/cjs/i18n/compiler/summary/index.js +0 -2
- package/cjs/i18n/compiler/summary/write.js +0 -18
- package/cjs/i18n/compiler/types.js +0 -1
- package/cjs/i18n/compiler-sync.js +0 -2
- package/cjs/i18n/compiler-worker.js +0 -3
- package/cjs/i18n/compiler.js +0 -1
- package/cjs/i18n/index.js +0 -2
- package/cjs/i18n/next.js +0 -3
- package/cjs/i18n/types.js +0 -1
- package/cjs/index.d.ts +0 -2
- package/cjs/next.d.ts +0 -3
- package/cjs/package.json +0 -34
- package/cjs/types.d.ts +0 -48
- package/client/formatRoutePathname.js +0 -5
- package/client/index.js +0 -3
- package/client/interpolateTo.js +0 -14
- package/client/routeHasDynamicPortion.js +0 -2
- package/compiler/api.js +0 -18
- package/compiler/code/data-routes.js +0 -99
- package/compiler/code/data-translations.js +0 -86
- package/compiler/code/data.js +0 -20
- package/compiler/code/generate.js +0 -46
- package/compiler/code/index.js +0 -2
- package/compiler/code/tsCompile.js +0 -27
- package/compiler/code/write.js +0 -39
- package/compiler/config.js +0 -13
- package/compiler/helpers.js +0 -14
- package/compiler/input/data-local.js +0 -60
- package/compiler/input/data-remote.js +0 -41
- package/compiler/input/data.js +0 -11
- package/compiler/input/index.js +0 -3
- package/compiler/input/types.js +0 -1
- package/compiler/input/write.js +0 -15
- package/compiler/pluralisation.js +0 -59
- package/compiler/summary/data.js +0 -28
- package/compiler/summary/generate.js +0 -27
- package/compiler/summary/index.js +0 -2
- package/compiler/summary/write.js +0 -18
- package/compiler/types.js +0 -1
- package/compiler-sync.js +0 -2
- package/compiler-worker.js +0 -3
- package/compiler.js +0 -1
- package/index.js +0 -2
- package/next.js +0 -3
- 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 };
|