@inlang/paraglide-js 2.25.4 → 2.26.0
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/compiler/runtime/create-runtime.js +24 -1
- package/dist/compiler/runtime/create-runtime.js.map +1 -1
- package/dist/compiler/runtime/extract-locale-from-url.js +11 -4
- package/dist/compiler/runtime/extract-locale-from-url.js.map +1 -1
- package/dist/compiler/runtime/generate-static-localized-urls.js +5 -3
- package/dist/compiler/runtime/generate-static-localized-urls.js.map +1 -1
- package/dist/compiler/runtime/localize-url.js +25 -54
- package/dist/compiler/runtime/localize-url.js.map +1 -1
- package/dist/compiler/runtime/route-strategy.js +9 -3
- package/dist/compiler/runtime/route-strategy.js.map +1 -1
- package/dist/compiler/runtime/type.test.js +9 -0
- package/dist/compiler/runtime/type.test.js.map +1 -1
- package/dist/compiler/runtime/url-pattern-cache.d.ts +11 -0
- package/dist/compiler/runtime/url-pattern-cache.js +42 -0
- package/dist/compiler/runtime/url-pattern-cache.js.map +1 -0
- package/dist/compiler/runtime/url-pattern-index.d.ts +24 -0
- package/dist/compiler/runtime/url-pattern-index.differential.test.d.ts +1 -0
- package/dist/compiler/runtime/url-pattern-index.differential.test.js +152 -0
- package/dist/compiler/runtime/url-pattern-index.differential.test.js.map +1 -0
- package/dist/compiler/runtime/url-pattern-index.js +168 -0
- package/dist/compiler/runtime/url-pattern-index.js.map +1 -0
- package/dist/compiler/runtime/url-pattern-index.test.d.ts +1 -0
- package/dist/compiler/runtime/url-pattern-index.test.js +262 -0
- package/dist/compiler/runtime/url-pattern-index.test.js.map +1 -0
- package/dist/compiler/server/create-server-file.js +1 -1
- package/dist/compiler/server/create-server-file.js.map +1 -1
- package/dist/compiler/server/create-server-file.test.js +12 -0
- package/dist/compiler/server/create-server-file.test.js.map +1 -1
- package/dist/services/env-variables/index.js +1 -1
- package/dist/services/env-variables/index.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { urlPatterns, routeStrategies } from "./variables.js";
|
|
2
|
+
/** @typedef {{ children: Map<string, PrefixNode>, ranks: Set<number> }} PrefixNode */
|
|
3
|
+
/** @typedef {{ items: any[], snapshots: any[], root: PrefixNode }} PatternIndex */
|
|
4
|
+
/** @type {PatternIndex | undefined} */
|
|
5
|
+
let urlPatternIndex;
|
|
6
|
+
/** @type {PatternIndex | undefined} */
|
|
7
|
+
let routeStrategyIndex;
|
|
8
|
+
let routingConfigVersion = 0;
|
|
9
|
+
/**
|
|
10
|
+
* Validate the public mutable configuration before reusing derived routing data.
|
|
11
|
+
* String/reference comparisons intentionally remain O(n): they preserve edits
|
|
12
|
+
* through retained array/object references without changing the public objects.
|
|
13
|
+
* Native matching of unrelated routes is the expensive work this index avoids.
|
|
14
|
+
* @returns {number}
|
|
15
|
+
*/
|
|
16
|
+
export function getRoutingConfigVersion() {
|
|
17
|
+
if (!indexIsCurrent(urlPatternIndex, urlPatterns, false)) {
|
|
18
|
+
urlPatternIndex = buildPatternIndex(urlPatterns, false);
|
|
19
|
+
routingConfigVersion++;
|
|
20
|
+
}
|
|
21
|
+
if (!indexIsCurrent(routeStrategyIndex, routeStrategies, true)) {
|
|
22
|
+
routeStrategyIndex = buildPatternIndex(routeStrategies, true);
|
|
23
|
+
routingConfigVersion++;
|
|
24
|
+
}
|
|
25
|
+
return routingConfigVersion;
|
|
26
|
+
}
|
|
27
|
+
/** @returns {number} */
|
|
28
|
+
export function currentRoutingConfigVersion() {
|
|
29
|
+
return routingConfigVersion;
|
|
30
|
+
}
|
|
31
|
+
/** @param {URL} url @returns {typeof urlPatterns} */
|
|
32
|
+
export function getUrlPatternCandidates(url) {
|
|
33
|
+
getRoutingConfigVersion();
|
|
34
|
+
return patternCandidates(urlPatternIndex, url, urlPatterns);
|
|
35
|
+
}
|
|
36
|
+
/** @param {URL} url @returns {typeof routeStrategies} */
|
|
37
|
+
export function getRouteStrategyCandidates(url) {
|
|
38
|
+
getRoutingConfigVersion();
|
|
39
|
+
return patternCandidates(routeStrategyIndex, url, routeStrategies);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Only a complete, conservative root-relative grammar is eligible. Unknown
|
|
43
|
+
* syntax stays unconditional, including invalid patterns whose constructor
|
|
44
|
+
* errors must remain observable. Stop indexing at the first dynamic segment.
|
|
45
|
+
* @param {string} pattern
|
|
46
|
+
* @returns {string[]}
|
|
47
|
+
*/
|
|
48
|
+
function mandatoryPathSegments(pattern) {
|
|
49
|
+
if (typeof pattern !== "string" ||
|
|
50
|
+
!pattern.startsWith("/") ||
|
|
51
|
+
pattern.startsWith("//"))
|
|
52
|
+
return [];
|
|
53
|
+
const segments = pattern.slice(1).split("/");
|
|
54
|
+
const names = new Set();
|
|
55
|
+
const literal = /^[A-Za-z0-9_-]+$/;
|
|
56
|
+
const parameter = /^:([A-Za-z_][A-Za-z0-9_]*)(?:[?*+]|\(\.\*\)\??)?$/;
|
|
57
|
+
for (let i = 0; i < segments.length; i++) {
|
|
58
|
+
const segment = segments[i] ?? "";
|
|
59
|
+
if (segment === "" && i === segments.length - 1)
|
|
60
|
+
continue;
|
|
61
|
+
if (literal.test(segment))
|
|
62
|
+
continue;
|
|
63
|
+
const match = parameter.exec(segment);
|
|
64
|
+
if (!match || names.has(match[1]))
|
|
65
|
+
return [];
|
|
66
|
+
names.add(match[1]);
|
|
67
|
+
}
|
|
68
|
+
const prefix = [];
|
|
69
|
+
for (const segment of segments) {
|
|
70
|
+
if (!literal.test(segment))
|
|
71
|
+
break;
|
|
72
|
+
prefix.push(segment);
|
|
73
|
+
}
|
|
74
|
+
return prefix;
|
|
75
|
+
}
|
|
76
|
+
/** @param {PatternIndex | undefined} index @param {any[]} items @param {boolean} policies */
|
|
77
|
+
function indexIsCurrent(index, items, policies) {
|
|
78
|
+
if (!index ||
|
|
79
|
+
index.items !== items ||
|
|
80
|
+
index.snapshots.length !== items.length)
|
|
81
|
+
return false;
|
|
82
|
+
for (let i = 0; i < items.length; i++) {
|
|
83
|
+
const item = items[i];
|
|
84
|
+
const saved = index.snapshots[i];
|
|
85
|
+
if (item !== saved.item ||
|
|
86
|
+
(policies ? item?.match : item?.pattern) !== saved.pattern)
|
|
87
|
+
return false;
|
|
88
|
+
if (policies)
|
|
89
|
+
continue;
|
|
90
|
+
const localized = item?.localized;
|
|
91
|
+
if (!Array.isArray(localized) ||
|
|
92
|
+
saved.localized !== localized ||
|
|
93
|
+
saved.entries.length !== localized.length)
|
|
94
|
+
return false;
|
|
95
|
+
for (let j = 0; j < localized.length; j++) {
|
|
96
|
+
const entry = localized[j];
|
|
97
|
+
const previous = saved.entries[j];
|
|
98
|
+
if (entry !== previous.entry ||
|
|
99
|
+
entry?.[0] !== previous.locale ||
|
|
100
|
+
entry?.[1] !== previous.pattern)
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
/** @param {any[]} items @param {boolean} policies @returns {PatternIndex} */
|
|
107
|
+
function buildPatternIndex(items, policies) {
|
|
108
|
+
/** @type {PrefixNode} */
|
|
109
|
+
const root = { children: new Map(), ranks: new Set() };
|
|
110
|
+
const snapshots = [];
|
|
111
|
+
for (let rank = 0; rank < items.length; rank++) {
|
|
112
|
+
const item = items[rank];
|
|
113
|
+
const pattern = policies ? item?.match : item?.pattern;
|
|
114
|
+
const localized = policies ? undefined : item?.localized;
|
|
115
|
+
const entries = Array.isArray(localized)
|
|
116
|
+
? Array.from(localized, (entry) => ({
|
|
117
|
+
entry,
|
|
118
|
+
locale: entry?.[0],
|
|
119
|
+
pattern: entry?.[1],
|
|
120
|
+
}))
|
|
121
|
+
: [];
|
|
122
|
+
snapshots.push({ item, pattern, localized, entries });
|
|
123
|
+
const paths = [pattern, ...entries.map((entry) => entry.pattern)].map(mandatoryPathSegments);
|
|
124
|
+
// A malformed element must also remain in the original native scan.
|
|
125
|
+
if ((!policies &&
|
|
126
|
+
(!Array.isArray(localized) ||
|
|
127
|
+
entries.some(({ entry }) => !Array.isArray(entry)))) ||
|
|
128
|
+
paths.some((path) => path.length === 0)) {
|
|
129
|
+
root.ranks.add(rank);
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
for (const path of paths) {
|
|
133
|
+
let node = root;
|
|
134
|
+
for (const segment of path) {
|
|
135
|
+
let child = node.children.get(segment);
|
|
136
|
+
if (!child) {
|
|
137
|
+
child = { children: new Map(), ranks: new Set() };
|
|
138
|
+
node.children.set(segment, child);
|
|
139
|
+
}
|
|
140
|
+
node = child;
|
|
141
|
+
}
|
|
142
|
+
node.ranks.add(rank);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return { items, snapshots, root };
|
|
146
|
+
}
|
|
147
|
+
/** @param {PatternIndex | undefined} index @param {URL} url @param {any[]} items @returns {any[]} */
|
|
148
|
+
function patternCandidates(index, url, items) {
|
|
149
|
+
// Root-relative constructor/base behavior for other schemes is deliberately
|
|
150
|
+
// left to URLPattern, as are configurations with no prunable prefixes.
|
|
151
|
+
if (!index ||
|
|
152
|
+
!/^https?:$/.test(url.protocol) ||
|
|
153
|
+
index.root.ranks.size === items.length)
|
|
154
|
+
return items;
|
|
155
|
+
const ranks = new Set(index.root.ranks);
|
|
156
|
+
let node = index.root;
|
|
157
|
+
for (const segment of url.pathname.slice(1).split("/")) {
|
|
158
|
+
const child = node.children.get(segment);
|
|
159
|
+
if (!child)
|
|
160
|
+
break;
|
|
161
|
+
node = child;
|
|
162
|
+
for (const rank of node.ranks)
|
|
163
|
+
ranks.add(rank);
|
|
164
|
+
}
|
|
165
|
+
// Specificity must never change precedence, including unconditional patterns.
|
|
166
|
+
return [...ranks].sort((a, b) => a - b).map((rank) => items[rank]);
|
|
167
|
+
}
|
|
168
|
+
//# sourceMappingURL=url-pattern-index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"url-pattern-index.js","sourceRoot":"","sources":["../../../src/compiler/runtime/url-pattern-index.js"],"names":[],"mappings":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AC;IACd,MAAM,KAAK,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACxC,IAAI,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC;IACtB,KAAK,MAAM,OAAO,IAAI,GAAG,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;QACxD,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QACzC,IAAI,CAAC,KAAK;YAAE,MAAM;QAClB,IAAI,GAAG,KAAK,CAAC;QACb,KAAK,MAAM,IAAI,IAAI,IAAI,CAAC,KAAK;YAAE,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAChD,CAAC;IACD,8EAA8E;IAC9E,OAAO,CAAC,GAAG,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;AACpE,CAAC","sourcesContent":["import { urlPatterns, routeStrategies } from \"./variables.js\";\n\n/** @typedef {{ children: Map<string, PrefixNode>, ranks: Set<number> }} PrefixNode */\n/** @typedef {{ items: any[], snapshots: any[], root: PrefixNode }} PatternIndex */\n\n/** @type {PatternIndex | undefined} */\nlet urlPatternIndex;\n/** @type {PatternIndex | undefined} */\nlet routeStrategyIndex;\nlet routingConfigVersion = 0;\n\n/**\n * Validate the public mutable configuration before reusing derived routing data.\n * String/reference comparisons intentionally remain O(n): they preserve edits\n * through retained array/object references without changing the public objects.\n * Native matching of unrelated routes is the expensive work this index avoids.\n * @returns {number}\n */\nexport function getRoutingConfigVersion() {\n\tif (!indexIsCurrent(urlPatternIndex, urlPatterns, false)) {\n\t\turlPatternIndex = buildPatternIndex(urlPatterns, false);\n\t\troutingConfigVersion++;\n\t}\n\tif (!indexIsCurrent(routeStrategyIndex, routeStrategies, true)) {\n\t\trouteStrategyIndex = buildPatternIndex(routeStrategies, true);\n\t\troutingConfigVersion++;\n\t}\n\treturn routingConfigVersion;\n}\n\n/** @returns {number} */\nexport function currentRoutingConfigVersion() {\n\treturn routingConfigVersion;\n}\n\n/** @param {URL} url @returns {typeof urlPatterns} */\nexport function getUrlPatternCandidates(url) {\n\tgetRoutingConfigVersion();\n\treturn patternCandidates(urlPatternIndex, url, urlPatterns);\n}\n\n/** @param {URL} url @returns {typeof routeStrategies} */\nexport function getRouteStrategyCandidates(url) {\n\tgetRoutingConfigVersion();\n\treturn patternCandidates(routeStrategyIndex, url, routeStrategies);\n}\n\n/**\n * Only a complete, conservative root-relative grammar is eligible. Unknown\n * syntax stays unconditional, including invalid patterns whose constructor\n * errors must remain observable. Stop indexing at the first dynamic segment.\n * @param {string} pattern\n * @returns {string[]}\n */\nfunction mandatoryPathSegments(pattern) {\n\tif (\n\t\ttypeof pattern !== \"string\" ||\n\t\t!pattern.startsWith(\"/\") ||\n\t\tpattern.startsWith(\"//\")\n\t)\n\t\treturn [];\n\tconst segments = pattern.slice(1).split(\"/\");\n\tconst names = new Set();\n\tconst literal = /^[A-Za-z0-9_-]+$/;\n\tconst parameter = /^:([A-Za-z_][A-Za-z0-9_]*)(?:[?*+]|\\(\\.\\*\\)\\??)?$/;\n\tfor (let i = 0; i < segments.length; i++) {\n\t\tconst segment = segments[i] ?? \"\";\n\t\tif (segment === \"\" && i === segments.length - 1) continue;\n\t\tif (literal.test(segment)) continue;\n\t\tconst match = parameter.exec(segment);\n\t\tif (!match || names.has(match[1])) return [];\n\t\tnames.add(match[1]);\n\t}\n\tconst prefix = [];\n\tfor (const segment of segments) {\n\t\tif (!literal.test(segment)) break;\n\t\tprefix.push(segment);\n\t}\n\treturn prefix;\n}\n\n/** @param {PatternIndex | undefined} index @param {any[]} items @param {boolean} policies */\nfunction indexIsCurrent(index, items, policies) {\n\tif (\n\t\t!index ||\n\t\tindex.items !== items ||\n\t\tindex.snapshots.length !== items.length\n\t)\n\t\treturn false;\n\tfor (let i = 0; i < items.length; i++) {\n\t\tconst item = items[i];\n\t\tconst saved = index.snapshots[i];\n\t\tif (\n\t\t\titem !== saved.item ||\n\t\t\t(policies ? item?.match : item?.pattern) !== saved.pattern\n\t\t)\n\t\t\treturn false;\n\t\tif (policies) continue;\n\t\tconst localized = item?.localized;\n\t\tif (\n\t\t\t!Array.isArray(localized) ||\n\t\t\tsaved.localized !== localized ||\n\t\t\tsaved.entries.length !== localized.length\n\t\t)\n\t\t\treturn false;\n\t\tfor (let j = 0; j < localized.length; j++) {\n\t\t\tconst entry = localized[j];\n\t\t\tconst previous = saved.entries[j];\n\t\t\tif (\n\t\t\t\tentry !== previous.entry ||\n\t\t\t\tentry?.[0] !== previous.locale ||\n\t\t\t\tentry?.[1] !== previous.pattern\n\t\t\t)\n\t\t\t\treturn false;\n\t\t}\n\t}\n\treturn true;\n}\n\n/** @param {any[]} items @param {boolean} policies @returns {PatternIndex} */\nfunction buildPatternIndex(items, policies) {\n\t/** @type {PrefixNode} */\n\tconst root = { children: new Map(), ranks: new Set() };\n\tconst snapshots = [];\n\tfor (let rank = 0; rank < items.length; rank++) {\n\t\tconst item = items[rank];\n\t\tconst pattern = policies ? item?.match : item?.pattern;\n\t\tconst localized = policies ? undefined : item?.localized;\n\t\tconst entries = Array.isArray(localized)\n\t\t\t? Array.from(localized, (entry) => ({\n\t\t\t\t\tentry,\n\t\t\t\t\tlocale: entry?.[0],\n\t\t\t\t\tpattern: entry?.[1],\n\t\t\t\t}))\n\t\t\t: [];\n\t\tsnapshots.push({ item, pattern, localized, entries });\n\t\tconst paths = [pattern, ...entries.map((entry) => entry.pattern)].map(\n\t\t\tmandatoryPathSegments\n\t\t);\n\t\t// A malformed element must also remain in the original native scan.\n\t\tif (\n\t\t\t(!policies &&\n\t\t\t\t(!Array.isArray(localized) ||\n\t\t\t\t\tentries.some(({ entry }) => !Array.isArray(entry)))) ||\n\t\t\tpaths.some((path) => path.length === 0)\n\t\t) {\n\t\t\troot.ranks.add(rank);\n\t\t\tcontinue;\n\t\t}\n\t\tfor (const path of paths) {\n\t\t\tlet node = root;\n\t\t\tfor (const segment of path) {\n\t\t\t\tlet child = node.children.get(segment);\n\t\t\t\tif (!child) {\n\t\t\t\t\tchild = { children: new Map(), ranks: new Set() };\n\t\t\t\t\tnode.children.set(segment, child);\n\t\t\t\t}\n\t\t\t\tnode = child;\n\t\t\t}\n\t\t\tnode.ranks.add(rank);\n\t\t}\n\t}\n\treturn { items, snapshots, root };\n}\n\n/** @param {PatternIndex | undefined} index @param {URL} url @param {any[]} items @returns {any[]} */\nfunction patternCandidates(index, url, items) {\n\t// Root-relative constructor/base behavior for other schemes is deliberately\n\t// left to URLPattern, as are configurations with no prunable prefixes.\n\tif (\n\t\t!index ||\n\t\t!/^https?:$/.test(url.protocol) ||\n\t\tindex.root.ranks.size === items.length\n\t)\n\t\treturn items;\n\tconst ranks = new Set(index.root.ranks);\n\tlet node = index.root;\n\tfor (const segment of url.pathname.slice(1).split(\"/\")) {\n\t\tconst child = node.children.get(segment);\n\t\tif (!child) break;\n\t\tnode = child;\n\t\tfor (const rank of node.ranks) ranks.add(rank);\n\t}\n\t// Specificity must never change precedence, including unconditional patterns.\n\treturn [...ranks].sort((a, b) => a - b).map((rank) => items[rank]);\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { expect, test } from "vitest";
|
|
2
|
+
import { URLPattern as PolyfillURLPattern } from "urlpattern-polyfill";
|
|
3
|
+
import { createParaglide } from "../create-paraglide.js";
|
|
4
|
+
import { newProject } from "@inlang/sdk";
|
|
5
|
+
const nativeURLPattern = Reflect.get(globalThis, "URLPattern");
|
|
6
|
+
const engines = [];
|
|
7
|
+
let runtimeModuleNonce = 0;
|
|
8
|
+
if (typeof nativeURLPattern === "function" &&
|
|
9
|
+
nativeURLPattern !== PolyfillURLPattern) {
|
|
10
|
+
engines.push(["native", nativeURLPattern]);
|
|
11
|
+
}
|
|
12
|
+
engines.push(["polyfill", PolyfillURLPattern]);
|
|
13
|
+
async function withURLPattern(ctor, fn) {
|
|
14
|
+
const descriptor = Object.getOwnPropertyDescriptor(globalThis, "URLPattern");
|
|
15
|
+
Object.defineProperty(globalThis, "URLPattern", {
|
|
16
|
+
configurable: true,
|
|
17
|
+
writable: true,
|
|
18
|
+
value: ctor,
|
|
19
|
+
});
|
|
20
|
+
try {
|
|
21
|
+
return await fn();
|
|
22
|
+
}
|
|
23
|
+
finally {
|
|
24
|
+
if (descriptor) {
|
|
25
|
+
Object.defineProperty(globalThis, "URLPattern", descriptor);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
Reflect.deleteProperty(globalThis, "URLPattern");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
async function withRuntime(options, ctor, fn) {
|
|
33
|
+
return withURLPattern(ctor, async () => fn(await createParaglide({
|
|
34
|
+
blob: await newProject({
|
|
35
|
+
settings: { baseLocale: "en", locales: ["en", "de"] },
|
|
36
|
+
}),
|
|
37
|
+
cookieName: `pattern-index-test-${runtimeModuleNonce++}`,
|
|
38
|
+
strategy: ["url", "baseLocale"],
|
|
39
|
+
...options,
|
|
40
|
+
})));
|
|
41
|
+
}
|
|
42
|
+
for (const [engineName, ctor] of engines) {
|
|
43
|
+
test(`${engineName} URLPattern keeps ordered routing and URL parts`, async () => {
|
|
44
|
+
await withRuntime({
|
|
45
|
+
urlPatterns: [
|
|
46
|
+
{
|
|
47
|
+
pattern: "/specific-path",
|
|
48
|
+
localized: [
|
|
49
|
+
["en", "/specific-path"],
|
|
50
|
+
["de", "/de/404"],
|
|
51
|
+
],
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
pattern: "/:path(.*)?",
|
|
55
|
+
localized: [
|
|
56
|
+
["en", "/:path(.*)?"],
|
|
57
|
+
["de", "/de/:path(.*)?"],
|
|
58
|
+
],
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
routeStrategies: [
|
|
62
|
+
{ match: "/specific-path", exclude: true },
|
|
63
|
+
{ match: "/:path(.*)?", strategy: ["url"] },
|
|
64
|
+
],
|
|
65
|
+
}, ctor, async (runtime) => {
|
|
66
|
+
const input = "https://alice:secret@example.com/specific-path?redirect=%2Faccount#section";
|
|
67
|
+
expect(runtime.localizeUrl(input, { locale: "de" }).href).toBe("https://alice:secret@example.com/de/404?redirect=%2Faccount#section");
|
|
68
|
+
expect(runtime.deLocalizeUrl("https://alice:secret@example.com/de/404?redirect=%2Faccount#section").href).toBe("https://alice:secret@example.com/specific-path?redirect=%2Faccount#section");
|
|
69
|
+
expect(runtime.extractLocaleFromUrl(input)).toBe("en");
|
|
70
|
+
expect(runtime.isExcludedByRouteStrategy("https://example.com/specific-path")).toBe(true);
|
|
71
|
+
expect(runtime.getStrategyForUrl("https://example.com/other")).toEqual([
|
|
72
|
+
"url",
|
|
73
|
+
]);
|
|
74
|
+
expect(runtime
|
|
75
|
+
.generateStaticLocalizedUrls(["https://example.com/specific-path"])
|
|
76
|
+
.map((url) => url.href)).toEqual([
|
|
77
|
+
"https://example.com/specific-path",
|
|
78
|
+
"https://example.com/de/404",
|
|
79
|
+
]);
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
test(`${engineName} URLPattern refreshes ordered indexes and same-URL caches after edits`, async () => {
|
|
83
|
+
await withRuntime({
|
|
84
|
+
urlPatterns: [
|
|
85
|
+
{
|
|
86
|
+
pattern: "/:path(.*)?",
|
|
87
|
+
localized: [
|
|
88
|
+
["en", "/:path(.*)?"],
|
|
89
|
+
["de", "/de/:path(.*)?"],
|
|
90
|
+
],
|
|
91
|
+
},
|
|
92
|
+
],
|
|
93
|
+
routeStrategies: [{ match: "/old/:slug", exclude: true }],
|
|
94
|
+
}, ctor, async (runtime) => {
|
|
95
|
+
const url = "https://example.com/old/story";
|
|
96
|
+
expect(runtime.extractLocaleFromUrl(url)).toBe("en");
|
|
97
|
+
expect(runtime.localizeUrl(url, { locale: "de" }).pathname).toBe("/de/old/story");
|
|
98
|
+
expect(runtime.isExcludedByRouteStrategy(url)).toBe(true);
|
|
99
|
+
const pattern = runtime.urlPatterns[0];
|
|
100
|
+
pattern.pattern = "/new/:path(.*)?";
|
|
101
|
+
pattern.localized[0][1] = "/new/:path(.*)?";
|
|
102
|
+
pattern.localized[1][1] = "/de/new/:path(.*)?";
|
|
103
|
+
runtime.routeStrategies[0].match = "/new/:slug";
|
|
104
|
+
// Reuse the exact href so both one-entry result caches must notice
|
|
105
|
+
// the config version change and the fast-path parse must be refreshed.
|
|
106
|
+
expect(runtime.extractLocaleFromUrl(url)).toBe(undefined);
|
|
107
|
+
expect(runtime.localizeUrl(url, { locale: "de" }).href).toBe(url);
|
|
108
|
+
expect(runtime.isExcludedByRouteStrategy(url)).toBe(false);
|
|
109
|
+
expect(runtime.localizeUrl("https://example.com/new/story", { locale: "de" })
|
|
110
|
+
.pathname).toBe("/de/new/story");
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
test(`${engineName} URLPattern refreshes warmed winners after collection and reference edits`, async () => {
|
|
114
|
+
await withRuntime({
|
|
115
|
+
urlPatterns: [
|
|
116
|
+
{
|
|
117
|
+
pattern: "/same/:slug",
|
|
118
|
+
localized: [["de", "/same/:slug"]],
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
routeStrategies: [{ match: "/same/:slug", strategy: ["url"] }],
|
|
122
|
+
}, ctor, async (runtime) => {
|
|
123
|
+
const url = "https://example.com/same/story";
|
|
124
|
+
expect(runtime.extractLocaleFromUrl(url)).toBe("de");
|
|
125
|
+
expect(runtime.getStrategyForUrl(url)).toEqual(["url"]);
|
|
126
|
+
// A pushed earlier pattern changes the extraction winner. Splicing it
|
|
127
|
+
// back out must restore the original winner on this same cached URL.
|
|
128
|
+
runtime.urlPatterns.unshift({
|
|
129
|
+
pattern: "/same/:slug",
|
|
130
|
+
localized: [["en", "/same/:slug"]],
|
|
131
|
+
});
|
|
132
|
+
expect(runtime.extractLocaleFromUrl(url)).toBe("en");
|
|
133
|
+
runtime.urlPatterns.splice(0, 1);
|
|
134
|
+
expect(runtime.extractLocaleFromUrl(url)).toBe("de");
|
|
135
|
+
// Replacing the localized array and its locale string changes the
|
|
136
|
+
// first matching locale even though its pattern text is unchanged.
|
|
137
|
+
const entry = runtime.urlPatterns[0];
|
|
138
|
+
entry.localized = [["en", "/same/:slug"]];
|
|
139
|
+
expect(runtime.extractLocaleFromUrl(url)).toBe("en");
|
|
140
|
+
// Replacing a strategy object with the same match string must refresh
|
|
141
|
+
// the warmed rule result as well as the candidate index snapshot.
|
|
142
|
+
runtime.routeStrategies[0] = {
|
|
143
|
+
match: "/same/:slug",
|
|
144
|
+
strategy: ["baseLocale"],
|
|
145
|
+
};
|
|
146
|
+
expect(runtime.getStrategyForUrl(url)).toEqual(["baseLocale"]);
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
test(`${engineName} URLPattern keeps invalid-pattern errors ahead of later matches`, async () => {
|
|
150
|
+
await withRuntime({
|
|
151
|
+
urlPatterns: [
|
|
152
|
+
{ pattern: "/broken/:id(", localized: [] },
|
|
153
|
+
{
|
|
154
|
+
pattern: "/valid/:id",
|
|
155
|
+
localized: [
|
|
156
|
+
["en", "/valid/:id"],
|
|
157
|
+
["de", "/de/valid/:id"],
|
|
158
|
+
],
|
|
159
|
+
},
|
|
160
|
+
],
|
|
161
|
+
}, ctor, async (runtime) => {
|
|
162
|
+
expect(() => runtime.localizeUrl("https://example.com/valid/7", { locale: "de" })).toThrow();
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
test(`${engineName} URLPattern keeps malformed localized entries unconditional`, async () => {
|
|
166
|
+
await withRuntime({ urlPatterns: [] }, ctor, async (runtime) => {
|
|
167
|
+
runtime.urlPatterns.push({
|
|
168
|
+
pattern: "/unrelated/:slug",
|
|
169
|
+
localized: [null],
|
|
170
|
+
});
|
|
171
|
+
// This route cannot be pruned by its path prefix: the original generic
|
|
172
|
+
// scan would encounter the malformed localized entry and throw.
|
|
173
|
+
expect(() => runtime.localizeUrl("https://example.com/elsewhere", { locale: "de" })).toThrow();
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
test(`${engineName} URLPattern preserves trailing-slash alias captures`, async () => {
|
|
177
|
+
await withRuntime({
|
|
178
|
+
trailingSlash: "always",
|
|
179
|
+
urlPatterns: [
|
|
180
|
+
{
|
|
181
|
+
pattern: "/:path(.*)",
|
|
182
|
+
localized: [
|
|
183
|
+
["en", "/:path(.*)"],
|
|
184
|
+
["de", "/de/:path(.*)/details"],
|
|
185
|
+
],
|
|
186
|
+
},
|
|
187
|
+
],
|
|
188
|
+
}, ctor, async (runtime) => {
|
|
189
|
+
expect(runtime.localizeUrl("https://example.com/foo", { locale: "de" }).href).toBe("https://example.com/de/foo/details/");
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
test(`${engineName} URLPattern keeps file-host bases separate`, async () => {
|
|
193
|
+
await withRuntime({
|
|
194
|
+
urlPatterns: [
|
|
195
|
+
{
|
|
196
|
+
pattern: "/section/:slug",
|
|
197
|
+
localized: [
|
|
198
|
+
["en", "/section/:slug"],
|
|
199
|
+
["de", "/de/section/:slug"],
|
|
200
|
+
],
|
|
201
|
+
},
|
|
202
|
+
],
|
|
203
|
+
routeStrategies: [{ match: "/section/:slug", exclude: true }],
|
|
204
|
+
}, ctor, async (runtime) => {
|
|
205
|
+
const first = new URL("file://server-one/section/story");
|
|
206
|
+
const second = new URL("file://server-two/section/story");
|
|
207
|
+
expect(runtime.extractLocaleFromUrl(first)).toBe("en");
|
|
208
|
+
expect(runtime.extractLocaleFromUrl(second)).toBe("en");
|
|
209
|
+
// Policy matching uses url.origin ("null" for both file hosts) as a
|
|
210
|
+
// URLPattern base and therefore retains the preexisting Invalid URL
|
|
211
|
+
// error. The locale extraction above exercises cache isolation safely.
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
for (const [engineName, ctor] of engines) {
|
|
216
|
+
test(`${engineName} URLPattern avoids unrelated late-route scans across APIs`, async () => {
|
|
217
|
+
let constructions = 0;
|
|
218
|
+
let executions = 0;
|
|
219
|
+
class CountingURLPattern extends ctor {
|
|
220
|
+
constructor(pattern, baseURL) {
|
|
221
|
+
if (baseURL === undefined)
|
|
222
|
+
super(pattern);
|
|
223
|
+
else
|
|
224
|
+
super(pattern, String(baseURL));
|
|
225
|
+
constructions++;
|
|
226
|
+
}
|
|
227
|
+
exec(input, baseURL) {
|
|
228
|
+
executions++;
|
|
229
|
+
return baseURL === undefined
|
|
230
|
+
? super.exec(input)
|
|
231
|
+
: super.exec(input, baseURL);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
const urlPatterns = Array.from({ length: 67 }, (_, index) => ({
|
|
235
|
+
pattern: `/route${index}/:slug`,
|
|
236
|
+
localized: [
|
|
237
|
+
["en", `/route${index}/:slug`],
|
|
238
|
+
["de", `/de/route${index}/:slug`],
|
|
239
|
+
],
|
|
240
|
+
}));
|
|
241
|
+
const routeStrategies = Array.from({ length: 67 }, (_, index) => ({
|
|
242
|
+
match: `/route${index}/:slug`,
|
|
243
|
+
exclude: true,
|
|
244
|
+
}));
|
|
245
|
+
await withRuntime({ urlPatterns, routeStrategies }, CountingURLPattern, async (runtime) => {
|
|
246
|
+
expect(runtime.localizeUrl("https://example.com/route66/story", {
|
|
247
|
+
locale: "de",
|
|
248
|
+
}).pathname).toBe("/de/route66/story");
|
|
249
|
+
expect(runtime.deLocalizeUrl("https://example.com/de/route66/story").pathname).toBe("/route66/story");
|
|
250
|
+
expect(runtime.extractLocaleFromUrl("https://example.com/de/route66/story")).toBe("de");
|
|
251
|
+
expect(runtime.isExcludedByRouteStrategy("https://example.com/route66/story")).toBe(true);
|
|
252
|
+
expect(runtime
|
|
253
|
+
.generateStaticLocalizedUrls(["https://example.com/route66/story"])
|
|
254
|
+
.map((url) => url.pathname)).toEqual(["/route66/story", "/de/route66/story"]);
|
|
255
|
+
// Includes createParaglide's capability probe. URLPattern work across
|
|
256
|
+
// five APIs stays proportional to the few matching patterns, not 67.
|
|
257
|
+
expect(constructions).toBeLessThanOrEqual(4);
|
|
258
|
+
expect(executions).toBeLessThanOrEqual(20);
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
//# sourceMappingURL=url-pattern-index.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"url-pattern-index.test.js","sourceRoot":"","sources":["../../../src/compiler/runtime/url-pattern-index.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,UAAU,IAAI,kBAAkB,EAAE,MAAM,qBAAqB,CAAC;AACvE,OAAO,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEzD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAIzC,MAAM,gBAAgB,GAAG,OAAO,CAAC,GAAG,CAAC,UAAU,EAAE,YAAY,CAEjD,CAAC;AACb,MAAM,OAAO,GAAiC,EAAE,CAAC;AACjD,IAAI,kBAAkB,GAAG,CAAC,CAAC;AAC3B,IACC,OAAO,gBAAgB,KAAK,UAAU;IACtC,gBAAgB,KAAK,kBAAkB,EACtC,CAAC;IACF,OAAO,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,gBAAgB,CAAC,CAAC,CAAC;AAC5C,CAAC;AACD,OAAO,CAAC,IAAI,CAAC,CAAC,UAAU,EAAE,kBAA4C,CAAC,CAAC,CAAC;AAEzE,KAAK,UAAU,cAAc,CAAI,IAAiB,EAAE,EAAoB;IACvE,MAAM,UAAU,GAAG,MAAM,CAAC,wBAAwB,CAAC,UAAU,EAAE,YAAY,CAAC,CAAC;IAC7E,MAAM,CAAC,cAAc,CAAC,UAAU,EAAE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C,CACtE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACb,MAAM,CACL,OAAO;iBACL,2BAA2B,CAAC,CAAC,mCAAmC,CAAC,CAAC;iBAClE,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,QAAQ,CAAC,CAC5B,CAAC,OAAO,CAAC,CAAC,gBAAgB,EAAE,mBAAmB,CAAC,CAAC,CAAC;YAEnD,sEAAsE;YACtE,qEAAqE;YACrE,MAAM,CAAC,aAAa,CAAC,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC;YAC7C,MAAM,CAAC,UAAU,CAAC,CAAC,mBAAmB,CAAC,EAAE,CAAC,CAAC;QAC5C,CAAC,CACD,CAAC;IACH,CAAC,CAAC,CAAC;AACJ,CAAC","sourcesContent":["import { expect, test } from \"vitest\";\nimport { URLPattern as PolyfillURLPattern } from \"urlpattern-polyfill\";\nimport { createParaglide } from \"../create-paraglide.js\";\nimport type { CompilerOptions } from \"../compiler-options.js\";\nimport { newProject } from \"@inlang/sdk\";\n\ntype PatternCtor = typeof URLPattern;\n\nconst nativeURLPattern = Reflect.get(globalThis, \"URLPattern\") as\n\t| PatternCtor\n\t| undefined;\nconst engines: Array<[string, PatternCtor]> = [];\nlet runtimeModuleNonce = 0;\nif (\n\ttypeof nativeURLPattern === \"function\" &&\n\tnativeURLPattern !== PolyfillURLPattern\n) {\n\tengines.push([\"native\", nativeURLPattern]);\n}\nengines.push([\"polyfill\", PolyfillURLPattern as unknown as PatternCtor]);\n\nasync function withURLPattern<T>(ctor: PatternCtor, fn: () => Promise<T>) {\n\tconst descriptor = Object.getOwnPropertyDescriptor(globalThis, \"URLPattern\");\n\tObject.defineProperty(globalThis, \"URLPattern\", {\n\t\tconfigurable: true,\n\t\twritable: true,\n\t\tvalue: ctor,\n\t});\n\ttry {\n\t\treturn await fn();\n\t} finally {\n\t\tif (descriptor) {\n\t\t\tObject.defineProperty(globalThis, \"URLPattern\", descriptor);\n\t\t} else {\n\t\t\tReflect.deleteProperty(globalThis, \"URLPattern\");\n\t\t}\n\t}\n}\n\nasync function withRuntime(\n\toptions: {\n\t\turlPatterns: Array<{\n\t\t\tpattern: string;\n\t\t\tlocalized: Array<[string, string]>;\n\t\t}>;\n\t\trouteStrategies?: CompilerOptions[\"routeStrategies\"];\n\t\ttrailingSlash?: \"always\" | \"never\";\n\t},\n\tctor: PatternCtor,\n\tfn: (runtime: Awaited<ReturnType<typeof createParaglide>>) => Promise<void>\n) {\n\treturn withURLPattern(ctor, async () =>\n\t\tfn(\n\t\t\tawait createParaglide({\n\t\t\t\tblob: await newProject({\n\t\t\t\t\tsettings: { baseLocale: \"en\", locales: [\"en\", \"de\"] },\n\t\t\t\t}),\n\t\t\t\tcookieName: `pattern-index-test-${runtimeModuleNonce++}`,\n\t\t\t\tstrategy: [\"url\", \"baseLocale\"],\n\t\t\t\t...options,\n\t\t\t})\n\t\t)\n\t);\n}\n\nfor (const [engineName, ctor] of engines) {\n\ttest(`${engineName} URLPattern keeps ordered routing and URL parts`, async () => {\n\t\tawait withRuntime(\n\t\t\t{\n\t\t\t\turlPatterns: [\n\t\t\t\t\t{\n\t\t\t\t\t\tpattern: \"/specific-path\",\n\t\t\t\t\t\tlocalized: [\n\t\t\t\t\t\t\t[\"en\", \"/specific-path\"],\n\t\t\t\t\t\t\t[\"de\", \"/de/404\"],\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t\t{\n\t\t\t\t\t\tpattern: \"/:path(.*)?\",\n\t\t\t\t\t\tlocalized: [\n\t\t\t\t\t\t\t[\"en\", \"/:path(.*)?\"],\n\t\t\t\t\t\t\t[\"de\", \"/de/:path(.*)?\"],\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t\trouteStrategies: [\n\t\t\t\t\t{ match: \"/specific-path\", exclude: true },\n\t\t\t\t\t{ match: \"/:path(.*)?\", strategy: [\"url\"] },\n\t\t\t\t],\n\t\t\t},\n\t\t\tctor,\n\t\t\tasync (runtime) => {\n\t\t\t\tconst input =\n\t\t\t\t\t\"https://alice:secret@example.com/specific-path?redirect=%2Faccount#section\";\n\t\t\t\texpect(runtime.localizeUrl(input, { locale: \"de\" }).href).toBe(\n\t\t\t\t\t\"https://alice:secret@example.com/de/404?redirect=%2Faccount#section\"\n\t\t\t\t);\n\t\t\t\texpect(\n\t\t\t\t\truntime.deLocalizeUrl(\n\t\t\t\t\t\t\"https://alice:secret@example.com/de/404?redirect=%2Faccount#section\"\n\t\t\t\t\t).href\n\t\t\t\t).toBe(\n\t\t\t\t\t\"https://alice:secret@example.com/specific-path?redirect=%2Faccount#section\"\n\t\t\t\t);\n\t\t\t\texpect(runtime.extractLocaleFromUrl(input)).toBe(\"en\");\n\t\t\t\texpect(\n\t\t\t\t\truntime.isExcludedByRouteStrategy(\"https://example.com/specific-path\")\n\t\t\t\t).toBe(true);\n\t\t\t\texpect(runtime.getStrategyForUrl(\"https://example.com/other\")).toEqual([\n\t\t\t\t\t\"url\",\n\t\t\t\t]);\n\t\t\t\texpect(\n\t\t\t\t\truntime\n\t\t\t\t\t\t.generateStaticLocalizedUrls([\"https://example.com/specific-path\"])\n\t\t\t\t\t\t.map((url) => url.href)\n\t\t\t\t).toEqual([\n\t\t\t\t\t\"https://example.com/specific-path\",\n\t\t\t\t\t\"https://example.com/de/404\",\n\t\t\t\t]);\n\t\t\t}\n\t\t);\n\t});\n\n\ttest(`${engineName} URLPattern refreshes ordered indexes and same-URL caches after edits`, async () => {\n\t\tawait withRuntime(\n\t\t\t{\n\t\t\t\turlPatterns: [\n\t\t\t\t\t{\n\t\t\t\t\t\tpattern: \"/:path(.*)?\",\n\t\t\t\t\t\tlocalized: [\n\t\t\t\t\t\t\t[\"en\", \"/:path(.*)?\"],\n\t\t\t\t\t\t\t[\"de\", \"/de/:path(.*)?\"],\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t\trouteStrategies: [{ match: \"/old/:slug\", exclude: true }],\n\t\t\t},\n\t\t\tctor,\n\t\t\tasync (runtime) => {\n\t\t\t\tconst url = \"https://example.com/old/story\";\n\t\t\t\texpect(runtime.extractLocaleFromUrl(url)).toBe(\"en\");\n\t\t\t\texpect(runtime.localizeUrl(url, { locale: \"de\" }).pathname).toBe(\n\t\t\t\t\t\"/de/old/story\"\n\t\t\t\t);\n\t\t\t\texpect(runtime.isExcludedByRouteStrategy(url)).toBe(true);\n\n\t\t\t\tconst pattern = runtime.urlPatterns[0]! as {\n\t\t\t\t\tpattern: string;\n\t\t\t\t\tlocalized: Array<[string, string]>;\n\t\t\t\t};\n\t\t\t\tpattern.pattern = \"/new/:path(.*)?\";\n\t\t\t\tpattern.localized[0]![1] = \"/new/:path(.*)?\";\n\t\t\t\tpattern.localized[1]![1] = \"/de/new/:path(.*)?\";\n\t\t\t\t(runtime.routeStrategies[0] as { match: string }).match = \"/new/:slug\";\n\n\t\t\t\t// Reuse the exact href so both one-entry result caches must notice\n\t\t\t\t// the config version change and the fast-path parse must be refreshed.\n\t\t\t\texpect(runtime.extractLocaleFromUrl(url)).toBe(undefined);\n\t\t\t\texpect(runtime.localizeUrl(url, { locale: \"de\" }).href).toBe(url);\n\t\t\t\texpect(runtime.isExcludedByRouteStrategy(url)).toBe(false);\n\t\t\t\texpect(\n\t\t\t\t\truntime.localizeUrl(\"https://example.com/new/story\", { locale: \"de\" })\n\t\t\t\t\t\t.pathname\n\t\t\t\t).toBe(\"/de/new/story\");\n\t\t\t}\n\t\t);\n\t});\n\n\ttest(`${engineName} URLPattern refreshes warmed winners after collection and reference edits`, async () => {\n\t\tawait withRuntime(\n\t\t\t{\n\t\t\t\turlPatterns: [\n\t\t\t\t\t{\n\t\t\t\t\t\tpattern: \"/same/:slug\",\n\t\t\t\t\t\tlocalized: [[\"de\", \"/same/:slug\"]],\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t\trouteStrategies: [{ match: \"/same/:slug\", strategy: [\"url\"] }],\n\t\t\t},\n\t\t\tctor,\n\t\t\tasync (runtime) => {\n\t\t\t\tconst url = \"https://example.com/same/story\";\n\t\t\t\texpect(runtime.extractLocaleFromUrl(url)).toBe(\"de\");\n\t\t\t\texpect(runtime.getStrategyForUrl(url)).toEqual([\"url\"]);\n\n\t\t\t\t// A pushed earlier pattern changes the extraction winner. Splicing it\n\t\t\t\t// back out must restore the original winner on this same cached URL.\n\t\t\t\t(\n\t\t\t\t\truntime.urlPatterns as Array<{\n\t\t\t\t\t\tpattern: string;\n\t\t\t\t\t\tlocalized: Array<[string, string]>;\n\t\t\t\t\t}>\n\t\t\t\t).unshift({\n\t\t\t\t\tpattern: \"/same/:slug\",\n\t\t\t\t\tlocalized: [[\"en\", \"/same/:slug\"]],\n\t\t\t\t});\n\t\t\t\texpect(runtime.extractLocaleFromUrl(url)).toBe(\"en\");\n\t\t\t\truntime.urlPatterns.splice(0, 1);\n\t\t\t\texpect(runtime.extractLocaleFromUrl(url)).toBe(\"de\");\n\n\t\t\t\t// Replacing the localized array and its locale string changes the\n\t\t\t\t// first matching locale even though its pattern text is unchanged.\n\t\t\t\tconst entry = runtime.urlPatterns[0]!;\n\t\t\t\tentry.localized = [[\"en\", \"/same/:slug\"]];\n\t\t\t\texpect(runtime.extractLocaleFromUrl(url)).toBe(\"en\");\n\n\t\t\t\t// Replacing a strategy object with the same match string must refresh\n\t\t\t\t// the warmed rule result as well as the candidate index snapshot.\n\t\t\t\truntime.routeStrategies[0] = {\n\t\t\t\t\tmatch: \"/same/:slug\",\n\t\t\t\t\tstrategy: [\"baseLocale\"],\n\t\t\t\t};\n\t\t\t\texpect(runtime.getStrategyForUrl(url)).toEqual([\"baseLocale\"]);\n\t\t\t}\n\t\t);\n\t});\n\n\ttest(`${engineName} URLPattern keeps invalid-pattern errors ahead of later matches`, async () => {\n\t\tawait withRuntime(\n\t\t\t{\n\t\t\t\turlPatterns: [\n\t\t\t\t\t{ pattern: \"/broken/:id(\", localized: [] },\n\t\t\t\t\t{\n\t\t\t\t\t\tpattern: \"/valid/:id\",\n\t\t\t\t\t\tlocalized: [\n\t\t\t\t\t\t\t[\"en\", \"/valid/:id\"],\n\t\t\t\t\t\t\t[\"de\", \"/de/valid/:id\"],\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t},\n\t\t\tctor,\n\t\t\tasync (runtime) => {\n\t\t\t\texpect(() =>\n\t\t\t\t\truntime.localizeUrl(\"https://example.com/valid/7\", { locale: \"de\" })\n\t\t\t\t).toThrow();\n\t\t\t}\n\t\t);\n\t});\n\n\ttest(`${engineName} URLPattern keeps malformed localized entries unconditional`, async () => {\n\t\tawait withRuntime({ urlPatterns: [] }, ctor, async (runtime) => {\n\t\t\t(\n\t\t\t\truntime.urlPatterns as unknown as Array<{\n\t\t\t\t\tpattern: string;\n\t\t\t\t\tlocalized: Array<[string, string]>;\n\t\t\t\t}>\n\t\t\t).push({\n\t\t\t\tpattern: \"/unrelated/:slug\",\n\t\t\t\tlocalized: [null as unknown as [string, string]],\n\t\t\t});\n\n\t\t\t// This route cannot be pruned by its path prefix: the original generic\n\t\t\t// scan would encounter the malformed localized entry and throw.\n\t\t\texpect(() =>\n\t\t\t\truntime.localizeUrl(\"https://example.com/elsewhere\", { locale: \"de\" })\n\t\t\t).toThrow();\n\t\t});\n\t});\n\n\ttest(`${engineName} URLPattern preserves trailing-slash alias captures`, async () => {\n\t\tawait withRuntime(\n\t\t\t{\n\t\t\t\ttrailingSlash: \"always\",\n\t\t\t\turlPatterns: [\n\t\t\t\t\t{\n\t\t\t\t\t\tpattern: \"/:path(.*)\",\n\t\t\t\t\t\tlocalized: [\n\t\t\t\t\t\t\t[\"en\", \"/:path(.*)\"],\n\t\t\t\t\t\t\t[\"de\", \"/de/:path(.*)/details\"],\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t},\n\t\t\tctor,\n\t\t\tasync (runtime) => {\n\t\t\t\texpect(\n\t\t\t\t\truntime.localizeUrl(\"https://example.com/foo\", { locale: \"de\" }).href\n\t\t\t\t).toBe(\"https://example.com/de/foo/details/\");\n\t\t\t}\n\t\t);\n\t});\n\n\ttest(`${engineName} URLPattern keeps file-host bases separate`, async () => {\n\t\tawait withRuntime(\n\t\t\t{\n\t\t\t\turlPatterns: [\n\t\t\t\t\t{\n\t\t\t\t\t\tpattern: \"/section/:slug\",\n\t\t\t\t\t\tlocalized: [\n\t\t\t\t\t\t\t[\"en\", \"/section/:slug\"],\n\t\t\t\t\t\t\t[\"de\", \"/de/section/:slug\"],\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t\trouteStrategies: [{ match: \"/section/:slug\", exclude: true }],\n\t\t\t},\n\t\t\tctor,\n\t\t\tasync (runtime) => {\n\t\t\t\tconst first = new URL(\"file://server-one/section/story\");\n\t\t\t\tconst second = new URL(\"file://server-two/section/story\");\n\t\t\t\texpect(runtime.extractLocaleFromUrl(first)).toBe(\"en\");\n\t\t\t\texpect(runtime.extractLocaleFromUrl(second)).toBe(\"en\");\n\t\t\t\t// Policy matching uses url.origin (\"null\" for both file hosts) as a\n\t\t\t\t// URLPattern base and therefore retains the preexisting Invalid URL\n\t\t\t\t// error. The locale extraction above exercises cache isolation safely.\n\t\t\t}\n\t\t);\n\t});\n}\n\nfor (const [engineName, ctor] of engines) {\n\ttest(`${engineName} URLPattern avoids unrelated late-route scans across APIs`, async () => {\n\t\tlet constructions = 0;\n\t\tlet executions = 0;\n\t\tclass CountingURLPattern extends ctor {\n\t\t\tconstructor(pattern?: string | URLPatternInit, baseURL?: string | URL) {\n\t\t\t\tif (baseURL === undefined) super(pattern as string | URLPatternInit);\n\t\t\t\telse super(pattern as string | URLPatternInit, String(baseURL));\n\t\t\t\tconstructions++;\n\t\t\t}\n\n\t\t\toverride exec(input: string | URL, baseURL?: string) {\n\t\t\t\texecutions++;\n\t\t\t\treturn baseURL === undefined\n\t\t\t\t\t? super.exec(input)\n\t\t\t\t\t: super.exec(input, baseURL);\n\t\t\t}\n\t\t}\n\n\t\tconst urlPatterns = Array.from({ length: 67 }, (_, index) => ({\n\t\t\tpattern: `/route${index}/:slug`,\n\t\t\tlocalized: [\n\t\t\t\t[\"en\", `/route${index}/:slug`] as [string, string],\n\t\t\t\t[\"de\", `/de/route${index}/:slug`] as [string, string],\n\t\t\t],\n\t\t}));\n\t\tconst routeStrategies = Array.from({ length: 67 }, (_, index) => ({\n\t\t\tmatch: `/route${index}/:slug`,\n\t\t\texclude: true as const,\n\t\t}));\n\n\t\tawait withRuntime(\n\t\t\t{ urlPatterns, routeStrategies },\n\t\t\tCountingURLPattern as unknown as PatternCtor,\n\t\t\tasync (runtime) => {\n\t\t\t\texpect(\n\t\t\t\t\truntime.localizeUrl(\"https://example.com/route66/story\", {\n\t\t\t\t\t\tlocale: \"de\",\n\t\t\t\t\t}).pathname\n\t\t\t\t).toBe(\"/de/route66/story\");\n\t\t\t\texpect(\n\t\t\t\t\truntime.deLocalizeUrl(\"https://example.com/de/route66/story\").pathname\n\t\t\t\t).toBe(\"/route66/story\");\n\t\t\t\texpect(\n\t\t\t\t\truntime.extractLocaleFromUrl(\"https://example.com/de/route66/story\")\n\t\t\t\t).toBe(\"de\");\n\t\t\t\texpect(\n\t\t\t\t\truntime.isExcludedByRouteStrategy(\"https://example.com/route66/story\")\n\t\t\t\t).toBe(true);\n\t\t\t\texpect(\n\t\t\t\t\truntime\n\t\t\t\t\t\t.generateStaticLocalizedUrls([\"https://example.com/route66/story\"])\n\t\t\t\t\t\t.map((url) => url.pathname)\n\t\t\t\t).toEqual([\"/route66/story\", \"/de/route66/story\"]);\n\n\t\t\t\t// Includes createParaglide's capability probe. URLPattern work across\n\t\t\t\t// five APIs stays proportional to the few matching patterns, not 67.\n\t\t\t\texpect(constructions).toBeLessThanOrEqual(4);\n\t\t\t\texpect(executions).toBeLessThanOrEqual(20);\n\t\t\t}\n\t\t);\n\t});\n}\n"]}
|
|
@@ -93,7 +93,7 @@ function createCompiledMessagesObject(compiledBundles) {
|
|
|
93
93
|
function injectCode(path) {
|
|
94
94
|
const code = fs.readFileSync(new URL(path, import.meta.url), "utf-8");
|
|
95
95
|
// Regex to match single-line and multi-line imports
|
|
96
|
-
const importRegex =
|
|
96
|
+
const importRegex = /^\s*import\s+[\s\S]*?from\s+['"][^'"]+['"]\s*;?/gm;
|
|
97
97
|
const sourceMapRegex = /\/\/# sourceMappingURL=.*$/gm;
|
|
98
98
|
const blockSourceMapRegex = /\/\*# sourceMappingURL=.*?\*\//g;
|
|
99
99
|
return code
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-server-file.js","sourceRoot":"","sources":["../../../src/compiler/server/create-server-file.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AAGzB,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAGtD;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAAC,IAUhC;IACA,IAAI,IAAI,GAAG;;;EAGV,UAAU,CAAC,iBAAiB,CAAC;CAC9B,CAAC;IAED,MAAM,uBAAuB,GAAG,yBAAyB,CAAC;IAC1D,MAAM,WAAW,GAAG,IAAI,CAAC,OAAO,CAAC,uBAAuB,CAAC,CAAC;IAC1D,IAAI,WAAW,KAAK,CAAC,CAAC,EAAE,CAAC;QACxB,MAAM,IAAI,KAAK,CACd,qEAAqE,CACrE,CAAC;IACH,CAAC;IAED,MAAM,SAAS,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,EAAE,WAAW,CAAC,GAAG,CAAC,CAAC;IAC1D,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,SAAS,EAAE,WAAW,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IACvE,MAAM,WAAW,GAAG,GAAG,MAAM,IAAI,CAAC;IAClC,MAAM,sBAAsB,GAAG,IAAI,CAAC,eAAe,CAAC,wBAAwB;QAC3E,CAAC,CAAC;YACA,GAAG,MAAM,kEAAkE;YAC3E,GAAG,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AAC,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;AAClC,CAAC;AAED,SAAS,4BAA4B,CACpC,eAA6C;IAE7C,MAAM,MAAM,GAAG,EAA4C,CAAC;IAE5D,KAAK,MAAM,cAAc,IAAI,eAAe,EAAE,CAAC;QAC9C,MAAM,QAAQ,GAAG,cAAc,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;QAC/C,MAAM,YAAY,GAAG,cAAc,CAAC,QAAQ,CAAC,CAAC;QAC9C,uEAAuE;QACvE,qDAAqD;QACrD,uEAAuE;QACvE,gEAAgE;QAChE,mDAAmD;QACnD,IAAI,MAAM,CAAC,YAAY,CAAC,KAAK,SAAS,EAAE,CAAC;YACxC,MAAM,CAAC,YAAY,CAAC,GAAG,EAAE,CAAC;QAC3B,CAAC;QACD,KAAK,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,IAAI,MAAM,CAAC,OAAO,CACrD,cAAc,CAAC,QAAQ,CACvB,EAAE,CAAC;YACH,MAAM,CAAC,YAAY,CAAC,CAAC,MAAM,CAAC,GAAG,eAAe,CAAC,IAAI;iBACjD,OAAO,CAAC,gBAAgB,YAAY,KAAK,EAAE,EAAE,CAAC;iBAC9C,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QACrB,CAAC;IACF,CAAC;IACD,OAAO,MAAM,CAAC;AACf,CAAC;AAED;;;;;;;;GAQG;AACH,SAAS,UAAU,CAAC,IAAY;IAC/B,MAAM,IAAI,GAAG,EAAE,CAAC,YAAY,CAAC,IAAI,GAAG,CAAC,IAAI,EAAE,OAAO,IAAI,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC,CAAC;IACtE,oDAAoD;IACpD,MAAM,WAAW,GAAG,
|
|
1
|
+
{"version":3,"file":"create-server-file.js","sourceRoot":"","sources":["../../../src/compiler/server/create-server-file.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AAGzB,OAAO,EAAE,cAAc,EAAE,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AC3B,CAAC;QACD,KAAK,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,IAAI,MAAM,CAAC,OAAO,CACrD,cAAc,CAAC,QAAQ,CACvB,EAAE,CAAC;YACH,MAAM,CAAC,YAAY,CAAC,CAAC,MAAM,CAAC,GAAG,eAAe,CAAC,IAAI;iBACjD,OAAO,CAAC,gBAAgB,YAAY,KAAK,EAAE,EAAE,CAAC;iBAC9C,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;QACrB,CAAC;IACF,CAAC;IACD,OAAO,MAAM,CAAC;AACf,CAAC;AAED;;;;;;;;GAQG;AACH,SAAS,UAAU,CAAC,IAAY;IAC/B,MAAM,IAAI,GAAG,EAAE,CAAC,YAAY,CAAC,IAAI,GAAG,CAAC,IAAI,EAAE,OAAO,IAAI,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC,CAAC;IACtE,oDAAoD;IACpD,MAAM,WAAW,GAAG,mDAAmD,CAAC;IACxE,MAAM,cAAc,GAAG,8BAA8B,CAAC;IACtD,MAAM,mBAAmB,GAAG,iCAAiC,CAAC;IAC9D,OAAO,IAAI;SACT,OAAO,CAAC,WAAW,EAAE,EAAE,CAAC;SACxB,OAAO,CAAC,cAAc,EAAE,EAAE,CAAC;SAC3B,OAAO,CAAC,mBAAmB,EAAE,EAAE,CAAC;SAChC,IAAI,EAAE,CAAC;AACV,CAAC","sourcesContent":["import fs from \"node:fs\";\nimport type { CompiledBundleWithMessages } from \"../compile-bundle.js\";\nimport type { CompilerOptions } from \"../compiler-options.js\";\nimport { toSafeModuleId } from \"../safe-module-id.js\";\nimport type { Locale } from \"./runtime.d.ts\";\n\n/**\n * Returns the code for the `runtime.js` module\n */\nexport function createServerFile(args: {\n\tcompiledBundles: CompiledBundleWithMessages[];\n\tcompilerOptions: {\n\t\texperimentalMiddlewareLocaleSplitting: NonNullable<\n\t\t\tCompilerOptions[\"experimentalMiddlewareLocaleSplitting\"]\n\t\t>;\n\t\tdisableAsyncLocalStorage: NonNullable<\n\t\t\tCompilerOptions[\"disableAsyncLocalStorage\"]\n\t\t>;\n\t};\n}): string {\n\tlet code = `\nimport * as runtime from \"./runtime.js\";\n\n${injectCode(\"./middleware.js\")}\n`;\n\n\tconst asyncLocalStorageMarker = \"// %async-local-storage\";\n\tconst markerIndex = code.indexOf(asyncLocalStorageMarker);\n\tif (markerIndex === -1) {\n\t\tthrow new Error(\n\t\t\t\"Expected a single %async-local-storage marker in server middleware.\"\n\t\t);\n\t}\n\n\tconst lineStart = code.lastIndexOf(\"\\n\", markerIndex) + 1;\n\tconst indent = code.slice(lineStart, markerIndex).replace(/\\t/g, \" \");\n\tconst innerIndent = `${indent} `;\n\tconst asyncLocalStorageBlock = args.compilerOptions.disableAsyncLocalStorage\n\t\t? [\n\t\t\t\t`${indent}requestAsyncLocalStorage = runtime.getServerAsyncLocalStorage();`,\n\t\t\t\t`${indent}if (!requestAsyncLocalStorage) {`,\n\t\t\t\t`${innerIndent}requestAsyncLocalStorage = createMockAsyncLocalStorage();`,\n\t\t\t\t`${innerIndent}runtime.overwriteServerAsyncLocalStorage(requestAsyncLocalStorage);`,\n\t\t\t\t`${indent}}`,\n\t\t\t]\n\t\t: [\n\t\t\t\t`${indent}requestAsyncLocalStorage = runtime.getServerAsyncLocalStorage();`,\n\t\t\t\t`${indent}if (!runtime.disableAsyncLocalStorage && !requestAsyncLocalStorage) {`,\n\t\t\t\t`${innerIndent}const { AsyncLocalStorage } = await import(\"async_hooks\");`,\n\t\t\t\t`${innerIndent}requestAsyncLocalStorage = runtime.getServerAsyncLocalStorage();`,\n\t\t\t\t`${innerIndent}if (!requestAsyncLocalStorage) {`,\n\t\t\t\t`${innerIndent} requestAsyncLocalStorage = new AsyncLocalStorage();`,\n\t\t\t\t`${innerIndent} runtime.overwriteServerAsyncLocalStorage(requestAsyncLocalStorage);`,\n\t\t\t\t`${innerIndent}}`,\n\t\t\t\t`${indent}}`,\n\t\t\t\t`${indent}if (!requestAsyncLocalStorage) {`,\n\t\t\t\t`${innerIndent}requestAsyncLocalStorage = createMockAsyncLocalStorage();`,\n\t\t\t\t`${innerIndent}runtime.overwriteServerAsyncLocalStorage(requestAsyncLocalStorage);`,\n\t\t\t\t`${indent}}`,\n\t\t\t];\n\n\tconst lineEnd = code.indexOf(\"\\n\", markerIndex);\n\tconst endOfMarkerLine = lineEnd === -1 ? code.length : lineEnd + 1;\n\n\tcode =\n\t\tcode.slice(0, lineStart) +\n\t\t`${asyncLocalStorageBlock.join(\"\\n\")}\\n` +\n\t\tcode.slice(endOfMarkerLine);\n\n\tif (args.compilerOptions.experimentalMiddlewareLocaleSplitting) {\n\t\t// Use a replacer function, not a replacement string. `String.replace()`\n\t\t// treats a string replacement as a pattern where `$&`, `` $` ``, `$'`,\n\t\t// `$$`, and `$<name>` have special meaning. A translated message value\n\t\t// that contains a literal `$` (a price, a currency symbol) can produce\n\t\t// one of these sequences inside the JSON payload, which `String.replace`\n\t\t// then expands - e.g. `` $` `` is replaced with \"everything before the\n\t\t// match\", splicing the file's own header into the string being built and\n\t\t// corrupting the generated server file. A function's return value is\n\t\t// used literally, with no pattern expansion.\n\t\tcode = code.replace(\n\t\t\t\"const compiledBundles = {};\",\n\t\t\t() =>\n\t\t\t\t`const compiledBundles = ${JSON.stringify(createCompiledMessagesObject(args.compiledBundles))};`\n\t\t);\n\t}\n\n\treturn code.replace(/\\t/g, \" \");\n}\n\nfunction createCompiledMessagesObject(\n\tcompiledBundles: CompiledBundleWithMessages[]\n): Record<string, Record<Locale, string>> {\n\tconst result = {} as Record<string, Record<Locale, string>>;\n\n\tfor (const compiledBundle of compiledBundles) {\n\t\tconst bundleId = compiledBundle.bundle.node.id;\n\t\tconst safeModuleId = toSafeModuleId(bundleId);\n\t\t// Key by the safe module id, not the raw bundle id: trackMessageCall()\n\t\t// records calls under safeModuleId, the client reads\n\t\t// globalThis.__paraglide.ssr[safeModuleId], and middleware.js looks up\n\t\t// compiledBundles[id] with that same safe id. Keying by the raw\n\t\t// bundleId here made every split-mode lookup miss.\n\t\tif (result[safeModuleId] === undefined) {\n\t\t\tresult[safeModuleId] = {};\n\t\t}\n\t\tfor (const [locale, compiledMessage] of Object.entries(\n\t\t\tcompiledBundle.messages\n\t\t)) {\n\t\t\tresult[safeModuleId][locale] = compiledMessage.code\n\t\t\t\t.replace(`export const ${safeModuleId} = `, \"\")\n\t\t\t\t.replace(/;$/, \"\");\n\t\t}\n\t}\n\treturn result;\n}\n\n/**\n * Load a file from the current directory.\n *\n * Prunes the imports on top of the file as the runtime is\n * self-contained.\n *\n * @param {string} path\n * @returns {string}\n */\nfunction injectCode(path: string): string {\n\tconst code = fs.readFileSync(new URL(path, import.meta.url), \"utf-8\");\n\t// Regex to match single-line and multi-line imports\n\tconst importRegex = /^\\s*import\\s+[\\s\\S]*?from\\s+['\"][^'\"]+['\"]\\s*;?/gm;\n\tconst sourceMapRegex = /\\/\\/# sourceMappingURL=.*$/gm;\n\tconst blockSourceMapRegex = /\\/\\*# sourceMappingURL=.*?\\*\\//g;\n\treturn code\n\t\t.replace(importRegex, \"\")\n\t\t.replace(sourceMapRegex, \"\")\n\t\t.replace(blockSourceMapRegex, \"\")\n\t\t.trim();\n}\n"]}
|
|
@@ -46,6 +46,18 @@ export const isExcludedByRouteStrategy = runtime.isExcludedByRouteStrategy;`);
|
|
|
46
46
|
expect(response.status).toBe(200);
|
|
47
47
|
expect(await response.text()).toBe("ready");
|
|
48
48
|
});
|
|
49
|
+
test("import statements in JSDoc examples survive in the generated server file", () => {
|
|
50
|
+
const serverCode = createServerFile({
|
|
51
|
+
compiledBundles: [],
|
|
52
|
+
compilerOptions: {
|
|
53
|
+
disableAsyncLocalStorage: false,
|
|
54
|
+
experimentalMiddlewareLocaleSplitting: false,
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
expect(serverCode).toContain(` * import { paraglideMiddleware } from './paraglide/server.js'\n * import handler from '@tanstack/react-start/server-entry'\n *\n * export default {`);
|
|
58
|
+
// only the `import * as runtime` line that createServerFile itself prepends
|
|
59
|
+
expect(serverCode.match(/^\s*import\s/gm)).toHaveLength(1);
|
|
60
|
+
});
|
|
49
61
|
test("keys compiledBundles by the safe module id, not the raw bundle id (experimentalMiddlewareLocaleSplitting)", () => {
|
|
50
62
|
// A bundle id that changes under toSafeModuleId (case-folded + a numeric
|
|
51
63
|
// suffix appended for the uppercase letters). trackMessageCall(), the
|