@bleedingdev/modern-js-plugin-i18n 3.2.0-ultramodern.12 → 3.2.0-ultramodern.120
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 +221 -11
- package/dist/cjs/cli/index.js +32 -5
- package/dist/cjs/runtime/I18nLink.js +17 -28
- package/dist/cjs/runtime/Link.js +252 -0
- package/dist/cjs/runtime/canonicalRoutes.js +18 -0
- package/dist/cjs/runtime/context.js +41 -10
- package/dist/cjs/runtime/hooks.js +17 -10
- package/dist/cjs/runtime/i18n/backend/config.js +9 -5
- package/dist/cjs/runtime/i18n/backend/defaults.js +15 -10
- package/dist/cjs/runtime/i18n/backend/defaults.node.js +16 -11
- package/dist/cjs/runtime/i18n/backend/index.js +9 -5
- package/dist/cjs/runtime/i18n/backend/middleware.common.js +9 -5
- package/dist/cjs/runtime/i18n/backend/middleware.js +9 -5
- package/dist/cjs/runtime/i18n/backend/middleware.node.js +13 -9
- package/dist/cjs/runtime/i18n/backend/sdk-backend.js +9 -5
- package/dist/cjs/runtime/i18n/backend/sdk-event.js +16 -11
- package/dist/cjs/runtime/i18n/detection/config.js +9 -5
- package/dist/cjs/runtime/i18n/detection/index.js +9 -5
- package/dist/cjs/runtime/i18n/detection/middleware.js +9 -5
- package/dist/cjs/runtime/i18n/detection/middleware.node.js +9 -5
- package/dist/cjs/runtime/i18n/index.js +9 -5
- package/dist/cjs/runtime/i18n/instance.js +17 -37
- package/dist/cjs/runtime/i18n/react-i18next.js +53 -0
- package/dist/cjs/runtime/i18n/utils.js +9 -17
- package/dist/cjs/runtime/index.js +50 -15
- package/dist/cjs/runtime/localizedPaths.js +105 -0
- package/dist/cjs/runtime/routerAdapter.js +167 -0
- package/dist/cjs/runtime/utils.js +87 -97
- package/dist/cjs/server/index.js +69 -13
- package/dist/cjs/shared/deepMerge.js +12 -8
- package/dist/cjs/shared/detection.js +9 -5
- package/dist/cjs/shared/localisedUrls.js +271 -0
- package/dist/cjs/shared/utils.js +15 -11
- package/dist/esm/cli/index.mjs +23 -0
- package/dist/esm/runtime/I18nLink.mjs +7 -22
- package/dist/esm/runtime/Link.mjs +209 -0
- package/dist/esm/runtime/canonicalRoutes.mjs +0 -0
- package/dist/esm/runtime/context.mjs +34 -7
- package/dist/esm/runtime/hooks.mjs +9 -6
- package/dist/esm/runtime/i18n/backend/defaults.mjs +1 -1
- package/dist/esm/runtime/i18n/backend/defaults.node.mjs +2 -2
- package/dist/esm/runtime/i18n/backend/middleware.node.mjs +3 -3
- package/dist/esm/runtime/i18n/instance.mjs +1 -19
- package/dist/esm/runtime/i18n/react-i18next.mjs +15 -0
- package/dist/esm/runtime/i18n/utils.mjs +0 -12
- package/dist/esm/runtime/index.mjs +23 -13
- package/dist/esm/runtime/localizedPaths.mjs +58 -0
- package/dist/esm/runtime/routerAdapter.mjs +129 -0
- package/dist/esm/runtime/utils.mjs +25 -30
- package/dist/esm/server/index.mjs +53 -7
- package/dist/esm/shared/localisedUrls.mjs +212 -0
- package/dist/esm-node/cli/index.mjs +23 -0
- package/dist/esm-node/runtime/I18nLink.mjs +7 -22
- package/dist/esm-node/runtime/Link.mjs +210 -0
- package/dist/esm-node/runtime/canonicalRoutes.mjs +1 -0
- package/dist/esm-node/runtime/context.mjs +34 -7
- package/dist/esm-node/runtime/hooks.mjs +9 -6
- package/dist/esm-node/runtime/i18n/backend/defaults.mjs +1 -1
- package/dist/esm-node/runtime/i18n/backend/defaults.node.mjs +2 -2
- package/dist/esm-node/runtime/i18n/backend/middleware.node.mjs +3 -3
- package/dist/esm-node/runtime/i18n/instance.mjs +1 -19
- package/dist/esm-node/runtime/i18n/react-i18next.mjs +16 -0
- package/dist/esm-node/runtime/i18n/utils.mjs +0 -12
- package/dist/esm-node/runtime/index.mjs +23 -13
- package/dist/esm-node/runtime/localizedPaths.mjs +59 -0
- package/dist/esm-node/runtime/routerAdapter.mjs +130 -0
- package/dist/esm-node/runtime/utils.mjs +25 -30
- package/dist/esm-node/server/index.mjs +53 -7
- package/dist/esm-node/shared/localisedUrls.mjs +213 -0
- package/dist/types/cli/index.d.ts +1 -0
- package/dist/types/runtime/I18nLink.d.ts +6 -0
- package/dist/types/runtime/Link.d.ts +56 -0
- package/dist/types/runtime/canonicalRoutes.d.ts +60 -0
- package/dist/types/runtime/context.d.ts +3 -0
- package/dist/types/runtime/hooks.d.ts +4 -2
- package/dist/types/runtime/i18n/backend/middleware.node.d.ts +1 -1
- package/dist/types/runtime/i18n/instance.d.ts +4 -6
- package/dist/types/runtime/i18n/react-i18next.d.ts +7 -0
- package/dist/types/runtime/index.d.ts +6 -1
- package/dist/types/runtime/localizedPaths.d.ts +39 -0
- package/dist/types/runtime/routerAdapter.d.ts +26 -0
- package/dist/types/runtime/types.d.ts +1 -1
- package/dist/types/runtime/utils.d.ts +13 -9
- package/dist/types/server/index.d.ts +6 -0
- package/dist/types/shared/localisedUrls.d.ts +21 -0
- package/dist/types/shared/type.d.ts +12 -0
- package/package.json +24 -28
- package/rstest.config.mts +39 -0
- package/src/cli/index.ts +44 -1
- package/src/runtime/I18nLink.tsx +14 -51
- package/src/runtime/Link.tsx +414 -0
- package/src/runtime/canonicalRoutes.ts +93 -0
- package/src/runtime/context.tsx +45 -7
- package/src/runtime/hooks.ts +13 -4
- package/src/runtime/i18n/backend/defaults.node.ts +2 -2
- package/src/runtime/i18n/backend/defaults.ts +3 -1
- package/src/runtime/i18n/backend/middleware.node.ts +1 -1
- package/src/runtime/i18n/instance.ts +3 -30
- package/src/runtime/i18n/react-i18next.ts +25 -0
- package/src/runtime/i18n/utils.ts +4 -26
- package/src/runtime/index.tsx +47 -12
- package/src/runtime/localizedPaths.ts +118 -0
- package/src/runtime/routerAdapter.tsx +333 -0
- package/src/runtime/types.ts +1 -1
- package/src/runtime/utils.ts +44 -37
- package/src/server/index.ts +117 -10
- package/src/shared/localisedUrls.ts +453 -0
- package/src/shared/type.ts +12 -0
- package/tests/i18nUtils.test.ts +52 -0
- package/tests/link.test.tsx +475 -0
- package/tests/linkTypes.test.ts +28 -0
- package/tests/localisedUrls.test.ts +312 -0
- package/tests/routerAdapter.test.tsx +452 -0
- package/tests/type-fixture/linkTypes.fixture.tsx +51 -0
- package/tests/type-fixture/tsconfig.json +15 -0
- package/dist/esm/rslib-runtime.mjs +0 -18
- package/dist/esm-node/rslib-runtime.mjs +0 -19
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __webpack_require__ = {};
|
|
3
|
+
(()=>{
|
|
4
|
+
__webpack_require__.d = (exports1, getters, values)=>{
|
|
5
|
+
var define = (defs, kind)=>{
|
|
6
|
+
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
[kind]: defs[key]
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
define(getters, "get");
|
|
12
|
+
define(values, "value");
|
|
13
|
+
};
|
|
14
|
+
})();
|
|
15
|
+
(()=>{
|
|
16
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
17
|
+
})();
|
|
18
|
+
(()=>{
|
|
19
|
+
__webpack_require__.r = (exports1)=>{
|
|
20
|
+
if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
21
|
+
value: 'Module'
|
|
22
|
+
});
|
|
23
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
24
|
+
value: true
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
})();
|
|
28
|
+
var __webpack_exports__ = {};
|
|
29
|
+
__webpack_require__.r(__webpack_exports__);
|
|
30
|
+
const LOCALE_PARAM_NAMES = new Set([
|
|
31
|
+
'lang',
|
|
32
|
+
'locale',
|
|
33
|
+
'language'
|
|
34
|
+
]);
|
|
35
|
+
const normalisePathPattern = (path)=>{
|
|
36
|
+
const withoutDuplicateSlashes = path.replace(/\/+/g, '/');
|
|
37
|
+
const withLeadingSlash = withoutDuplicateSlashes.startsWith('/') ? withoutDuplicateSlashes : `/${withoutDuplicateSlashes}`;
|
|
38
|
+
const withoutTrailingSlash = withLeadingSlash.length > 1 ? withLeadingSlash.replace(/\/+$/, '') : withLeadingSlash;
|
|
39
|
+
return withoutTrailingSlash.replace(/\[(.+?)\]/g, ':$1');
|
|
40
|
+
};
|
|
41
|
+
const normaliseRoutePath = (path)=>{
|
|
42
|
+
const normalized = normalisePathPattern(path);
|
|
43
|
+
return '/' === normalized ? '' : normalized.slice(1);
|
|
44
|
+
};
|
|
45
|
+
const getLocaleParamSegment = (segment)=>{
|
|
46
|
+
if (!segment.startsWith(':')) return null;
|
|
47
|
+
const paramName = segment.slice(1).replace(/\?$/, '');
|
|
48
|
+
return LOCALE_PARAM_NAMES.has(paramName) ? segment : null;
|
|
49
|
+
};
|
|
50
|
+
const splitPathSegments = (path)=>{
|
|
51
|
+
if (!path) return [];
|
|
52
|
+
return normalisePathPattern(path).split('/').filter(Boolean);
|
|
53
|
+
};
|
|
54
|
+
const stripLeadingLocaleParam = (path)=>{
|
|
55
|
+
const segments = splitPathSegments(path);
|
|
56
|
+
const leadingLocaleParam = getLocaleParamSegment(segments[0] || '');
|
|
57
|
+
if (!leadingLocaleParam) return path;
|
|
58
|
+
const remainingPath = segments.slice(1).join('/');
|
|
59
|
+
return remainingPath ? `/${remainingPath}` : void 0;
|
|
60
|
+
};
|
|
61
|
+
const getLeadingLocaleParam = (path)=>{
|
|
62
|
+
const segments = splitPathSegments(path);
|
|
63
|
+
return getLocaleParamSegment(segments[0] || '');
|
|
64
|
+
};
|
|
65
|
+
const resolveLocalisedUrlsConfig = (option)=>{
|
|
66
|
+
if (false === option) return {
|
|
67
|
+
enabled: false,
|
|
68
|
+
map: {}
|
|
69
|
+
};
|
|
70
|
+
if (option && 'object' == typeof option) return {
|
|
71
|
+
enabled: true,
|
|
72
|
+
map: option
|
|
73
|
+
};
|
|
74
|
+
return {
|
|
75
|
+
enabled: true,
|
|
76
|
+
map: {}
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
const isLocalisableRoutePath = (path)=>{
|
|
80
|
+
const pathWithoutLocale = stripLeadingLocaleParam(path);
|
|
81
|
+
if (!pathWithoutLocale || '/' === pathWithoutLocale || '*' === pathWithoutLocale) return false;
|
|
82
|
+
return true;
|
|
83
|
+
};
|
|
84
|
+
const joinPath = (parentPath, routePath)=>{
|
|
85
|
+
if (!isLocalisableRoutePath(routePath)) return parentPath;
|
|
86
|
+
const segment = normaliseRoutePath(stripLeadingLocaleParam(routePath) || '');
|
|
87
|
+
return normalisePathPattern(`${parentPath}/${segment}`);
|
|
88
|
+
};
|
|
89
|
+
const ensureLocalisedUrlsForPath = (canonicalPath, languages, localisedUrls)=>{
|
|
90
|
+
const entry = localisedUrls[canonicalPath];
|
|
91
|
+
if (!entry) throw new Error(`localisedUrls is enabled, but route "${canonicalPath}" does not define localised URLs for languages: ${languages.join(', ')}. Add localisedUrls["${canonicalPath}"] or set localeDetection.localisedUrls to false.`);
|
|
92
|
+
const missingLanguages = languages.filter((language)=>!entry[language]);
|
|
93
|
+
if (missingLanguages.length > 0) throw new Error(`localisedUrls["${canonicalPath}"] is missing languages: ${missingLanguages.join(', ')}. Every configured language must have a localised URL.`);
|
|
94
|
+
return entry;
|
|
95
|
+
};
|
|
96
|
+
const validateLocalisedUrls = (routes, languages, localisedUrls)=>{
|
|
97
|
+
const visit = (route, parentPath)=>{
|
|
98
|
+
const canonicalPath = joinPath(parentPath, route.path);
|
|
99
|
+
if (isLocalisableRoutePath(route.path)) ensureLocalisedUrlsForPath(canonicalPath, languages, localisedUrls);
|
|
100
|
+
if ('children' in route && route.children) route.children.forEach((child)=>visit(child, canonicalPath));
|
|
101
|
+
};
|
|
102
|
+
routes.forEach((route)=>visit(route, ''));
|
|
103
|
+
};
|
|
104
|
+
const getLocalisedRoutePaths = (canonicalPath, parentLocalisedPaths, languages, entry)=>{
|
|
105
|
+
const paths = languages.map((language)=>{
|
|
106
|
+
const fullPath = normalisePathPattern(entry[language]);
|
|
107
|
+
const parentPath = normalisePathPattern(parentLocalisedPaths[language] || '/');
|
|
108
|
+
if ('/' === parentPath) return normaliseRoutePath(fullPath) || void 0;
|
|
109
|
+
const parentPrefix = `${parentPath}/`;
|
|
110
|
+
if (!fullPath.startsWith(parentPrefix)) throw new Error(`localisedUrls["${canonicalPath}"].${language} must be nested under "${parentPath}" because its parent route is localised there.`);
|
|
111
|
+
return normaliseRoutePath(fullPath.slice(parentPath.length));
|
|
112
|
+
});
|
|
113
|
+
return Array.from(new Set(paths.filter(Boolean)));
|
|
114
|
+
};
|
|
115
|
+
const transformLocalisedRoute = (route, parentCanonicalPath, parentLocalisedPaths, languages, localisedUrls)=>{
|
|
116
|
+
const canonicalPath = joinPath(parentCanonicalPath, route.path);
|
|
117
|
+
const localisedUrlEntry = isLocalisableRoutePath(route.path) ? ensureLocalisedUrlsForPath(canonicalPath, languages, localisedUrls) : void 0;
|
|
118
|
+
const routeLocalisedPaths = localisedUrlEntry ? languages.reduce((acc, language)=>{
|
|
119
|
+
acc[language] = normalisePathPattern(localisedUrlEntry[language]);
|
|
120
|
+
return acc;
|
|
121
|
+
}, {}) : parentLocalisedPaths;
|
|
122
|
+
const children = 'children' in route && route.children ? route.children.flatMap((child)=>transformLocalisedRoute(child, canonicalPath, routeLocalisedPaths, languages, localisedUrls)) : void 0;
|
|
123
|
+
const baseRoute = {
|
|
124
|
+
...route,
|
|
125
|
+
...children ? {
|
|
126
|
+
children
|
|
127
|
+
} : {}
|
|
128
|
+
};
|
|
129
|
+
if (!localisedUrlEntry) return [
|
|
130
|
+
baseRoute
|
|
131
|
+
];
|
|
132
|
+
return getLocalisedRoutePaths(canonicalPath, parentLocalisedPaths, languages, localisedUrlEntry).map((localisedPath, index)=>cloneRouteWithLocalisedPath(baseRoute, localisedPath, index, canonicalPath));
|
|
133
|
+
};
|
|
134
|
+
const legalRouteIdPart = (value)=>value.replace(/[^a-zA-Z0-9_$-]+/g, '_').replace(/^_+|_+$/g, '') || 'index';
|
|
135
|
+
const suffixRouteIds = (route, suffix)=>{
|
|
136
|
+
const children = 'children' in route && route.children ? route.children.map((child)=>suffixRouteIds(child, suffix)) : void 0;
|
|
137
|
+
return {
|
|
138
|
+
...route,
|
|
139
|
+
...route.id ? {
|
|
140
|
+
id: `${route.id}__localised_${suffix}`
|
|
141
|
+
} : {},
|
|
142
|
+
...children ? {
|
|
143
|
+
children
|
|
144
|
+
} : {}
|
|
145
|
+
};
|
|
146
|
+
};
|
|
147
|
+
const cloneRouteWithLocalisedPath = (route, path, index, canonicalPath)=>{
|
|
148
|
+
const leadingLocaleParam = getLeadingLocaleParam(route.path);
|
|
149
|
+
const localisedPath = leadingLocaleParam ? normaliseRoutePath(`${leadingLocaleParam}/${path}`) : path;
|
|
150
|
+
const routeWithPath = {
|
|
151
|
+
...route,
|
|
152
|
+
path: localisedPath
|
|
153
|
+
};
|
|
154
|
+
routeWithPath.modernCanonicalPath = canonicalPath;
|
|
155
|
+
return 0 === index ? routeWithPath : suffixRouteIds(routeWithPath, legalRouteIdPart(localisedPath));
|
|
156
|
+
};
|
|
157
|
+
const applyLocalisedUrlsToRoutes = (routes, languages, localisedUrls)=>{
|
|
158
|
+
const rootLocalisedPaths = languages.reduce((acc, language)=>{
|
|
159
|
+
acc[language] = '/';
|
|
160
|
+
return acc;
|
|
161
|
+
}, {});
|
|
162
|
+
validateLocalisedUrls(routes, languages, localisedUrls);
|
|
163
|
+
return routes.flatMap((route)=>transformLocalisedRoute(route, '', rootLocalisedPaths, languages, localisedUrls));
|
|
164
|
+
};
|
|
165
|
+
const escapeRegExp = (value)=>value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
166
|
+
const getParamName = (segment)=>segment.slice(1).replace(/\?$/, '');
|
|
167
|
+
const compilePathPattern = (pattern)=>{
|
|
168
|
+
const names = [];
|
|
169
|
+
const segments = normalisePathPattern(pattern).split('/').filter(Boolean);
|
|
170
|
+
const source = segments.map((segment)=>{
|
|
171
|
+
if (segment.startsWith(':')) {
|
|
172
|
+
names.push(getParamName(segment));
|
|
173
|
+
const paramPattern = '([^/]+)';
|
|
174
|
+
return segment.endsWith('?') ? `(?:/${paramPattern})?` : `/${paramPattern}`;
|
|
175
|
+
}
|
|
176
|
+
if ('*' === segment) {
|
|
177
|
+
names.push('*');
|
|
178
|
+
return '/(.*)';
|
|
179
|
+
}
|
|
180
|
+
return `/${escapeRegExp(segment)}`;
|
|
181
|
+
}).join('');
|
|
182
|
+
return {
|
|
183
|
+
names,
|
|
184
|
+
regexp: new RegExp(`^${source || '/'}$`)
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
const matchPathPattern = (pathname, pattern)=>{
|
|
188
|
+
const { names, regexp } = compilePathPattern(pattern);
|
|
189
|
+
const match = regexp.exec(normalisePathPattern(pathname));
|
|
190
|
+
if (!match) return null;
|
|
191
|
+
return names.reduce((params, name, index)=>{
|
|
192
|
+
params[name] = decodeURIComponent(match[index + 1] || '');
|
|
193
|
+
return params;
|
|
194
|
+
}, {});
|
|
195
|
+
};
|
|
196
|
+
const buildPathFromPattern = (pattern, params)=>{
|
|
197
|
+
const segments = normalisePathPattern(pattern).split('/').filter(Boolean);
|
|
198
|
+
const path = segments.map((segment)=>{
|
|
199
|
+
if (segment.startsWith(':')) {
|
|
200
|
+
const param = params[getParamName(segment)];
|
|
201
|
+
return param ? encodeURIComponent(param) : '';
|
|
202
|
+
}
|
|
203
|
+
if ('*' === segment) return params['*'] || '';
|
|
204
|
+
return segment;
|
|
205
|
+
}).filter(Boolean).join('/');
|
|
206
|
+
return `/${path}`;
|
|
207
|
+
};
|
|
208
|
+
const resolveLocalisedPath = (pathname, targetLanguage, languages, localisedUrls)=>{
|
|
209
|
+
const normalizedPathname = normalisePathPattern(pathname);
|
|
210
|
+
for (const [canonicalPattern, localisedUrlEntry] of Object.entries(localisedUrls)){
|
|
211
|
+
const targetPattern = localisedUrlEntry[targetLanguage];
|
|
212
|
+
if (!targetPattern) continue;
|
|
213
|
+
const params = matchPathPattern(normalizedPathname, canonicalPattern);
|
|
214
|
+
if (params) return buildPathFromPattern(targetPattern, params);
|
|
215
|
+
}
|
|
216
|
+
for (const localisedUrlEntry of Object.values(localisedUrls)){
|
|
217
|
+
const targetPattern = localisedUrlEntry[targetLanguage];
|
|
218
|
+
if (targetPattern) for (const language of languages){
|
|
219
|
+
const sourcePattern = localisedUrlEntry[language];
|
|
220
|
+
if (!sourcePattern) continue;
|
|
221
|
+
const params = matchPathPattern(normalizedPathname, sourcePattern);
|
|
222
|
+
if (params) return buildPathFromPattern(targetPattern, params);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return normalizedPathname;
|
|
226
|
+
};
|
|
227
|
+
const resolveCanonicalLocalisedPath = (pathname, languages, localisedUrls)=>{
|
|
228
|
+
const normalizedPathname = normalisePathPattern(pathname);
|
|
229
|
+
for (const [canonicalPattern, localisedUrlEntry] of Object.entries(localisedUrls)){
|
|
230
|
+
const canonicalParams = matchPathPattern(normalizedPathname, canonicalPattern);
|
|
231
|
+
if (canonicalParams) return buildPathFromPattern(canonicalPattern, canonicalParams);
|
|
232
|
+
for (const language of languages){
|
|
233
|
+
const sourcePattern = localisedUrlEntry[language];
|
|
234
|
+
if (!sourcePattern) continue;
|
|
235
|
+
const params = matchPathPattern(normalizedPathname, sourcePattern);
|
|
236
|
+
if (params) return buildPathFromPattern(canonicalPattern, params);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return normalizedPathname;
|
|
240
|
+
};
|
|
241
|
+
__webpack_require__.d(__webpack_exports__, {}, {
|
|
242
|
+
applyLocalisedUrlsToRoutes: applyLocalisedUrlsToRoutes,
|
|
243
|
+
buildPathFromPattern: buildPathFromPattern,
|
|
244
|
+
matchPathPattern: matchPathPattern,
|
|
245
|
+
normalisePathPattern: normalisePathPattern,
|
|
246
|
+
resolveCanonicalLocalisedPath: resolveCanonicalLocalisedPath,
|
|
247
|
+
resolveLocalisedPath: resolveLocalisedPath,
|
|
248
|
+
resolveLocalisedUrlsConfig: resolveLocalisedUrlsConfig,
|
|
249
|
+
validateLocalisedUrls: validateLocalisedUrls
|
|
250
|
+
});
|
|
251
|
+
exports.applyLocalisedUrlsToRoutes = __webpack_exports__.applyLocalisedUrlsToRoutes;
|
|
252
|
+
exports.buildPathFromPattern = __webpack_exports__.buildPathFromPattern;
|
|
253
|
+
exports.matchPathPattern = __webpack_exports__.matchPathPattern;
|
|
254
|
+
exports.normalisePathPattern = __webpack_exports__.normalisePathPattern;
|
|
255
|
+
exports.resolveCanonicalLocalisedPath = __webpack_exports__.resolveCanonicalLocalisedPath;
|
|
256
|
+
exports.resolveLocalisedPath = __webpack_exports__.resolveLocalisedPath;
|
|
257
|
+
exports.resolveLocalisedUrlsConfig = __webpack_exports__.resolveLocalisedUrlsConfig;
|
|
258
|
+
exports.validateLocalisedUrls = __webpack_exports__.validateLocalisedUrls;
|
|
259
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
260
|
+
"applyLocalisedUrlsToRoutes",
|
|
261
|
+
"buildPathFromPattern",
|
|
262
|
+
"matchPathPattern",
|
|
263
|
+
"normalisePathPattern",
|
|
264
|
+
"resolveCanonicalLocalisedPath",
|
|
265
|
+
"resolveLocalisedPath",
|
|
266
|
+
"resolveLocalisedUrlsConfig",
|
|
267
|
+
"validateLocalisedUrls"
|
|
268
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
269
|
+
Object.defineProperty(exports, '__esModule', {
|
|
270
|
+
value: true
|
|
271
|
+
});
|
package/dist/cjs/shared/utils.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __webpack_require__ = {};
|
|
3
3
|
(()=>{
|
|
4
|
-
__webpack_require__.d = (exports1,
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
__webpack_require__.d = (exports1, getters, values)=>{
|
|
5
|
+
var define = (defs, kind)=>{
|
|
6
|
+
for(var key in defs)if (__webpack_require__.o(defs, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
[kind]: defs[key]
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
define(getters, "get");
|
|
12
|
+
define(values, "value");
|
|
9
13
|
};
|
|
10
14
|
})();
|
|
11
15
|
(()=>{
|
|
@@ -23,12 +27,6 @@ var __webpack_require__ = {};
|
|
|
23
27
|
})();
|
|
24
28
|
var __webpack_exports__ = {};
|
|
25
29
|
__webpack_require__.r(__webpack_exports__);
|
|
26
|
-
__webpack_require__.d(__webpack_exports__, {
|
|
27
|
-
getBackendOptions: ()=>getBackendOptions,
|
|
28
|
-
getEntryConfig: ()=>getEntryConfig,
|
|
29
|
-
getLocaleDetectionOptions: ()=>getLocaleDetectionOptions,
|
|
30
|
-
removeEntryConfigKey: ()=>removeEntryConfigKey
|
|
31
|
-
});
|
|
32
30
|
function getEntryConfig(entryName, config, entryKey) {
|
|
33
31
|
const entryConfigMap = config[entryKey];
|
|
34
32
|
return entryConfigMap?.[entryName];
|
|
@@ -63,6 +61,12 @@ function getBackendOptions(entryName, backend) {
|
|
|
63
61
|
if ('backendOptionsByEntry' in fullConfig) return removeEntryConfigKey(fullConfig, 'backendOptionsByEntry');
|
|
64
62
|
return backend;
|
|
65
63
|
}
|
|
64
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
65
|
+
getBackendOptions: ()=>getBackendOptions,
|
|
66
|
+
getEntryConfig: ()=>getEntryConfig,
|
|
67
|
+
getLocaleDetectionOptions: ()=>getLocaleDetectionOptions,
|
|
68
|
+
removeEntryConfigKey: ()=>removeEntryConfigKey
|
|
69
|
+
});
|
|
66
70
|
exports.getBackendOptions = __webpack_exports__.getBackendOptions;
|
|
67
71
|
exports.getEntryConfig = __webpack_exports__.getEntryConfig;
|
|
68
72
|
exports.getLocaleDetectionOptions = __webpack_exports__.getLocaleDetectionOptions;
|
package/dist/esm/cli/index.mjs
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { getPublicDirRoutePrefixes } from "@modern-js/server-core";
|
|
2
2
|
import fs from "fs";
|
|
3
3
|
import path from "path";
|
|
4
|
+
import { applyLocalisedUrlsToRoutes, resolveLocalisedUrlsConfig } from "../shared/localisedUrls.mjs";
|
|
4
5
|
import { getBackendOptions, getLocaleDetectionOptions } from "../shared/utils.mjs";
|
|
6
|
+
import "../runtime/types.mjs";
|
|
5
7
|
function hasJsonFiles(dirPath) {
|
|
6
8
|
try {
|
|
7
9
|
if (!fs.existsSync(dirPath) || !fs.statSync(dirPath).isDirectory()) return false;
|
|
@@ -80,6 +82,27 @@ const i18nPlugin = (options = {})=>({
|
|
|
80
82
|
plugins
|
|
81
83
|
};
|
|
82
84
|
});
|
|
85
|
+
api.modifyFileSystemRoutes(({ entrypoint, routes })=>{
|
|
86
|
+
if (!localeDetection) return {
|
|
87
|
+
entrypoint,
|
|
88
|
+
routes
|
|
89
|
+
};
|
|
90
|
+
const localeDetectionOptions = getLocaleDetectionOptions(entrypoint.entryName, localeDetection);
|
|
91
|
+
const { localePathRedirect, languages = [], localisedUrls } = localeDetectionOptions;
|
|
92
|
+
if (!localePathRedirect || 0 === languages.length) return {
|
|
93
|
+
entrypoint,
|
|
94
|
+
routes
|
|
95
|
+
};
|
|
96
|
+
const localisedUrlsConfig = resolveLocalisedUrlsConfig(localisedUrls);
|
|
97
|
+
if (!localisedUrlsConfig.enabled) return {
|
|
98
|
+
entrypoint,
|
|
99
|
+
routes
|
|
100
|
+
};
|
|
101
|
+
return {
|
|
102
|
+
entrypoint,
|
|
103
|
+
routes: applyLocalisedUrlsToRoutes(routes, languages, localisedUrlsConfig.map)
|
|
104
|
+
};
|
|
105
|
+
});
|
|
83
106
|
api._internalServerPlugins(({ plugins })=>{
|
|
84
107
|
const { serverRoutes, metaName } = api.getAppContext();
|
|
85
108
|
const normalizedConfig = api.getNormalizedConfig();
|
|
@@ -1,28 +1,13 @@
|
|
|
1
1
|
import { jsx } from "react/jsx-runtime";
|
|
2
|
-
import { Link
|
|
3
|
-
|
|
4
|
-
import { buildLocalizedUrl } from "./utils.mjs";
|
|
5
|
-
const useRouterHooks = ()=>{
|
|
6
|
-
const inRouter = useInRouterContext();
|
|
7
|
-
return {
|
|
8
|
-
Link: inRouter ? router_Link : null,
|
|
9
|
-
params: inRouter ? useParams() : {},
|
|
10
|
-
hasRouter: inRouter
|
|
11
|
-
};
|
|
12
|
-
};
|
|
2
|
+
import { Link } from "./Link.mjs";
|
|
3
|
+
let warnedDeprecation = false;
|
|
13
4
|
const I18nLink = ({ to, children, ...props })=>{
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if ('development' === process.env.NODE_ENV && hasRouter && !params.lang) console.warn("I18nLink is being used outside of a :lang dynamic route context. This may cause unexpected behavior. Please ensure I18nLink is used within a route that has a :lang parameter.");
|
|
19
|
-
if (!hasRouter || !Link) return /*#__PURE__*/ jsx("a", {
|
|
20
|
-
href: localizedTo,
|
|
21
|
-
...props,
|
|
22
|
-
children: children
|
|
23
|
-
});
|
|
5
|
+
if ('development' === process.env.NODE_ENV && !warnedDeprecation) {
|
|
6
|
+
warnedDeprecation = true;
|
|
7
|
+
console.warn("[plugin-i18n] I18nLink is deprecated. Import { Link } from '@modern-js/plugin-i18n/runtime' instead — it accepts the same language-agnostic `to` values.");
|
|
8
|
+
}
|
|
24
9
|
return /*#__PURE__*/ jsx(Link, {
|
|
25
|
-
to:
|
|
10
|
+
to: to,
|
|
26
11
|
...props,
|
|
27
12
|
children: children
|
|
28
13
|
});
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useMemo } from "react";
|
|
3
|
+
import { useModernI18n } from "./context.mjs";
|
|
4
|
+
import { canonicalPath } from "./localizedPaths.mjs";
|
|
5
|
+
import { useI18nRouterAdapter } from "./routerAdapter.mjs";
|
|
6
|
+
import { buildLocalizedUrl, splitUrlTarget } from "./utils.mjs";
|
|
7
|
+
const EXTERNAL_TARGET_RE = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i;
|
|
8
|
+
const warnedTargets = new Set();
|
|
9
|
+
const warnOnce = (key, message)=>{
|
|
10
|
+
if ('development' !== process.env.NODE_ENV || warnedTargets.has(key)) return;
|
|
11
|
+
warnedTargets.add(key);
|
|
12
|
+
console.warn(message);
|
|
13
|
+
};
|
|
14
|
+
const interpolateRouteParams = (pathname, params)=>{
|
|
15
|
+
if (!/[$:*{]/.test(pathname)) return pathname;
|
|
16
|
+
const resolveParam = (name)=>{
|
|
17
|
+
const value = params?.[name];
|
|
18
|
+
return void 0 === value ? void 0 : String(value);
|
|
19
|
+
};
|
|
20
|
+
const segments = pathname.split('/').map((segment)=>{
|
|
21
|
+
if (!segment) return segment;
|
|
22
|
+
if (segment.startsWith('{-$') && segment.endsWith('}')) {
|
|
23
|
+
const value = resolveParam(segment.slice(3, -1));
|
|
24
|
+
return void 0 === value ? null : encodeURIComponent(value);
|
|
25
|
+
}
|
|
26
|
+
if ('$' === segment || '*' === segment) {
|
|
27
|
+
const value = resolveParam('_splat') ?? resolveParam('*');
|
|
28
|
+
return void 0 === value ? null : value.split('/').map(encodeURIComponent).join('/');
|
|
29
|
+
}
|
|
30
|
+
if (segment.startsWith('$')) {
|
|
31
|
+
const value = resolveParam(segment.slice(1));
|
|
32
|
+
if (void 0 === value) {
|
|
33
|
+
warnOnce(`missing-param:${pathname}:${segment}`, `[plugin-i18n] <Link to="${pathname}"> is missing required param "${segment.slice(1)}".`);
|
|
34
|
+
return segment;
|
|
35
|
+
}
|
|
36
|
+
return encodeURIComponent(value);
|
|
37
|
+
}
|
|
38
|
+
if (segment.startsWith(':')) {
|
|
39
|
+
const optional = segment.endsWith('?');
|
|
40
|
+
const name = segment.slice(1, optional ? -1 : void 0);
|
|
41
|
+
const value = resolveParam(name);
|
|
42
|
+
if (void 0 === value) {
|
|
43
|
+
if (optional) return null;
|
|
44
|
+
warnOnce(`missing-param:${pathname}:${segment}`, `[plugin-i18n] <Link to="${pathname}"> is missing required param "${name}".`);
|
|
45
|
+
return segment;
|
|
46
|
+
}
|
|
47
|
+
return encodeURIComponent(value);
|
|
48
|
+
}
|
|
49
|
+
return segment;
|
|
50
|
+
}).filter((segment)=>null !== segment);
|
|
51
|
+
return segments.join('/') || '/';
|
|
52
|
+
};
|
|
53
|
+
const normalizeSearch = (search, searchFromTo)=>{
|
|
54
|
+
if (search && 'object' == typeof search) {
|
|
55
|
+
const entries = Object.entries(search).filter(([, value])=>null != value);
|
|
56
|
+
const searchObject = Object.fromEntries(entries.map(([key, value])=>[
|
|
57
|
+
key,
|
|
58
|
+
String(value)
|
|
59
|
+
]));
|
|
60
|
+
const params = new URLSearchParams(searchObject);
|
|
61
|
+
const serialized = params.toString();
|
|
62
|
+
return {
|
|
63
|
+
searchString: serialized ? `?${serialized}` : '',
|
|
64
|
+
searchObject
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
const raw = 'string' == typeof search && search ? search : searchFromTo;
|
|
68
|
+
if (!raw) return {
|
|
69
|
+
searchString: '',
|
|
70
|
+
searchObject: void 0
|
|
71
|
+
};
|
|
72
|
+
const searchString = raw.startsWith('?') ? raw : `?${raw}`;
|
|
73
|
+
const searchObject = {};
|
|
74
|
+
new URLSearchParams(searchString).forEach((value, key)=>{
|
|
75
|
+
searchObject[key] = value;
|
|
76
|
+
});
|
|
77
|
+
return {
|
|
78
|
+
searchString,
|
|
79
|
+
searchObject
|
|
80
|
+
};
|
|
81
|
+
};
|
|
82
|
+
const splitActiveProps = (active, activeProps)=>{
|
|
83
|
+
if (!active || !activeProps) return {};
|
|
84
|
+
return activeProps;
|
|
85
|
+
};
|
|
86
|
+
const mergeClassNames = (...values)=>{
|
|
87
|
+
const classNames = values.filter((value)=>'string' == typeof value && value.length > 0);
|
|
88
|
+
return classNames.length > 0 ? classNames.join(' ') : void 0;
|
|
89
|
+
};
|
|
90
|
+
const Link = (props)=>{
|
|
91
|
+
const { to, params, children, hash: hashProp, search: searchProp, hashScrollIntoView, activeOptions, activeProps, ...rest } = props;
|
|
92
|
+
const adapter = useI18nRouterAdapter();
|
|
93
|
+
const { language, supportedLanguages, localisedUrls } = useModernI18n();
|
|
94
|
+
const config = {
|
|
95
|
+
languages: supportedLanguages,
|
|
96
|
+
localisedUrls
|
|
97
|
+
};
|
|
98
|
+
const isExternal = EXTERNAL_TARGET_RE.test(to);
|
|
99
|
+
const isBareHash = to.startsWith('#');
|
|
100
|
+
const target = useMemo(()=>{
|
|
101
|
+
if (isExternal || isBareHash) return null;
|
|
102
|
+
const { pathname, search: searchFromTo, hash: hashFromTo } = splitUrlTarget(to);
|
|
103
|
+
const interpolated = interpolateRouteParams(pathname || '/', params);
|
|
104
|
+
const firstSegment = interpolated.split('/').filter(Boolean)[0];
|
|
105
|
+
if (firstSegment && supportedLanguages.includes(firstSegment)) warnOnce(`lang-prefix:${to}`, `[plugin-i18n] <Link to="${to}"> starts with a language prefix. Write language-agnostic canonical paths; the Link localizes them automatically.`);
|
|
106
|
+
const localizedPathname = buildLocalizedUrl(interpolated, language, supportedLanguages, localisedUrls);
|
|
107
|
+
const hash = hashProp ?? (hashFromTo ? hashFromTo.slice(1) : '');
|
|
108
|
+
const { searchString, searchObject } = normalizeSearch(searchProp, searchFromTo);
|
|
109
|
+
return {
|
|
110
|
+
canonicalPathname: interpolated,
|
|
111
|
+
localizedPathname,
|
|
112
|
+
hash,
|
|
113
|
+
searchString,
|
|
114
|
+
searchObject,
|
|
115
|
+
href: `${localizedPathname}${searchString}${hash ? `#${hash}` : ''}`
|
|
116
|
+
};
|
|
117
|
+
}, [
|
|
118
|
+
to,
|
|
119
|
+
params,
|
|
120
|
+
hashProp,
|
|
121
|
+
searchProp,
|
|
122
|
+
isExternal,
|
|
123
|
+
isBareHash,
|
|
124
|
+
language,
|
|
125
|
+
supportedLanguages,
|
|
126
|
+
localisedUrls
|
|
127
|
+
]);
|
|
128
|
+
const isActive = useMemo(()=>{
|
|
129
|
+
if (!target || !adapter.location) return false;
|
|
130
|
+
const current = canonicalPath(adapter.location.pathname, config);
|
|
131
|
+
const targetCanonical = canonicalPath(target.canonicalPathname, config);
|
|
132
|
+
const exact = activeOptions?.exact ?? '/' === targetCanonical;
|
|
133
|
+
if (current === targetCanonical) return true;
|
|
134
|
+
if (exact) return false;
|
|
135
|
+
return current.startsWith('/' === targetCanonical ? '/' : `${targetCanonical}/`);
|
|
136
|
+
}, [
|
|
137
|
+
target,
|
|
138
|
+
adapter.location,
|
|
139
|
+
activeOptions?.exact,
|
|
140
|
+
supportedLanguages,
|
|
141
|
+
localisedUrls
|
|
142
|
+
]);
|
|
143
|
+
const resolvedActiveProps = splitActiveProps(isActive, activeProps);
|
|
144
|
+
const activeAttributes = isActive ? {
|
|
145
|
+
'data-status': 'active',
|
|
146
|
+
'aria-current': rest['aria-current'] ?? resolvedActiveProps['aria-current'] ?? 'page'
|
|
147
|
+
} : {};
|
|
148
|
+
if (!target) {
|
|
149
|
+
const { prefetch: _prefetch, preload: _preload, replace: _replace, ...anchorProps } = rest;
|
|
150
|
+
return /*#__PURE__*/ jsx("a", {
|
|
151
|
+
href: to,
|
|
152
|
+
...anchorProps,
|
|
153
|
+
children: children
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
const { Link: RouterLink, hasRouter, framework } = adapter;
|
|
157
|
+
if (!hasRouter || !RouterLink) {
|
|
158
|
+
const { prefetch: _prefetch, preload: _preload, replace: _replace, ...anchorProps } = rest;
|
|
159
|
+
const { className: activeClassName, style: activeStyle, ...activeRest } = resolvedActiveProps;
|
|
160
|
+
return /*#__PURE__*/ jsx("a", {
|
|
161
|
+
href: target.href,
|
|
162
|
+
...anchorProps,
|
|
163
|
+
...activeRest,
|
|
164
|
+
...activeAttributes,
|
|
165
|
+
className: mergeClassNames(rest.className, activeClassName),
|
|
166
|
+
style: {
|
|
167
|
+
...rest.style,
|
|
168
|
+
...activeStyle
|
|
169
|
+
},
|
|
170
|
+
children: children
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
const { className: activeClassName, style: activeStyle, ...activeRest } = resolvedActiveProps;
|
|
174
|
+
const mergedClassName = mergeClassNames(rest.className, activeClassName);
|
|
175
|
+
const mergedStyle = {
|
|
176
|
+
...rest.style,
|
|
177
|
+
...activeStyle
|
|
178
|
+
};
|
|
179
|
+
if ('tanstack' === framework) return /*#__PURE__*/ jsx(RouterLink, {
|
|
180
|
+
to: target.localizedPathname,
|
|
181
|
+
...target.searchObject ? {
|
|
182
|
+
search: target.searchObject
|
|
183
|
+
} : {},
|
|
184
|
+
...target.hash ? {
|
|
185
|
+
hash: target.hash
|
|
186
|
+
} : {},
|
|
187
|
+
...void 0 === hashScrollIntoView ? {} : {
|
|
188
|
+
hashScrollIntoView
|
|
189
|
+
},
|
|
190
|
+
...rest,
|
|
191
|
+
...activeRest,
|
|
192
|
+
...activeAttributes,
|
|
193
|
+
className: mergedClassName,
|
|
194
|
+
style: mergedStyle,
|
|
195
|
+
children: children
|
|
196
|
+
});
|
|
197
|
+
return /*#__PURE__*/ jsx(RouterLink, {
|
|
198
|
+
to: target.href,
|
|
199
|
+
...rest,
|
|
200
|
+
...activeRest,
|
|
201
|
+
...activeAttributes,
|
|
202
|
+
className: mergedClassName,
|
|
203
|
+
style: mergedStyle,
|
|
204
|
+
children: children
|
|
205
|
+
});
|
|
206
|
+
};
|
|
207
|
+
const runtime_Link = Link;
|
|
208
|
+
export default runtime_Link;
|
|
209
|
+
export { Link, interpolateRouteParams };
|
|
File without changes
|