@bleedingdev/modern-js-i18n-runtime-extensions 3.9.0-ultramodern.3 → 3.9.0-ultramodern.5
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/dist/cjs/index.js +9 -2
- package/dist/cjs/localeRedirectPolicy.js +96 -0
- package/dist/cjs/localisedUrls/index.js +7 -0
- package/dist/cjs/localisedUrls/routeIdentity.js +118 -0
- package/dist/cjs/localisedUrls/routes.js +8 -0
- package/dist/cjs/paths.js +56 -0
- package/dist/esm/index.mjs +1 -0
- package/dist/esm/localeRedirectPolicy.mjs +46 -0
- package/dist/esm/localisedUrls/index.mjs +1 -0
- package/dist/esm/localisedUrls/routeIdentity.mjs +77 -0
- package/dist/esm/localisedUrls/routes.mjs +8 -0
- package/dist/esm/paths.mjs +12 -0
- package/dist/esm-node/index.mjs +1 -0
- package/dist/esm-node/localeRedirectPolicy.mjs +47 -0
- package/dist/esm-node/localisedUrls/index.mjs +1 -0
- package/dist/esm-node/localisedUrls/routeIdentity.mjs +78 -0
- package/dist/esm-node/localisedUrls/routes.mjs +8 -0
- package/dist/esm-node/paths.mjs +13 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/localeRedirectPolicy.d.ts +22 -0
- package/dist/types/localisedUrls/index.d.ts +1 -0
- package/dist/types/localisedUrls/routeIdentity.d.ts +24 -0
- package/dist/types/paths.d.ts +9 -0
- package/package.json +16 -4
- package/src/index.ts +1 -0
- package/src/localeRedirectPolicy.ts +127 -0
- package/src/localisedUrls/index.ts +4 -0
- package/src/localisedUrls/routeIdentity.ts +107 -0
- package/src/localisedUrls/routes.ts +10 -0
- package/src/paths.ts +37 -0
- package/tests/localeRedirectPolicy.test.ts +205 -0
- package/tests/localisedUrls.test.ts +3 -113
- package/tests/paths.test.ts +189 -0
- package/tests/reactLanguageSync.test.tsx +20 -1
- package/tests/clientRedirectIntegration.test.tsx +0 -74
package/dist/cjs/index.js
CHANGED
|
@@ -6,6 +6,9 @@ var __webpack_modules__ = {
|
|
|
6
6
|
"./language-sync/react" (module) {
|
|
7
7
|
module.exports = require("./language-sync/react.js");
|
|
8
8
|
},
|
|
9
|
+
"./localeRedirectPolicy" (module) {
|
|
10
|
+
module.exports = require("./localeRedirectPolicy.js");
|
|
11
|
+
},
|
|
9
12
|
"./localisedUrls/index" (module) {
|
|
10
13
|
module.exports = require("./localisedUrls/index.js");
|
|
11
14
|
}
|
|
@@ -65,9 +68,13 @@ var __webpack_exports__ = {};
|
|
|
65
68
|
var __rspack_reexport = {};
|
|
66
69
|
for(const __rspack_import_key in _language_sync_react__rspack_import_1)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_language_sync_react__rspack_import_1[__rspack_import_key];
|
|
67
70
|
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
68
|
-
var
|
|
71
|
+
var _localeRedirectPolicy__rspack_import_2 = __webpack_require__("./localeRedirectPolicy");
|
|
72
|
+
var __rspack_reexport = {};
|
|
73
|
+
for(const __rspack_import_key in _localeRedirectPolicy__rspack_import_2)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_localeRedirectPolicy__rspack_import_2[__rspack_import_key];
|
|
74
|
+
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
75
|
+
var _localisedUrls_index__rspack_import_3 = __webpack_require__("./localisedUrls/index");
|
|
69
76
|
var __rspack_reexport = {};
|
|
70
|
-
for(const __rspack_import_key in
|
|
77
|
+
for(const __rspack_import_key in _localisedUrls_index__rspack_import_3)if ("default" !== __rspack_import_key) __rspack_reexport[__rspack_import_key] = ()=>_localisedUrls_index__rspack_import_3[__rspack_import_key];
|
|
71
78
|
__webpack_require__.d(__webpack_exports__, __rspack_reexport);
|
|
72
79
|
})();
|
|
73
80
|
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
@@ -0,0 +1,96 @@
|
|
|
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
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
31
|
+
buildLocalizedUrl: ()=>buildLocalizedUrl,
|
|
32
|
+
createLocaleRedirectResponse: ()=>createLocaleRedirectResponse,
|
|
33
|
+
getLanguageFromPath: ()=>getLanguageFromPath,
|
|
34
|
+
isStaticResourceRequest: ()=>isStaticResourceRequest,
|
|
35
|
+
shouldIgnoreRedirect: ()=>shouldIgnoreRedirect
|
|
36
|
+
});
|
|
37
|
+
const index_js_namespaceObject = require("./localisedUrls/index.js");
|
|
38
|
+
const stripUrlPathPrefix = (pathname, urlPath)=>{
|
|
39
|
+
const basePath = urlPath.replace('/*', '');
|
|
40
|
+
if (!basePath || '/' === basePath) return pathname;
|
|
41
|
+
const remainingPath = pathname.startsWith(basePath) ? pathname.slice(basePath.length) : pathname;
|
|
42
|
+
return remainingPath || '/';
|
|
43
|
+
};
|
|
44
|
+
const shouldIgnoreRedirect = (pathname, urlPath, ignoreRedirectRoutes)=>{
|
|
45
|
+
const remainingPath = stripUrlPathPrefix(pathname, urlPath);
|
|
46
|
+
return (0, index_js_namespaceObject.shouldSkipLocaleRedirect)(remainingPath, [], ignoreRedirectRoutes);
|
|
47
|
+
};
|
|
48
|
+
const isStaticResourceRequest = (pathname, staticRoutePrefixes, languages = [])=>{
|
|
49
|
+
if ((0, index_js_namespaceObject.isDefaultLocaleRedirectSkipPath)(pathname, languages)) return true;
|
|
50
|
+
const matchesStaticRoutePrefix = (targetPathname)=>staticRoutePrefixes.some((prefix)=>(0, index_js_namespaceObject.matchesPathPrefix)(targetPathname, prefix));
|
|
51
|
+
if (matchesStaticRoutePrefix(pathname)) return true;
|
|
52
|
+
const pathWithoutLanguage = (0, index_js_namespaceObject.stripLanguagePrefix)(pathname, languages);
|
|
53
|
+
return pathWithoutLanguage !== pathname && matchesStaticRoutePrefix(pathWithoutLanguage);
|
|
54
|
+
};
|
|
55
|
+
const getLanguageFromPath = (req, urlPath, languages)=>{
|
|
56
|
+
const url = new URL(req.url, `http://${req.header().host}`);
|
|
57
|
+
const pathname = url.pathname;
|
|
58
|
+
const remainingPath = stripUrlPathPrefix(pathname, urlPath);
|
|
59
|
+
const segments = remainingPath.split('/').filter(Boolean);
|
|
60
|
+
const firstSegment = segments[0];
|
|
61
|
+
if (languages.includes(firstSegment)) return firstSegment;
|
|
62
|
+
return null;
|
|
63
|
+
};
|
|
64
|
+
const buildLocalizedUrl = (req, urlPath, language, languages, localisedUrls)=>{
|
|
65
|
+
const url = new URL(req.url);
|
|
66
|
+
const pathname = url.pathname;
|
|
67
|
+
const basePath = urlPath.replace('/*', '');
|
|
68
|
+
const remainingPath = stripUrlPathPrefix(pathname, urlPath);
|
|
69
|
+
const newPathname = (0, index_js_namespaceObject.localiseTargetPathname)(remainingPath, language, languages, localisedUrls);
|
|
70
|
+
const suffix = `${url.search}${url.hash}`;
|
|
71
|
+
const localizedUrl = '/' === basePath ? newPathname + suffix : basePath + newPathname + suffix;
|
|
72
|
+
return localizedUrl;
|
|
73
|
+
};
|
|
74
|
+
const createLocaleRedirectResponse = (location)=>new Response(null, {
|
|
75
|
+
status: 302,
|
|
76
|
+
headers: {
|
|
77
|
+
'Cache-Control': 'private, no-store',
|
|
78
|
+
Location: location,
|
|
79
|
+
Vary: 'Accept-Language, Cookie'
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
exports.buildLocalizedUrl = __webpack_exports__.buildLocalizedUrl;
|
|
83
|
+
exports.createLocaleRedirectResponse = __webpack_exports__.createLocaleRedirectResponse;
|
|
84
|
+
exports.getLanguageFromPath = __webpack_exports__.getLanguageFromPath;
|
|
85
|
+
exports.isStaticResourceRequest = __webpack_exports__.isStaticResourceRequest;
|
|
86
|
+
exports.shouldIgnoreRedirect = __webpack_exports__.shouldIgnoreRedirect;
|
|
87
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
88
|
+
"buildLocalizedUrl",
|
|
89
|
+
"createLocaleRedirectResponse",
|
|
90
|
+
"getLanguageFromPath",
|
|
91
|
+
"isStaticResourceRequest",
|
|
92
|
+
"shouldIgnoreRedirect"
|
|
93
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
94
|
+
Object.defineProperty(exports, '__esModule', {
|
|
95
|
+
value: true
|
|
96
|
+
});
|
|
@@ -32,6 +32,8 @@ __webpack_require__.d(__webpack_exports__, {
|
|
|
32
32
|
applyLocalisedUrlsToRoutes: ()=>external_routes_js_namespaceObject.applyLocalisedUrlsToRoutes,
|
|
33
33
|
buildPathFromPattern: ()=>external_patterns_js_namespaceObject.buildPathFromPattern,
|
|
34
34
|
canonicalTargetPathname: ()=>external_pathname_js_namespaceObject.canonicalTargetPathname,
|
|
35
|
+
canonicaliseLocalisedRoutes: ()=>external_routeIdentity_js_namespaceObject.canonicaliseLocalisedRoutes,
|
|
36
|
+
createLocalisedRouteRewrite: ()=>external_routeIdentity_js_namespaceObject.createLocalisedRouteRewrite,
|
|
35
37
|
isDefaultLocaleRedirectSkipPath: ()=>external_redirect_js_namespaceObject.isDefaultLocaleRedirectSkipPath,
|
|
36
38
|
localiseTargetPathname: ()=>external_pathname_js_namespaceObject.localiseTargetPathname,
|
|
37
39
|
matchPathPattern: ()=>external_patterns_js_namespaceObject.matchPathPattern,
|
|
@@ -51,11 +53,14 @@ const external_pathname_js_namespaceObject = require("./pathname.js");
|
|
|
51
53
|
const external_patterns_js_namespaceObject = require("./patterns.js");
|
|
52
54
|
const external_redirect_js_namespaceObject = require("./redirect.js");
|
|
53
55
|
const external_resolve_js_namespaceObject = require("./resolve.js");
|
|
56
|
+
const external_routeIdentity_js_namespaceObject = require("./routeIdentity.js");
|
|
54
57
|
const external_routes_js_namespaceObject = require("./routes.js");
|
|
55
58
|
exports.DEFAULT_LOCALE_REDIRECT_SKIP_RULES = __webpack_exports__.DEFAULT_LOCALE_REDIRECT_SKIP_RULES;
|
|
56
59
|
exports.applyLocalisedUrlsToRoutes = __webpack_exports__.applyLocalisedUrlsToRoutes;
|
|
57
60
|
exports.buildPathFromPattern = __webpack_exports__.buildPathFromPattern;
|
|
58
61
|
exports.canonicalTargetPathname = __webpack_exports__.canonicalTargetPathname;
|
|
62
|
+
exports.canonicaliseLocalisedRoutes = __webpack_exports__.canonicaliseLocalisedRoutes;
|
|
63
|
+
exports.createLocalisedRouteRewrite = __webpack_exports__.createLocalisedRouteRewrite;
|
|
59
64
|
exports.isDefaultLocaleRedirectSkipPath = __webpack_exports__.isDefaultLocaleRedirectSkipPath;
|
|
60
65
|
exports.localiseTargetPathname = __webpack_exports__.localiseTargetPathname;
|
|
61
66
|
exports.matchPathPattern = __webpack_exports__.matchPathPattern;
|
|
@@ -73,6 +78,8 @@ for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
|
73
78
|
"applyLocalisedUrlsToRoutes",
|
|
74
79
|
"buildPathFromPattern",
|
|
75
80
|
"canonicalTargetPathname",
|
|
81
|
+
"canonicaliseLocalisedRoutes",
|
|
82
|
+
"createLocalisedRouteRewrite",
|
|
76
83
|
"isDefaultLocaleRedirectSkipPath",
|
|
77
84
|
"localiseTargetPathname",
|
|
78
85
|
"matchPathPattern",
|
|
@@ -0,0 +1,118 @@
|
|
|
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
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
31
|
+
canonicaliseLocalisedRoutes: ()=>canonicaliseLocalisedRoutes,
|
|
32
|
+
createLocalisedRouteRewrite: ()=>createLocalisedRouteRewrite
|
|
33
|
+
});
|
|
34
|
+
const external_normalise_js_namespaceObject = require("./normalise.js");
|
|
35
|
+
const external_patterns_js_namespaceObject = require("./patterns.js");
|
|
36
|
+
function canonicaliseLocalisedRoutes(routes) {
|
|
37
|
+
const seen = new Set();
|
|
38
|
+
return routes.flatMap((route)=>{
|
|
39
|
+
const identity = route.modernLocalisedRoute;
|
|
40
|
+
const key = identity?.id ?? identity?.path;
|
|
41
|
+
if (void 0 !== key) {
|
|
42
|
+
if (seen.has(key)) return [];
|
|
43
|
+
seen.add(key);
|
|
44
|
+
}
|
|
45
|
+
return [
|
|
46
|
+
{
|
|
47
|
+
...route,
|
|
48
|
+
...identity ? {
|
|
49
|
+
id: identity.id,
|
|
50
|
+
path: identity.path
|
|
51
|
+
} : {},
|
|
52
|
+
...route.children ? {
|
|
53
|
+
children: canonicaliseLocalisedRoutes(route.children)
|
|
54
|
+
} : {}
|
|
55
|
+
}
|
|
56
|
+
];
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
function rewritePath(url, mappings) {
|
|
60
|
+
for (const { from, to } of mappings){
|
|
61
|
+
const params = (0, external_patterns_js_namespaceObject.matchPathPattern)(url.pathname, from);
|
|
62
|
+
if (params) {
|
|
63
|
+
url.pathname = (0, external_patterns_js_namespaceObject.buildPathFromPattern)(to, params);
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return url;
|
|
68
|
+
}
|
|
69
|
+
function createLocalisedRouteRewrite(routes) {
|
|
70
|
+
const input = [];
|
|
71
|
+
const output = [];
|
|
72
|
+
const visit = (nodes, parent)=>{
|
|
73
|
+
for (const route of nodes){
|
|
74
|
+
const fullPath = (0, external_normalise_js_namespaceObject.normalisePathPattern)(`${parent}/${route.path ?? ''}`);
|
|
75
|
+
const identity = route.modernLocalisedRoute;
|
|
76
|
+
if (identity?.paths && fullPath.endsWith(identity.canonicalPath)) {
|
|
77
|
+
const prefix = fullPath.slice(0, -identity.canonicalPath.length);
|
|
78
|
+
const hasLocale = prefix.split('/').some((segment)=>/^:(?:lang|locale|language)\??$/.test(segment));
|
|
79
|
+
for (const [language, localisedPath] of Object.entries(identity.paths)){
|
|
80
|
+
const localePrefix = prefix.split('/').map((segment)=>/^:(?:lang|locale|language)\??$/.test(segment) ? language : segment).join('/');
|
|
81
|
+
const canonical = (0, external_normalise_js_namespaceObject.normalisePathPattern)(`${localePrefix}/${identity.canonicalPath}`);
|
|
82
|
+
const localised = (0, external_normalise_js_namespaceObject.normalisePathPattern)(`${localePrefix}/${localisedPath}`);
|
|
83
|
+
if (canonical !== localised) {
|
|
84
|
+
input.push({
|
|
85
|
+
from: localised,
|
|
86
|
+
to: canonical
|
|
87
|
+
});
|
|
88
|
+
if (hasLocale) output.push({
|
|
89
|
+
from: canonical,
|
|
90
|
+
to: localised
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (route.children) visit(route.children, fullPath);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
visit(canonicaliseLocalisedRoutes(routes), '');
|
|
99
|
+
if (0 === input.length) return;
|
|
100
|
+
const specificity = (pattern)=>pattern.split('/').filter((segment)=>segment && !segment.startsWith(':') && '*' !== segment).length;
|
|
101
|
+
for (const mappings of [
|
|
102
|
+
input,
|
|
103
|
+
output
|
|
104
|
+
])mappings.sort((a, b)=>specificity(b.from) - specificity(a.from));
|
|
105
|
+
return {
|
|
106
|
+
input: ({ url })=>rewritePath(url, input),
|
|
107
|
+
output: ({ url })=>rewritePath(url, output)
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
exports.canonicaliseLocalisedRoutes = __webpack_exports__.canonicaliseLocalisedRoutes;
|
|
111
|
+
exports.createLocalisedRouteRewrite = __webpack_exports__.createLocalisedRouteRewrite;
|
|
112
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
113
|
+
"canonicaliseLocalisedRoutes",
|
|
114
|
+
"createLocalisedRouteRewrite"
|
|
115
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
116
|
+
Object.defineProperty(exports, '__esModule', {
|
|
117
|
+
value: true
|
|
118
|
+
});
|
|
@@ -134,6 +134,14 @@ const transformLocalisedRoute = (route, parentCanonicalPath, parentLocalisedPath
|
|
|
134
134
|
const children = 'children' in route && route.children ? route.children.flatMap((child)=>transformLocalisedRoute(child, canonicalPath, routeLocalisedPaths, languages, localisedUrls)) : void 0;
|
|
135
135
|
const baseRoute = {
|
|
136
136
|
...route,
|
|
137
|
+
...!isFrameworkInternalRoutePath(canonicalPath) ? {
|
|
138
|
+
modernLocalisedRoute: {
|
|
139
|
+
id: route.id,
|
|
140
|
+
path: route.path,
|
|
141
|
+
canonicalPath,
|
|
142
|
+
paths: localisedUrlEntry
|
|
143
|
+
}
|
|
144
|
+
} : {},
|
|
137
145
|
...children ? {
|
|
138
146
|
children
|
|
139
147
|
} : {}
|
|
@@ -0,0 +1,56 @@
|
|
|
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
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
31
|
+
buildLocalizedUrl: ()=>buildLocalizedUrl,
|
|
32
|
+
canonicalPath: ()=>canonicalPath,
|
|
33
|
+
localizePath: ()=>localizePath
|
|
34
|
+
});
|
|
35
|
+
const url_namespaceObject = require("@modern-js/runtime-utils/url");
|
|
36
|
+
const index_js_namespaceObject = require("./localisedUrls/index.js");
|
|
37
|
+
const buildLocalizedUrl = (target, language, languages, localisedUrls)=>{
|
|
38
|
+
const { pathname, search, hash } = (0, url_namespaceObject.splitUrlTarget)(target);
|
|
39
|
+
return `${(0, index_js_namespaceObject.localiseTargetPathname)(pathname, language, languages, localisedUrls)}${search}${hash}`;
|
|
40
|
+
};
|
|
41
|
+
const localizePath = (target, language, config)=>buildLocalizedUrl(target, language, config.languages, config.localisedUrls);
|
|
42
|
+
const canonicalPath = (target, config)=>{
|
|
43
|
+
const { pathname, search, hash } = (0, url_namespaceObject.splitUrlTarget)(target);
|
|
44
|
+
return `${(0, index_js_namespaceObject.canonicalTargetPathname)(pathname, config.languages, config.localisedUrls)}${search}${hash}`;
|
|
45
|
+
};
|
|
46
|
+
exports.buildLocalizedUrl = __webpack_exports__.buildLocalizedUrl;
|
|
47
|
+
exports.canonicalPath = __webpack_exports__.canonicalPath;
|
|
48
|
+
exports.localizePath = __webpack_exports__.localizePath;
|
|
49
|
+
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
50
|
+
"buildLocalizedUrl",
|
|
51
|
+
"canonicalPath",
|
|
52
|
+
"localizePath"
|
|
53
|
+
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
|
54
|
+
Object.defineProperty(exports, '__esModule', {
|
|
55
|
+
value: true
|
|
56
|
+
});
|
package/dist/esm/index.mjs
CHANGED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { isDefaultLocaleRedirectSkipPath, localiseTargetPathname, matchesPathPrefix, shouldSkipLocaleRedirect, stripLanguagePrefix } from "./localisedUrls/index.mjs";
|
|
2
|
+
const stripUrlPathPrefix = (pathname, urlPath)=>{
|
|
3
|
+
const basePath = urlPath.replace('/*', '');
|
|
4
|
+
if (!basePath || '/' === basePath) return pathname;
|
|
5
|
+
const remainingPath = pathname.startsWith(basePath) ? pathname.slice(basePath.length) : pathname;
|
|
6
|
+
return remainingPath || '/';
|
|
7
|
+
};
|
|
8
|
+
const shouldIgnoreRedirect = (pathname, urlPath, ignoreRedirectRoutes)=>{
|
|
9
|
+
const remainingPath = stripUrlPathPrefix(pathname, urlPath);
|
|
10
|
+
return shouldSkipLocaleRedirect(remainingPath, [], ignoreRedirectRoutes);
|
|
11
|
+
};
|
|
12
|
+
const isStaticResourceRequest = (pathname, staticRoutePrefixes, languages = [])=>{
|
|
13
|
+
if (isDefaultLocaleRedirectSkipPath(pathname, languages)) return true;
|
|
14
|
+
const matchesStaticRoutePrefix = (targetPathname)=>staticRoutePrefixes.some((prefix)=>matchesPathPrefix(targetPathname, prefix));
|
|
15
|
+
if (matchesStaticRoutePrefix(pathname)) return true;
|
|
16
|
+
const pathWithoutLanguage = stripLanguagePrefix(pathname, languages);
|
|
17
|
+
return pathWithoutLanguage !== pathname && matchesStaticRoutePrefix(pathWithoutLanguage);
|
|
18
|
+
};
|
|
19
|
+
const getLanguageFromPath = (req, urlPath, languages)=>{
|
|
20
|
+
const url = new URL(req.url, `http://${req.header().host}`);
|
|
21
|
+
const pathname = url.pathname;
|
|
22
|
+
const remainingPath = stripUrlPathPrefix(pathname, urlPath);
|
|
23
|
+
const segments = remainingPath.split('/').filter(Boolean);
|
|
24
|
+
const firstSegment = segments[0];
|
|
25
|
+
if (languages.includes(firstSegment)) return firstSegment;
|
|
26
|
+
return null;
|
|
27
|
+
};
|
|
28
|
+
const buildLocalizedUrl = (req, urlPath, language, languages, localisedUrls)=>{
|
|
29
|
+
const url = new URL(req.url);
|
|
30
|
+
const pathname = url.pathname;
|
|
31
|
+
const basePath = urlPath.replace('/*', '');
|
|
32
|
+
const remainingPath = stripUrlPathPrefix(pathname, urlPath);
|
|
33
|
+
const newPathname = localiseTargetPathname(remainingPath, language, languages, localisedUrls);
|
|
34
|
+
const suffix = `${url.search}${url.hash}`;
|
|
35
|
+
const localizedUrl = '/' === basePath ? newPathname + suffix : basePath + newPathname + suffix;
|
|
36
|
+
return localizedUrl;
|
|
37
|
+
};
|
|
38
|
+
const createLocaleRedirectResponse = (location)=>new Response(null, {
|
|
39
|
+
status: 302,
|
|
40
|
+
headers: {
|
|
41
|
+
'Cache-Control': 'private, no-store',
|
|
42
|
+
Location: location,
|
|
43
|
+
Vary: 'Accept-Language, Cookie'
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
export { buildLocalizedUrl, createLocaleRedirectResponse, getLanguageFromPath, isStaticResourceRequest, shouldIgnoreRedirect };
|
|
@@ -4,4 +4,5 @@ export { canonicalTargetPathname, localiseTargetPathname, stripLanguagePrefix }
|
|
|
4
4
|
export { buildPathFromPattern, matchPathPattern } from "./patterns.mjs";
|
|
5
5
|
export { DEFAULT_LOCALE_REDIRECT_SKIP_RULES, isDefaultLocaleRedirectSkipPath, matchesPathPrefix, shouldSkipLocaleRedirect } from "./redirect.mjs";
|
|
6
6
|
export { resolveCanonicalLocalisedPath, resolveLocalisedPath } from "./resolve.mjs";
|
|
7
|
+
export { canonicaliseLocalisedRoutes, createLocalisedRouteRewrite } from "./routeIdentity.mjs";
|
|
7
8
|
export { applyLocalisedUrlsToRoutes, validateLocalisedUrls } from "./routes.mjs";
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { normalisePathPattern } from "./normalise.mjs";
|
|
2
|
+
import { buildPathFromPattern, matchPathPattern } from "./patterns.mjs";
|
|
3
|
+
function canonicaliseLocalisedRoutes(routes) {
|
|
4
|
+
const seen = new Set();
|
|
5
|
+
return routes.flatMap((route)=>{
|
|
6
|
+
const identity = route.modernLocalisedRoute;
|
|
7
|
+
const key = identity?.id ?? identity?.path;
|
|
8
|
+
if (void 0 !== key) {
|
|
9
|
+
if (seen.has(key)) return [];
|
|
10
|
+
seen.add(key);
|
|
11
|
+
}
|
|
12
|
+
return [
|
|
13
|
+
{
|
|
14
|
+
...route,
|
|
15
|
+
...identity ? {
|
|
16
|
+
id: identity.id,
|
|
17
|
+
path: identity.path
|
|
18
|
+
} : {},
|
|
19
|
+
...route.children ? {
|
|
20
|
+
children: canonicaliseLocalisedRoutes(route.children)
|
|
21
|
+
} : {}
|
|
22
|
+
}
|
|
23
|
+
];
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
function rewritePath(url, mappings) {
|
|
27
|
+
for (const { from, to } of mappings){
|
|
28
|
+
const params = matchPathPattern(url.pathname, from);
|
|
29
|
+
if (params) {
|
|
30
|
+
url.pathname = buildPathFromPattern(to, params);
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return url;
|
|
35
|
+
}
|
|
36
|
+
function createLocalisedRouteRewrite(routes) {
|
|
37
|
+
const input = [];
|
|
38
|
+
const output = [];
|
|
39
|
+
const visit = (nodes, parent)=>{
|
|
40
|
+
for (const route of nodes){
|
|
41
|
+
const fullPath = normalisePathPattern(`${parent}/${route.path ?? ''}`);
|
|
42
|
+
const identity = route.modernLocalisedRoute;
|
|
43
|
+
if (identity?.paths && fullPath.endsWith(identity.canonicalPath)) {
|
|
44
|
+
const prefix = fullPath.slice(0, -identity.canonicalPath.length);
|
|
45
|
+
const hasLocale = prefix.split('/').some((segment)=>/^:(?:lang|locale|language)\??$/.test(segment));
|
|
46
|
+
for (const [language, localisedPath] of Object.entries(identity.paths)){
|
|
47
|
+
const localePrefix = prefix.split('/').map((segment)=>/^:(?:lang|locale|language)\??$/.test(segment) ? language : segment).join('/');
|
|
48
|
+
const canonical = normalisePathPattern(`${localePrefix}/${identity.canonicalPath}`);
|
|
49
|
+
const localised = normalisePathPattern(`${localePrefix}/${localisedPath}`);
|
|
50
|
+
if (canonical !== localised) {
|
|
51
|
+
input.push({
|
|
52
|
+
from: localised,
|
|
53
|
+
to: canonical
|
|
54
|
+
});
|
|
55
|
+
if (hasLocale) output.push({
|
|
56
|
+
from: canonical,
|
|
57
|
+
to: localised
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (route.children) visit(route.children, fullPath);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
visit(canonicaliseLocalisedRoutes(routes), '');
|
|
66
|
+
if (0 === input.length) return;
|
|
67
|
+
const specificity = (pattern)=>pattern.split('/').filter((segment)=>segment && !segment.startsWith(':') && '*' !== segment).length;
|
|
68
|
+
for (const mappings of [
|
|
69
|
+
input,
|
|
70
|
+
output
|
|
71
|
+
])mappings.sort((a, b)=>specificity(b.from) - specificity(a.from));
|
|
72
|
+
return {
|
|
73
|
+
input: ({ url })=>rewritePath(url, input),
|
|
74
|
+
output: ({ url })=>rewritePath(url, output)
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
export { canonicaliseLocalisedRoutes, createLocalisedRouteRewrite };
|
|
@@ -101,6 +101,14 @@ const transformLocalisedRoute = (route, parentCanonicalPath, parentLocalisedPath
|
|
|
101
101
|
const children = 'children' in route && route.children ? route.children.flatMap((child)=>transformLocalisedRoute(child, canonicalPath, routeLocalisedPaths, languages, localisedUrls)) : void 0;
|
|
102
102
|
const baseRoute = {
|
|
103
103
|
...route,
|
|
104
|
+
...!isFrameworkInternalRoutePath(canonicalPath) ? {
|
|
105
|
+
modernLocalisedRoute: {
|
|
106
|
+
id: route.id,
|
|
107
|
+
path: route.path,
|
|
108
|
+
canonicalPath,
|
|
109
|
+
paths: localisedUrlEntry
|
|
110
|
+
}
|
|
111
|
+
} : {},
|
|
104
112
|
...children ? {
|
|
105
113
|
children
|
|
106
114
|
} : {}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { splitUrlTarget } from "@modern-js/runtime-utils/url";
|
|
2
|
+
import { canonicalTargetPathname, localiseTargetPathname } from "./localisedUrls/index.mjs";
|
|
3
|
+
const buildLocalizedUrl = (target, language, languages, localisedUrls)=>{
|
|
4
|
+
const { pathname, search, hash } = splitUrlTarget(target);
|
|
5
|
+
return `${localiseTargetPathname(pathname, language, languages, localisedUrls)}${search}${hash}`;
|
|
6
|
+
};
|
|
7
|
+
const localizePath = (target, language, config)=>buildLocalizedUrl(target, language, config.languages, config.localisedUrls);
|
|
8
|
+
const canonicalPath = (target, config)=>{
|
|
9
|
+
const { pathname, search, hash } = splitUrlTarget(target);
|
|
10
|
+
return `${canonicalTargetPathname(pathname, config.languages, config.localisedUrls)}${search}${hash}`;
|
|
11
|
+
};
|
|
12
|
+
export { buildLocalizedUrl, canonicalPath, localizePath };
|
package/dist/esm-node/index.mjs
CHANGED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { isDefaultLocaleRedirectSkipPath, localiseTargetPathname, matchesPathPrefix, shouldSkipLocaleRedirect, stripLanguagePrefix } from "./localisedUrls/index.mjs";
|
|
3
|
+
const stripUrlPathPrefix = (pathname, urlPath)=>{
|
|
4
|
+
const basePath = urlPath.replace('/*', '');
|
|
5
|
+
if (!basePath || '/' === basePath) return pathname;
|
|
6
|
+
const remainingPath = pathname.startsWith(basePath) ? pathname.slice(basePath.length) : pathname;
|
|
7
|
+
return remainingPath || '/';
|
|
8
|
+
};
|
|
9
|
+
const shouldIgnoreRedirect = (pathname, urlPath, ignoreRedirectRoutes)=>{
|
|
10
|
+
const remainingPath = stripUrlPathPrefix(pathname, urlPath);
|
|
11
|
+
return shouldSkipLocaleRedirect(remainingPath, [], ignoreRedirectRoutes);
|
|
12
|
+
};
|
|
13
|
+
const isStaticResourceRequest = (pathname, staticRoutePrefixes, languages = [])=>{
|
|
14
|
+
if (isDefaultLocaleRedirectSkipPath(pathname, languages)) return true;
|
|
15
|
+
const matchesStaticRoutePrefix = (targetPathname)=>staticRoutePrefixes.some((prefix)=>matchesPathPrefix(targetPathname, prefix));
|
|
16
|
+
if (matchesStaticRoutePrefix(pathname)) return true;
|
|
17
|
+
const pathWithoutLanguage = stripLanguagePrefix(pathname, languages);
|
|
18
|
+
return pathWithoutLanguage !== pathname && matchesStaticRoutePrefix(pathWithoutLanguage);
|
|
19
|
+
};
|
|
20
|
+
const getLanguageFromPath = (req, urlPath, languages)=>{
|
|
21
|
+
const url = new URL(req.url, `http://${req.header().host}`);
|
|
22
|
+
const pathname = url.pathname;
|
|
23
|
+
const remainingPath = stripUrlPathPrefix(pathname, urlPath);
|
|
24
|
+
const segments = remainingPath.split('/').filter(Boolean);
|
|
25
|
+
const firstSegment = segments[0];
|
|
26
|
+
if (languages.includes(firstSegment)) return firstSegment;
|
|
27
|
+
return null;
|
|
28
|
+
};
|
|
29
|
+
const buildLocalizedUrl = (req, urlPath, language, languages, localisedUrls)=>{
|
|
30
|
+
const url = new URL(req.url);
|
|
31
|
+
const pathname = url.pathname;
|
|
32
|
+
const basePath = urlPath.replace('/*', '');
|
|
33
|
+
const remainingPath = stripUrlPathPrefix(pathname, urlPath);
|
|
34
|
+
const newPathname = localiseTargetPathname(remainingPath, language, languages, localisedUrls);
|
|
35
|
+
const suffix = `${url.search}${url.hash}`;
|
|
36
|
+
const localizedUrl = '/' === basePath ? newPathname + suffix : basePath + newPathname + suffix;
|
|
37
|
+
return localizedUrl;
|
|
38
|
+
};
|
|
39
|
+
const createLocaleRedirectResponse = (location)=>new Response(null, {
|
|
40
|
+
status: 302,
|
|
41
|
+
headers: {
|
|
42
|
+
'Cache-Control': 'private, no-store',
|
|
43
|
+
Location: location,
|
|
44
|
+
Vary: 'Accept-Language, Cookie'
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
export { buildLocalizedUrl, createLocaleRedirectResponse, getLanguageFromPath, isStaticResourceRequest, shouldIgnoreRedirect };
|
|
@@ -5,4 +5,5 @@ export { canonicalTargetPathname, localiseTargetPathname, stripLanguagePrefix }
|
|
|
5
5
|
export { buildPathFromPattern, matchPathPattern } from "./patterns.mjs";
|
|
6
6
|
export { DEFAULT_LOCALE_REDIRECT_SKIP_RULES, isDefaultLocaleRedirectSkipPath, matchesPathPrefix, shouldSkipLocaleRedirect } from "./redirect.mjs";
|
|
7
7
|
export { resolveCanonicalLocalisedPath, resolveLocalisedPath } from "./resolve.mjs";
|
|
8
|
+
export { canonicaliseLocalisedRoutes, createLocalisedRouteRewrite } from "./routeIdentity.mjs";
|
|
8
9
|
export { applyLocalisedUrlsToRoutes, validateLocalisedUrls } from "./routes.mjs";
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import "node:module";
|
|
2
|
+
import { normalisePathPattern } from "./normalise.mjs";
|
|
3
|
+
import { buildPathFromPattern, matchPathPattern } from "./patterns.mjs";
|
|
4
|
+
function canonicaliseLocalisedRoutes(routes) {
|
|
5
|
+
const seen = new Set();
|
|
6
|
+
return routes.flatMap((route)=>{
|
|
7
|
+
const identity = route.modernLocalisedRoute;
|
|
8
|
+
const key = identity?.id ?? identity?.path;
|
|
9
|
+
if (void 0 !== key) {
|
|
10
|
+
if (seen.has(key)) return [];
|
|
11
|
+
seen.add(key);
|
|
12
|
+
}
|
|
13
|
+
return [
|
|
14
|
+
{
|
|
15
|
+
...route,
|
|
16
|
+
...identity ? {
|
|
17
|
+
id: identity.id,
|
|
18
|
+
path: identity.path
|
|
19
|
+
} : {},
|
|
20
|
+
...route.children ? {
|
|
21
|
+
children: canonicaliseLocalisedRoutes(route.children)
|
|
22
|
+
} : {}
|
|
23
|
+
}
|
|
24
|
+
];
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
function rewritePath(url, mappings) {
|
|
28
|
+
for (const { from, to } of mappings){
|
|
29
|
+
const params = matchPathPattern(url.pathname, from);
|
|
30
|
+
if (params) {
|
|
31
|
+
url.pathname = buildPathFromPattern(to, params);
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return url;
|
|
36
|
+
}
|
|
37
|
+
function createLocalisedRouteRewrite(routes) {
|
|
38
|
+
const input = [];
|
|
39
|
+
const output = [];
|
|
40
|
+
const visit = (nodes, parent)=>{
|
|
41
|
+
for (const route of nodes){
|
|
42
|
+
const fullPath = normalisePathPattern(`${parent}/${route.path ?? ''}`);
|
|
43
|
+
const identity = route.modernLocalisedRoute;
|
|
44
|
+
if (identity?.paths && fullPath.endsWith(identity.canonicalPath)) {
|
|
45
|
+
const prefix = fullPath.slice(0, -identity.canonicalPath.length);
|
|
46
|
+
const hasLocale = prefix.split('/').some((segment)=>/^:(?:lang|locale|language)\??$/.test(segment));
|
|
47
|
+
for (const [language, localisedPath] of Object.entries(identity.paths)){
|
|
48
|
+
const localePrefix = prefix.split('/').map((segment)=>/^:(?:lang|locale|language)\??$/.test(segment) ? language : segment).join('/');
|
|
49
|
+
const canonical = normalisePathPattern(`${localePrefix}/${identity.canonicalPath}`);
|
|
50
|
+
const localised = normalisePathPattern(`${localePrefix}/${localisedPath}`);
|
|
51
|
+
if (canonical !== localised) {
|
|
52
|
+
input.push({
|
|
53
|
+
from: localised,
|
|
54
|
+
to: canonical
|
|
55
|
+
});
|
|
56
|
+
if (hasLocale) output.push({
|
|
57
|
+
from: canonical,
|
|
58
|
+
to: localised
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (route.children) visit(route.children, fullPath);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
visit(canonicaliseLocalisedRoutes(routes), '');
|
|
67
|
+
if (0 === input.length) return;
|
|
68
|
+
const specificity = (pattern)=>pattern.split('/').filter((segment)=>segment && !segment.startsWith(':') && '*' !== segment).length;
|
|
69
|
+
for (const mappings of [
|
|
70
|
+
input,
|
|
71
|
+
output
|
|
72
|
+
])mappings.sort((a, b)=>specificity(b.from) - specificity(a.from));
|
|
73
|
+
return {
|
|
74
|
+
input: ({ url })=>rewritePath(url, input),
|
|
75
|
+
output: ({ url })=>rewritePath(url, output)
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
export { canonicaliseLocalisedRoutes, createLocalisedRouteRewrite };
|